Skip to content

Commit

Permalink
test: fix wasm mocker
Browse files Browse the repository at this point in the history
Signed-off-by: Zxilly <[email protected]>
  • Loading branch information
Zxilly committed Sep 16, 2024
1 parent a07a517 commit 07222f6
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 9 deletions.
3 changes: 2 additions & 1 deletion ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@
"vite": "^5.4.5",
"vite-plugin-html": "^3.2.2",
"vite-plugin-singlefile": "^2.0.2",
"vitest": "^2.1.1"
"vitest": "^2.1.1",
"vitest-fetch-mock": "^0.3.0"
},
"pnpm": {
"peerDependencyRules": {
Expand Down
56 changes: 56 additions & 0 deletions ui/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 10 additions & 8 deletions ui/src/worker/helper.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
// @vitest-environment node

import { readFile } from "node:fs/promises";

import path from "node:path";
import { describe, expect, it, vi } from "vitest";
import createFetchMock from "vitest-fetch-mock";
import { GsaInstance } from "./helper.ts";
import "@vitest/web-worker";
import "../runtime/wasm_exec.js";

vi.mock("../../gsa.wasm?init", async () => {
const fetchMocker = createFetchMock(vi);
fetchMocker.enableMocks();

// @ts-expect-error the mocker got the wrong type
fetchMocker.mockResponse(async () => {
const buffer = await readFile(path.join(__dirname, "../../gsa.wasm"));
// create blob
return {
default: async (i: WebAssembly.Imports) => {
return (await WebAssembly.instantiate(buffer, i)).instance;
},
};
return new Response(buffer, {
status: 200,
headers: { "Content-Type": "application/octet-stream" },
});
});

describe("worker helper", () => {
Expand Down

0 comments on commit 07222f6

Please sign in to comment.