diff --git a/README.md b/README.md index adfaafb..f93cbee 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/build.ts b/build.ts deleted file mode 100644 index f15cebc..0000000 --- a/build.ts +++ /dev/null @@ -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")) -} \ No newline at end of file diff --git a/bun.lockb b/bun.lockb old mode 100644 new mode 100755 index 4ad2e83..3c51561 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/examples/run.ts b/examples/run.ts deleted file mode 100644 index 7e955b7..0000000 --- a/examples/run.ts +++ /dev/null @@ -1,3 +0,0 @@ -import { SomeData } from "./data.luau"; - -console.log("Received data: ", SomeData) \ No newline at end of file diff --git a/package.json b/package.json index 9af1b3a..a8552fe 100644 --- a/package.json +++ b/package.json @@ -8,4 +8,4 @@ "peerDependencies": { "typescript": "^5.0.0" } -} \ No newline at end of file +} diff --git a/src/index.ts b/src/index.ts index e85c5aa..8295f3d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -16,4 +16,4 @@ const plugin: BunPlugin = { }, }; -export default plugin; \ No newline at end of file +export default plugin; diff --git a/test.ts b/test.ts new file mode 100644 index 0000000..414ad75 --- /dev/null +++ b/test.ts @@ -0,0 +1,8 @@ +import { TestRunner } from "./tests/runner"; + +await new TestRunner({ + collection: { + "import": "./tests/import.test.ts", + }, +}) + .run(); diff --git a/examples/data.luau b/tests/data.luau similarity index 100% rename from examples/data.luau rename to tests/data.luau diff --git a/tests/import.test.ts b/tests/import.test.ts new file mode 100644 index 0000000..e9b4044 --- /dev/null +++ b/tests/import.test.ts @@ -0,0 +1,3 @@ +import { SomeData } from "./data.luau"; + +console.log("[LOG] tests::import -> Received data: ", SomeData); diff --git a/tests/runner.ts b/tests/runner.ts new file mode 100644 index 0000000..96709fe --- /dev/null +++ b/tests/runner.ts @@ -0,0 +1,67 @@ +import { buildFromSource as build } from "../utils/build"; + +export class TestRunner { + private tests: Record; + private dryRun: boolean; + private ignoreFail: boolean; + private callback: ( + meta: { name: string; success: boolean; timeElapsed: number }, + ) => void; + + constructor(opts: { + dryRun?: boolean; + ignoreFail?: boolean; + collection: Record; + 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); + } + } + } +} diff --git a/utils/build.ts b/utils/build.ts new file mode 100644 index 0000000..d595357 --- /dev/null +++ b/utils/build.ts @@ -0,0 +1,19 @@ +import plugin from "../src/index"; + +export async function buildFromSource(entrypoint: string): Promise { + 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"); + } +}