Skip to content

Commit

Permalink
test: make tests compatible with parallel execution
Browse files Browse the repository at this point in the history
  • Loading branch information
ozum committed Mar 13, 2021
1 parent eceb41e commit 32d388a
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 29 deletions.
72 changes: 72 additions & 0 deletions test/__snapshots__/generate.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,78 @@ value;
}
`;

exports[`generate should not clear output directory if clear is false. 1`] = `
Object {
"README.md": "# Hello!
",
"archive.purchase-history.js": "id;
contact_id;
purchased_at;
total_amount;
",
"archive/archive.md": "# Schema:
",
"index.js": "const ArchivePurchaseHistory = \\"\\";
const PublicAccount = \\"\\";
const PublicContact = \\"\\";
const PublicLineItem = \\"\\";
const PublicProduct = \\"\\";
const PublicPurchase = \\"\\";
const PublicPurchaseHistory = \\"\\";
const PublicRegion = \\"\\";
const PublicSystemOption = \\"\\";
const PublicSystemOptionHistory = \\"\\";
",
"public.account.js": "id;
name;
primary_contact_id;
secondary_contact_id;
",
"public.contact.js": "id;
account_id;
region_Id;
name;
surname;
phone;
preferred_languages;
priority;
",
"public.line-item.js": "purchase_id;
product_id;
quantity;
unit_price;
",
"public.product.js": "id;
name;
price;
",
"public.purchase-history.js": "id;
contact_id;
purchased_at;
total_amount;
",
"public.purchase.js": "id;
created_at;
purchased_at;
contact_id;
",
"public.region.js": "id;
name;
parentid;
",
"public.system-option-history.js": "id;
system_option_key;
system_option_sub_key;
",
"public.system-option.js": "key;
sub_key;
value;
",
"public/public.md": "# Schema:
",
}
`;

exports[`generate should not create files if render returns undefined. 1`] = `
Object {
"useless.md": "# Useless
Expand Down
56 changes: 27 additions & 29 deletions test/generate.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import { join } from "path";
import { promises as fs, existsSync } from "fs";
import { promises as fs } from "fs";
import { load } from "fs-structure";
import { ignoreCode } from "ignor";
import { tmpdir } from "os";
import { generate, Options } from "../src/index";
import App from "./test-helper/pg-generator-test/src/app/index";

const TEST_ROOT = join(tmpdir(), "pgen");
const outDir = join(TEST_ROOT, "out");
const outDir2 = join(TEST_ROOT, "out2");
const app = new App({ outDir }, { db: {}, templateDir: join(__dirname, "templates") } as any);

// WORKAROUND: Jest does not output error of dynamic import in (src/utils/compose-with.ts). This function rethrows error.
async function g(subGenerator = "app", options?: Options, { argLength = 3, cwd = "" } = {}): ReturnType<typeof load> {
/**
* Helper function to execute generate and return file-structure as a result to compare with snapshots.
*/
async function g(subGenerator = "app", options?: Options, { argLength = 3, cwd = false } = {}): ReturnType<typeof load> {
const oldCwd = process.cwd();
if (cwd) {
await fs.mkdir(cwd, { recursive: true });
process.chdir(cwd);
}
const tempDir = await fs.mkdtemp(join(tmpdir(), "pg-generator-"));
const outDir = join(tempDir, "out"); // For tests, directory should not be created yet.

if (!options?.clear) await fs.mkdir(outDir, { recursive: true }); // Create output directory to test "clear: false".

const mergedOptions = {
clear: true,
Expand All @@ -27,24 +23,26 @@ async function g(subGenerator = "app", options?: Options, { argLength = 3, cwd =
...options,
};

if (cwd) {
await fs.mkdir(outDir, { recursive: true });
process.chdir(outDir);
}

try {
// To test generate function with 2 paramerets and 3 parameters.
// To test generate function with 1,2 and 3 parameters.
if (argLength === 3) await generate(join(__dirname, "test-helper/pg-generator-test/src"), subGenerator, mergedOptions);
else if (argLength === 2) await generate(join(__dirname, "test-helper/pg-generator-test/src", subGenerator), mergedOptions);
else if (argLength === 1) await generate(join(__dirname, "test-helper/pg-generator-test/src", subGenerator));
const structure = await load(mergedOptions.outDir ?? outDir);
process.chdir(oldCwd);
return structure;
return await load(mergedOptions.outDir ?? outDir);
} catch (error) {
process.chdir(oldCwd);
// WORKAROUND: Jest does not output error of dynamic import in (src/utils/compose-with.ts). This function rethrows error.
throw new Error(error.message);
} finally {
process.chdir(oldCwd);
fs.rmdir(tempDir, { recursive: true });
}
}

afterAll(async () => {
await fs.rmdir(TEST_ROOT, { recursive: true });
});

describe("generate", () => {
it("should generate files.", async () => {
expect(await g("app")).toMatchSnapshot();
Expand All @@ -55,7 +53,7 @@ describe("generate", () => {
});

it("should generate files if called with 1 argument.", async () => {
await expect(() => g("app", {}, { argLength: 1, cwd: outDir })).rejects.toThrow(" have same names");
await expect(() => g("app", {}, { argLength: 1, cwd: true })).rejects.toThrow(" have same names");
});

it("should add context from context module using options.", async () => {
Expand Down Expand Up @@ -83,8 +81,7 @@ describe("generate", () => {
});

it("should not clear output directory if clear is false.", async () => {
await g("app", { clear: false, outDir: outDir2 });
expect(existsSync(outDir2)).toBe(true);
expect(await g("app", { clear: false })).toMatchSnapshot();
});

it("should throw if target is cwd and clear is true", async () => {
Expand All @@ -96,9 +93,7 @@ describe("generate", () => {
});

it("should output cwd if no outDir is provided.", async () => {
await fs.rmdir(outDir, { recursive: true }).catch(ignoreCode("ENOENT"));
await fs.mkdir(outDir);
expect(await g("app", { outDir: undefined, clear: false }, { cwd: outDir })).toMatchSnapshot();
expect(await g("app", { outDir: undefined, clear: false }, { cwd: true })).toMatchSnapshot();
});

it("should throw if a filter in file name cannot be found.", async () => {
Expand All @@ -123,8 +118,11 @@ describe("generate", () => {
});

describe("App", () => {
const appOutDir = join(tmpdir(), "pgen");
const app = new App({ outDir: appOutDir }, { db: {}, templateDir: join(__dirname, "templates") } as any);

it("should return default destination path.", () => {
expect(app.defaultDestinationPath()).toBe(outDir);
expect(app.defaultDestinationPath()).toBe(appOutDir);
});

it("should return default template path.", () => {
Expand Down

0 comments on commit 32d388a

Please sign in to comment.