Skip to content

Commit

Permalink
test: add tests for wasm (#1951)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Nov 23, 2023
1 parent edb7a33 commit d139617
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/fixture/nitro.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export default defineNitroConfig({
experimental: {
openAPI: true,
asyncContext: true,
wasm: true,
},
cloudflare: {
pages: {
Expand Down
15 changes: 15 additions & 0 deletions test/fixture/routes/wasm/sum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export default defineLazyEventHandler(async () => {
// @ts-ignore
const { sum } = await import("~/wasm/sum.wasm")
.then((wasm) => wasm.default({}))
.then((r) => r.instance.exports);

// @ts-ignore
// const { sum } = await import("~/wasm/sum.wasm")
// .then((mod) => WebAssembly.instantiate(mod.default, {}))
// .then((instance) => instance.exports);

return eventHandler((event) => {
return `2+3=${sum(2, 3)}`;
});
});
9 changes: 9 additions & 0 deletions test/fixture/wasm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"private": true,
"scripts": {
"build:sum": "npx --yes --package=assemblyscript -c 'asc ./sum.asc.ts -o sum.wasm'",
"buld": "npm run build:sum",
"test:sum": "NODE_OPTIONS='--experimental-wasm-modules' node -e 'import(`./sum.wasm`).then(mod => console.log(mod.sum(2,3)))'",
"test": "npm run build:sum && npm run test:sum"
}
}
6 changes: 6 additions & 0 deletions test/fixture/wasm/sum.asc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @ts-nocheck
// https://www.assemblyscript.org

export function sum(a: i32, b: i32): i32 {
return a + b;
}
Binary file added test/fixture/wasm/sum.wasm
Binary file not shown.
10 changes: 10 additions & 0 deletions test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { expect, it, afterAll, beforeAll, describe } from "vitest";
import { fileURLToPath } from "mlly";
import { joinURL } from "ufo";
import { defu } from "defu";
import { isWindows } from "std-env";
import * as _nitro from "../src";
import type { Nitro } from "../src";

Expand Down Expand Up @@ -629,4 +630,13 @@ export function testNitro(
);
});
});

describe("wasm", () => {
it.skipIf(ctx.isWorker || ctx.preset === "deno-server" || isWindows)(
"sum works",
async () => {
expect((await callHandler({ url: "/wasm/sum" })).data).toBe("2+3=5");
}
);
});
}

0 comments on commit d139617

Please sign in to comment.