Skip to content

Commit 79e0f40

Browse files
committed
Use Wasm file from mdn examples
1 parent 2aea674 commit 79e0f40

File tree

5 files changed

+23
-7
lines changed

5 files changed

+23
-7
lines changed

e2e/__tests__/__snapshots__/nativeEsm.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Time: <<REPLACED>>
88
Ran all test suites matching /native-esm-deep-cjs-reexport.test.js/i."
99
`;
1010
11-
exports[`runs Wasm test with native ESM 1`] = `
11+
exports[`runs WebAssembly (Wasm) test with native ESM 1`] = `
1212
"Test Suites: 1 passed, 1 total
1313
Tests: 4 passed, 4 total
1414
Snapshots: 0 total

e2e/__tests__/nativeEsm.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ onNodeVersions('>=16.9.0', () => {
6868
});
6969
});
7070

71-
test('runs Wasm test with native ESM', () => {
71+
test('runs WebAssembly (Wasm) test with native ESM', () => {
7272
const {exitCode, stderr, stdout} = runJest(DIR, ['native-esm-wasm.test.js'], {
7373
nodeOptions: '--experimental-vm-modules --no-warnings',
7474
});

e2e/native-esm/42.wasm

-181 Bytes
Binary file not shown.

e2e/native-esm/__tests__/native-esm-wasm.test.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,36 @@
88
// the point here is that it's the node core module
99
// eslint-disable-next-line no-restricted-imports
1010
import {readFileSync} from 'fs';
11-
// The file was generated by wasm-pack
12-
import {getAnswer} from '../42.wasm';
11+
// file origin: https://github.com/mdn/webassembly-examples/blob/2f2163287f86fe29deb162335bccca7d5d95ca4f/understanding-text-format/add.wasm
12+
// source code: https://github.com/mdn/webassembly-examples/blob/2f2163287f86fe29deb162335bccca7d5d95ca4f/understanding-text-format/add.was
13+
import {add} from '../add.wasm';
1314

14-
const wasmFileBuffer = readFileSync('42.wasm');
15+
const wasmFileBuffer = readFileSync('add.wasm');
1516

1617
test('supports native wasm imports', () => {
17-
expect(getAnswer()).toBe(42);
18+
expect(add(1, 2)).toBe(3);
19+
20+
// because arguments are i32 (signed), fractional part is truncated
21+
expect(add(0.99, 1.01)).toBe(1);
22+
23+
// because return value is i32 (signed), (2^31 - 1) + 1 overflows and becomes -2^31
24+
expect(add(Math.pow(2, 31) - 1, 1)).toBe(-Math.pow(2, 31));
25+
26+
// invalid or missing arguments are treated as 0
27+
expect(add('hello', 'world')).toBe(0);
28+
expect(add()).toBe(0);
29+
expect(add(null)).toBe(0);
30+
expect(add({}, [])).toBe(0);
31+
32+
// redundant arguments are silently ignored
33+
expect(add(1, 2, 3)).toBe(3);
1834
});
1935

2036
test('supports imports from "data:application/wasm" URI with base64 encoding', async () => {
2137
const importedWasmModule = await import(
2238
`data:application/wasm;base64,${wasmFileBuffer.toString('base64')}`
2339
);
24-
expect(importedWasmModule.getAnswer()).toBe(42);
40+
expect(importedWasmModule.add(0, 42)).toBe(42);
2541
});
2642

2743
test('imports from "data:text/wasm" URI without explicit encoding fail', async () => {

e2e/native-esm/add.wasm

41 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)