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

Tests cannot run when using bindings to NPM dependencies #2477

Open
jpgneves opened this issue Mar 2, 2021 · 3 comments
Open

Tests cannot run when using bindings to NPM dependencies #2477

jpgneves opened this issue Mar 2, 2021 · 3 comments
Labels

Comments

@jpgneves
Copy link

jpgneves commented Mar 2, 2021

Describe the Bug

Given a crate A with a package.json next to Cargo.toml containing NPM package dependencies, running cargo test --target wasm32-unknown-unknown (or wasm-pack test) will fail as the required dependencies are not present to be loaded.

It feels like wasm-bindgen needs to invoke npm install to install the dependencies in the temporary test directory to install the dependencies prior to running the tests?

Steps to Reproduce

  1. Set up a crate which creates bindings to a NPM module via #[wasm_bindgen(module = ...)]
  2. Add tests using #[wasm_bindgen_test] which make use of the bindings
  3. Run cargo test --target wasm32-unknown-unknown or wasm-pack test

Minimal example using bindings to React: https://github.com/jpgneves/wbg-test

Expected Behavior

Tests run successfully.

Actual Behavior

The following error is given (for the example linked above):

     Running target/wasm32-unknown-unknown/debug/deps/node-f2b1b07656587b53.wasm
Set timeout to 20 seconds...
internal/modules/cjs/loader.js:818                
  throw err;
  ^

Error: Cannot find module 'react'
Require stack:
- /home/joao/proj/wbg-test/target/wasm32-unknown-unknown/wbg-tmp/wasm-bindgen-test.js
- /home/joao/proj/wbg-test/target/wasm32-unknown-unknown/wbg-tmp/run.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:815:15)
    at Function.Module._load (internal/modules/cjs/loader.js:667:27)
    at Module.require (internal/modules/cjs/loader.js:887:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (/home/joao/proj/wbg-test/target/wasm32-unknown-unknown/wbg-tmp/wasm-bindgen-test.js:4:23)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Module.require (internal/modules/cjs/loader.js:887:19) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/home/joao/proj/wbg-test/target/wasm32-unknown-unknown/wbg-tmp/wasm-bindgen-test.js',
    '/home/joao/proj/wbg-test/target/wasm32-unknown-unknown/wbg-tmp/run.js'
  ]
}
error: test failed, to rerun pass '--test node'
Error: Running Wasm tests with wasm-bindgen-test failed
Caused by: failed to execute `cargo test`: exited with exit code: 1
  full command: "cargo" "test" "--target" "wasm32-unknown-unknown"

The contents of target/wasm32-unknown-unknown/wbg-tmp:

package.json
run.js
wasm-bindgen-test_bg.wasm 
wasm-bindgen-test.js

Where package.json contains the declared dependencies:

{
  "react": "^17.0.1"
}
@jpgneves jpgneves added the bug label Mar 2, 2021
@simlay
Copy link

simlay commented Jun 30, 2021

rustwasm/wasm-pack/pull/986 implements part of this for wasm-pack build but I don't think that applies to wasm-pack test.

@jpgneves
Copy link
Author

jpgneves commented Jul 6, 2021

It's been a while since I last looked at this, but the biggest missing piece IIRC was the fact that the dependencies are not actually installed in some way, so they cannot be loaded when running the tests either.

I'm not sure what's the best approach here, as requiring an npm install of the dependencies then adds an implicit dependency of on npm on the runner.

@jpgneves
Copy link
Author

jpgneves commented Aug 10, 2021

So I delved deeper into this and, as far as I can tell, there's at least two things which need to happen for tests to work:

  • wasm-bindgen-test-runner probably should npm install the package under test to the temporary directory (so all its dependencies are available and importable during the tests)
  • The HTTP server currently serving the test JS and HTTP files needs to implement some additional module resolution logic, probably similar to what Webpack does

I am currently hacking away at these two points to get to a point where things work. If you have any feedback on this, I'd be happy to hear it! cc @simlay

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

No branches or pull requests

2 participants