Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testing a wasm lib in vitest #28

Open
bartektelec opened this issue Jul 18, 2023 · 0 comments
Open

testing a wasm lib in vitest #28

bartektelec opened this issue Jul 18, 2023 · 0 comments

Comments

@bartektelec
Copy link

bartektelec commented Jul 18, 2023

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant