Testing WebAssembly with Jest/jsdom
If you have WebAssembly working in your frontend app, your next step might be adding a unit test for it. However, if you’re using jsdom (possibly with Jest), you’ll likely get an error like “both async and sync fetching of the wasm failed”.
The error is caused by the jsdom environment trying to fetch the .wasm file while running the test. By default jsdom doesn’t provide the global Fetch API, which means it needs to be mocked. This can be achieved by installing “jest-fetch-mock”:
The mock should respond with the .wasm binary file when the request is made to it. The simplest way is to enable it for all tests in a setup file, e.g. setupJest.js:
You can read jest-fetch-mock documentation for more granular control of the mock.
Now your tests should work correctly with WebAssembly 🚀