nshen/vite-plugin-wasm-pack

testing a wasm lib in vitest

Opened this issue · 0 comments

I have been struggling to test components that use my Rust wasm-pack generated code.
I narrowed down the problem to the invokation of wasm-pack's await init().
It looks like vitest freezes there and can't proceed so I can't finish my tests.

I have tried replacing it with initSync() and running the init fn from the setupTests.ts file so it initializes before the test suite runs but no luck.

Running the async init function imported from my-wasm-lib/pkg throws a ReferenceError: self is not defined.
I tried mocking the self namespace with

globalThis.self = globalThis;

in my setupTests file and then I'd get a TypeError: Invalid URL

I wonder if it's possible at all to run the WASM thread in Vitest or am I doing something wrong.
Using the regular vite server with the wasm lib works just fine in the browser enviroment.

My vite config:

  plugins: [svelte({ hot: !process.env.VITEST }), wasmPack('./wasm-lib')],
  server: {
    port: 5000,
    strictPort: true,
    open: false
  },
  test: {
    setupFiles: ['./setupTests.ts'],
    globals: true,
    include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
  }

Deps:

vite: 4.0.5,
vitest: 0.31.1,
vite-plugin-wasm-pack: 0.1.12

I build my wasm package using wasm-pack build ./my-wasm-lib --target web
Tests are also declared with @vitest-environment jsdom

Any ideas?