mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-24 13:52:45 +00:00
42 lines
1.4 KiB
TypeScript
42 lines
1.4 KiB
TypeScript
import { readFileSync } from "fs";
|
|
import { join } from "path";
|
|
|
|
// Mock the fetch API for WASM loading in Jest
|
|
global.fetch = jest.fn((url: RequestInfo | URL) => {
|
|
const urlString = typeof url === "string" ? url : url.toString();
|
|
if (urlString.endsWith("pathfinding_bg.wasm")) {
|
|
const wasmBuffer = readFileSync(
|
|
join(__dirname, "../static/js/pathfinding_bg.wasm"),
|
|
);
|
|
return Promise.resolve({
|
|
arrayBuffer: () => Promise.resolve(wasmBuffer.buffer),
|
|
headers: new Headers(),
|
|
ok: true,
|
|
redirected: false,
|
|
status: 200,
|
|
statusText: "OK",
|
|
type: "basic",
|
|
url: urlString,
|
|
clone: () => global.fetch(url),
|
|
} as Response);
|
|
}
|
|
return Promise.reject(new Error(`Unhandled fetch request for: ${urlString}`));
|
|
});
|
|
|
|
// Mock WebAssembly.instantiateStreaming for Node.js environment
|
|
// This is needed because wasm-bindgen generated code uses instantiateStreaming
|
|
// but Jest runs in Node.js where it's not available.
|
|
global.WebAssembly.instantiateStreaming = jest.fn(
|
|
async (
|
|
source: Response | PromiseLike<Response> | WebAssembly.Module,
|
|
importObject: WebAssembly.Imports | undefined,
|
|
) => {
|
|
if (source instanceof WebAssembly.Module) {
|
|
return WebAssembly.instantiate(source, importObject);
|
|
}
|
|
const response = await source;
|
|
const buffer = await response.arrayBuffer();
|
|
return WebAssembly.instantiate(buffer, importObject);
|
|
},
|
|
);
|