mirror of
https://github.com/CompeyDev/bun-lune-loader.git
synced 2024-12-12 04:40:39 +00:00
feat: initial tests implementation
This commit is contained in:
parent
680724233d
commit
02929fb1c6
11 changed files with 101 additions and 19 deletions
|
@ -12,4 +12,5 @@ To run:
|
|||
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.
|
||||
|
|
13
build.ts
13
build.ts
|
@ -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
BIN
bun.lockb
Normal file → Executable file
Binary file not shown.
|
@ -1,3 +0,0 @@
|
|||
import { SomeData } from "./data.luau";
|
||||
|
||||
console.log("Received data: ", SomeData)
|
8
test.ts
Normal file
8
test.ts
Normal 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
3
tests/import.test.ts
Normal file
|
@ -0,0 +1,3 @@
|
|||
import { SomeData } from "./data.luau";
|
||||
|
||||
console.log("[LOG] tests::import -> Received data: ", SomeData);
|
67
tests/runner.ts
Normal file
67
tests/runner.ts
Normal 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
19
utils/build.ts
Normal 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");
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue