Skip to content

Commit

Permalink
Add structure to integration tests
Browse files Browse the repository at this point in the history
Break out some common test functionality into `util.mts` and add another
level of directory when creating the test directories so we can have
multiple separate tests within each test driver instead of having to
push lots of different tests into the same one.

I couldn't find a way to augment the test context with any additional
properties (like the test directory) from the `beforeEach` method so
instead at the beginning of each test we have to have
`const dir = getTestDir(...);`

Move the test directory into `dist`, which means we no longer need a
custom `.gitignore` to ignore the `staging` directory.
  • Loading branch information
elliotgoodrich committed Jun 2, 2024
1 parent 0584429 commit f9b2567
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 66 deletions.
14 changes: 6 additions & 8 deletions integration/src/biome.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,17 @@ import {
makeLintRule,
makeFormatToRule,
} from "@ninjutsu-build/biome";
import { mkdirSync, rmSync, existsSync } from "node:fs";
import { existsSync } from "node:fs";
import { execSync, spawnSync } from "node:child_process";
import { join } from "node:path/posix";
import { getTestDir, setup } from "./util.mjs";

const dir = join("integration", "staging", "biome");
describe("biome", (suiteCtx) => {
beforeEach(setup(suiteCtx));

describe("biome tests", () => {
beforeEach(() => {
rmSync(dir, { force: true, recursive: true });
mkdirSync(dir);
});
test("Basic example", (testCtx) => {
const dir = getTestDir(suiteCtx, testCtx);

test("Basic example", () => {
const formatted = "formatted.mts";
writeFileSync(join(dir, formatted), "export const value = { foo: 1 };\n");
const unformatted = "unformatted.mts";
Expand Down
15 changes: 5 additions & 10 deletions integration/src/esbuild.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,15 @@ import { readFileSync, writeFileSync } from "node:fs";
import { NinjaBuilder } from "@ninjutsu-build/core";
import { makeESBuildRule } from "@ninjutsu-build/esbuild";
import { makeNodeRule } from "@ninjutsu-build/node";
import { mkdirSync, rmSync } from "node:fs";
import { execSync, spawnSync } from "node:child_process";
import { join } from "node:path";
import { getDeps } from "./util.mjs";
import { getDeps, getTestDir, setup } from "./util.mjs";

const dir = join("integration", "staging", "esbuild");
describe("esbuild", (suiteCtx) => {
beforeEach(setup(suiteCtx));

describe("esbuild tests", () => {
beforeEach(() => {
rmSync(dir, { force: true, recursive: true });
mkdirSync(dir);
});

test("Basic example", () => {
test("Basic example", (testCtx) => {
const dir = getTestDir(suiteCtx, testCtx);
const add = "add.mts";
writeFileSync(
join(dir, add),
Expand Down
16 changes: 6 additions & 10 deletions integration/src/node.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,16 @@ import { strict as assert } from "node:assert";
import { readFileSync, writeFileSync } from "node:fs";
import { NinjaBuilder } from "@ninjutsu-build/core";
import { makeNodeRule } from "@ninjutsu-build/node";
import { mkdirSync, rmSync, symlinkSync } from "node:fs";
import { mkdirSync, symlinkSync } from "node:fs";
import { execSync } from "node:child_process";
import { join } from "node:path";
import { callNinja, depsMatch, getDeps } from "./util.mjs";
import { callNinja, depsMatch, getDeps, setup, getTestDir } from "./util.mjs";

const dir = join("integration", "staging", "node");
describe("node", (suiteCtx) => {
beforeEach(setup(suiteCtx));

describe("node tests", () => {
beforeEach(() => {
rmSync(dir, { force: true, recursive: true });
mkdirSync(dir);
});

test("Basic example", () => {
test("Basic example", (testCtx) => {
const dir = getTestDir(suiteCtx, testCtx);
const zero = "number/zero.cjs";
mkdirSync(join(dir, "number"));
writeFileSync(join(dir, zero), "exports.zero = 0;\n");
Expand Down
71 changes: 34 additions & 37 deletions integration/src/tsc.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,23 @@ import { beforeEach, test, describe } from "node:test";
import { strict as assert } from "node:assert";
import { NinjaBuilder, getInput, validations } from "@ninjutsu-build/core";
import { makeTSCRule, makeTypeCheckRule } from "@ninjutsu-build/tsc";
import {
writeFileSync,
mkdirSync,
rmSync,
symlinkSync,
existsSync,
} from "node:fs";
import { writeFileSync, mkdirSync, symlinkSync, existsSync } from "node:fs";
import { join } from "node:path/posix";
import { getDeps, callNinja, callNinjaWithFailure } from "./util.mjs";
import {
getDeps,
callNinja,
callNinjaWithFailure,
getTestDir,
setup,
} from "./util.mjs";
import { relative as relativeNative, sep } from "node:path";
import { fileURLToPath } from "node:url";

const dir = join("integration", "staging", "tsc");

// Tell TypeScript to look for `@types/node` package installed in the
// workspace `node_modules` directory, otherwise it'll fail to find it
const typeRoots = [
relativeNative(
dir,
fileURLToPath(import.meta.resolve("@types/node/package.json")),
)
.split(sep)
.slice(0, -2)
.join("/"),
];

const compilerOptions = {
outDir: "dist",
declaration: true,
strict: true,
alwaysStrict: true,
skipLibCheck: true,
typeRoots,
};

describe("tsc tests", () => {
beforeEach(() => {
rmSync(dir, { force: true, recursive: true });
mkdirSync(dir);
});
describe("tsc", (suiteCtx) => {
beforeEach(setup(suiteCtx));

test("Basic example", () => {
test("Basic example", (testCtx) => {
const dir = getTestDir(suiteCtx, testCtx);
const negate = "negate.mts";
writeFileSync(
join(dir, negate),
Expand Down Expand Up @@ -113,6 +88,28 @@ describe("tsc tests", () => {
const ninja = new NinjaBuilder({}, dir);
const tsc = makeTSCRule(ninja);
const typecheck = makeTypeCheckRule(ninja);

// Tell TypeScript to look for `@types/node` package installed in the
// workspace `node_modules` directory, otherwise it'll fail to find it
const typeRoots = [
relativeNative(
dir,
fileURLToPath(import.meta.resolve("@types/node/package.json")),
)
.split(sep)
.slice(0, -2)
.join("/"),
];

const compilerOptions = {
outDir: "dist",
declaration: true,
strict: true,
alwaysStrict: true,
skipLibCheck: true,
typeRoots,
};

const output = tsc({ in: [script], compilerOptions });
const output2 = tsc({ in: [script2], compilerOptions });
const [stamp] = typecheck({
Expand Down
18 changes: 18 additions & 0 deletions integration/src/util.mts
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import { execSync, spawnSync } from "node:child_process";
import { strict as assert } from "node:assert";
import { mkdirSync, rmSync } from "node:fs";

export function getTestDir(
suiteCtx: { readonly name: string },
testCtx: { readonly name: string },
): string {
return `integration/dist/${suiteCtx.name}/${testCtx.name}`;
}

export function setup(suiteCtx: { readonly name: string }): (s: {
readonly name: string;
}) => void {
return (testCtx: { readonly name: string }) => {
const dir = getTestDir(suiteCtx, testCtx);
rmSync(dir, { force: true, recursive: true });
mkdirSync(dir, { recursive: true });
};
}

export function getDeps(cwd: string): Record<string, string[]> {
const deps: Record<string, string[]> = {};
Expand Down
1 change: 0 additions & 1 deletion integration/staging/.gitignore

This file was deleted.

0 comments on commit f9b2567

Please sign in to comment.