|
| 1 | +import { expect, test } from '@rstest/core'; |
| 2 | +import { callExp, getImpCalls } from './src/glue.js'; |
| 3 | + |
| 4 | +// Import-bearing wasm (the wasm declares `(import "<module>" "imp" ...)`): |
| 5 | +// - mod.wasm imports a RESOLVABLE JS module ('./glue.js') — the wasm-bindgen |
| 6 | +// `--target bundler` shape, including the circular glue<->wasm dependency. |
| 7 | +// wasmLoader.mjs links the glue statically and instantiates with an |
| 8 | +// importObject, so a plain `import` works transparently. |
| 9 | +// - envmod.wasm imports the synthetic, non-resolvable 'env' module: the build |
| 10 | +// must still succeed (no `import 'env'` emitted) and only fail at runtime. |
| 11 | + |
| 12 | +test('resolvable glue import: circular wasm-bindgen-bundler shape links transparently', () => { |
| 13 | + expect(callExp()).toBe(42); // glue.callExp -> wasm.exp -> imports ./glue.js imp() -> 42 |
| 14 | +}); |
| 15 | + |
| 16 | +test('direct import of an import-bearing wasm resolves its importObject', async () => { |
| 17 | + const ns = await import('./src/mod.wasm'); |
| 18 | + expect(ns.exp()).toBe(42); |
| 19 | +}); |
| 20 | + |
| 21 | +test('non-resolvable env import builds, and only LinkErrors at runtime', async () => { |
| 22 | + await expect(import('./src/envmod.wasm')).rejects.toThrow(); |
| 23 | +}); |
| 24 | + |
| 25 | +// The wasm loader wires an import-module by actually resolving it, not by |
| 26 | +// checking for a `./ ../ /` prefix. aliasmod.wasm imports `@e2e/wasm-glue` |
| 27 | +// (a non-relative specifier mapped via resolve.alias); a prefix-only heuristic |
| 28 | +// would drop it and the wasm would LinkError. Actual resolution wires the glue. |
| 29 | +test('alias import module: non-relative specifier resolves its glue via resolve.alias', async () => { |
| 30 | + const ns = await import('./src/aliasmod.wasm'); |
| 31 | + expect(ns.exp()).toBe(42); // wasm.exp -> imports @e2e/wasm-glue imp() -> 42 |
| 32 | +}); |
| 33 | + |
| 34 | +// Regression guard for the load-bearing property that distinguishes the |
| 35 | +// build-time wasmLoader from any native auto-link approach: the glue the test |
| 36 | +// imports and the glue the wasm links against must be the SAME instance. The |
| 37 | +// loader keeps glue in rspack's module graph (one shared instance), so a state |
| 38 | +// mutation the wasm triggers through `imp()` is observable here. A native |
| 39 | +// `import('x.wasm')` would link the wasm against a separate Node-loaded glue |
| 40 | +// copy, leaving `getImpCalls()` at its prior value (split-brain). |
| 41 | +test('import-bearing wasm links the same glue instance the test holds (no split-brain)', () => { |
| 42 | + const before = getImpCalls(); |
| 43 | + callExp(); // -> wasm.exp() -> calls glue.imp() exactly once |
| 44 | + expect(getImpCalls()).toBe(before + 1); |
| 45 | +}); |
0 commit comments