Skip to content

Commit 11a9a8e

Browse files
committed
test: add unit tests for index exports
1 parent 51ed3d1 commit 11a9a8e

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

lib/index.test.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
2+
3+
// Mock the wasm_exec module
4+
vi.mock('../../lib/wasm_exec', () => ({}));
5+
6+
// Mock @lightninglabs/lnc-core to avoid actual imports
7+
vi.mock('@lightninglabs/lnc-core');
8+
9+
describe('Index Module', () => {
10+
let originalInstantiateStreaming: any;
11+
12+
beforeEach(() => {
13+
// Store original values
14+
originalInstantiateStreaming =
15+
globalThis.WebAssembly?.instantiateStreaming;
16+
17+
// Mock WebAssembly for testing
18+
globalThis.WebAssembly = {
19+
instantiateStreaming: vi.fn(),
20+
instantiate: vi.fn(),
21+
compile: vi.fn()
22+
} as any;
23+
});
24+
25+
afterEach(() => {
26+
// Restore original values
27+
if (originalInstantiateStreaming) {
28+
globalThis.WebAssembly.instantiateStreaming =
29+
originalInstantiateStreaming;
30+
}
31+
vi.restoreAllMocks();
32+
});
33+
34+
describe('WebAssembly Polyfill', () => {
35+
it('should polyfill WebAssembly.instantiateStreaming when not available', async () => {
36+
// Remove instantiateStreaming to test polyfill
37+
delete (globalThis.WebAssembly as any).instantiateStreaming;
38+
39+
// Import the index module to trigger the polyfill
40+
await import('./index');
41+
42+
// Now WebAssembly.instantiateStreaming should exist
43+
expect(typeof globalThis.WebAssembly?.instantiateStreaming).toBe(
44+
'function'
45+
);
46+
});
47+
48+
it('should use existing WebAssembly.instantiateStreaming when available', async () => {
49+
// Set up existing WebAssembly.instantiateStreaming
50+
const existingInstantiateStreaming = vi.fn().mockResolvedValue({
51+
module: {},
52+
instance: {}
53+
});
54+
55+
globalThis.WebAssembly = {
56+
...globalThis.WebAssembly,
57+
instantiateStreaming: existingInstantiateStreaming
58+
};
59+
60+
// Import the index module
61+
await import('./index');
62+
63+
// The existing function should still be there
64+
expect(globalThis.WebAssembly.instantiateStreaming).toBe(
65+
existingInstantiateStreaming
66+
);
67+
});
68+
});
69+
});

0 commit comments

Comments
 (0)