I've got a POC project where I'm trying to test a function that depends on a webassembly module, but for some reason I don't understand it's not loading it correctly during the test.
The webassembly module(rust) is very simple. It exposes a single method that takes two numbers and adds them together. It's prepared by wasm-pack(v0.11) with --target bundler
and I've got a simple integration with this in my webpack config using wasm-pack-plugin so that this module is watched and built should the rust code change. In my toplevel project.json I've got this module as a filesystem-based dependency:
"rust_module": "file:src/rust_module/pkg"
In my main "production" code(typescript), I've got a simple import and a simple usage:
import * as r_m from 'rust_module'
export function addFiveSix(){
return r_m.add(5,6)
}
In my webpack(v5) config, I'm using the ecmascript module "experiment" and when I execute webpack bundle
, it executes without any error.
However, when I execute Jest(cross-env NODE_OPTIONS=--experimental-vm-modules jest
) to run a simple test of adddFiveSix
, it errors out:
spike_jest_wasm\src\rust_module\pkg\index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import * as wasm from "./index_bg.wasm";
^^^^^^
SyntaxError: Cannot use import statement outside a module
I'm sort of assuming that Jest does it's own in-memory rollup of all the linked code as it bootstraps itself(using babel and whatnot)...and that it isn't capable of what webpack is doing. Is this what's happening? Is there a way around this? Is there a way I can use the webpack bundle as the input of jest?
Fyi, I've got this project in a public bitbucket repo if this ends up being useful.
My problem was that I didn't tell Jest to transform the rust_module.
Adding
transformIgnorePatterns: ["/node_modules/(?!(rust_module))/"],
modulePaths: ['<rootDir>/node_modules/*']
where rust_module
is the name of the module solved my problem.