feat: initial tests implementation

This commit is contained in:
Erica Marigold 2023-09-25 19:25:39 +05:30
parent 680724233d
commit 02929fb1c6
11 changed files with 101 additions and 19 deletions

View file

@ -12,4 +12,5 @@ To run:
bun run src/index.ts bun run src/index.ts
``` ```
This project was created using `bun init` in bun v1.0.0. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime. This project was created using `bun init` in bun v1.0.0. [Bun](https://bun.sh)
is a fast all-in-one JavaScript runtime.

View file

@ -1,13 +0,0 @@
import plugin from "./src/index";
const out = await Bun.build({
plugins: [plugin],
entrypoints: ["./examples/run.ts"],
});
if (out.success) {
eval(await out.outputs[0].text())
} else {
console.warn("Build failed:")
console.warn(out.logs.map((v) => " " + v.message).join("\n"))
}

BIN
bun.lockb Normal file → Executable file

Binary file not shown.

View file

@ -1,3 +0,0 @@
import { SomeData } from "./data.luau";
console.log("Received data: ", SomeData)

8
test.ts Normal file
View file

@ -0,0 +1,8 @@
import { TestRunner } from "./tests/runner";
await new TestRunner({
collection: {
"import": "./tests/import.test.ts",
},
})
.run();

3
tests/import.test.ts Normal file
View file

@ -0,0 +1,3 @@
import { SomeData } from "./data.luau";
console.log("[LOG] tests::import -> Received data: ", SomeData);

67
tests/runner.ts Normal file
View file

@ -0,0 +1,67 @@
import { buildFromSource as build } from "../utils/build";
export class TestRunner {
private tests: Record<string, string>;
private dryRun: boolean;
private ignoreFail: boolean;
private callback: (
meta: { name: string; success: boolean; timeElapsed: number },
) => void;
constructor(opts: {
dryRun?: boolean;
ignoreFail?: boolean;
collection: Record<string, string>;
callback?: (meta: { name: string; success: boolean }) => void;
}) {
this.tests = opts.collection;
this.dryRun = opts.dryRun ?? false;
this.ignoreFail = opts.ignoreFail ?? false;
this.callback = ({ name, success, timeElapsed }) => {
console.log(
`${
success ? "[OK]" : "[FAILED]"
} tests::${name} ...(${timeElapsed}ms)`,
);
};
}
async run() {
console.log(`Running ${Object.keys(this.tests).length} tests...`)
for (let testName in this.tests) {
const callbackOpts = {
name: testName,
success: true,
timeElapsed: 0,
};
const startTime = performance.now();
if (!this.dryRun) {
// Build and execute test from source
let testFailed: boolean;
await build(this.tests[testName]).catch(() => {
// Test failed
testFailed = true;
this.callback({
...callbackOpts,
timeElapsed: Number((performance.now() - startTime).toFixed(2)),
success: this.ignoreFail ? true : false,
});
}).then(() => {
!testFailed
? this.callback({
...callbackOpts,
timeElapsed: Number((performance.now() - startTime).toFixed(2)),
})
: {};
});
} else {
this.callback(callbackOpts);
}
}
}
}

19
utils/build.ts Normal file
View file

@ -0,0 +1,19 @@
import plugin from "../src/index";
export async function buildFromSource(entrypoint: string): Promise<void> {
const out = await Bun.build({
plugins: [plugin],
entrypoints: [entrypoint],
});
if (out.success) {
eval(await out.outputs[0].text());
Promise.resolve();
} else {
console.warn("Build failed:");
console.warn(out.logs.map((v) => " " + v.message).join("\n"));
Promise.reject("failed to build");
}
}