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

How to allow testing in both browser and Node.js? #2571

Open
RReverser opened this issue Jun 4, 2021 · 4 comments
Open

How to allow testing in both browser and Node.js? #2571

RReverser opened this issue Jun 4, 2021 · 4 comments
Labels

Comments

@RReverser
Copy link
Member

Summary

I have code that is only using standard JS APIs, and I want to ensure it works in both browser and Node.js environments.

If I use

wasm_bindgen_test_configure!(run_in_browser);

then running e.g. wasm-pack test --headless --chrome succeeds, but wasm-pack test --node fails with:

this test suite is only configured to run in a browser, but we're only testing node.js tests so skipping

If I comment out that wasm_bindgen_test_configure!(...) call, then instead wasm-pack test --node succeeds, but browser test command fails with:

This test suite is only configured to run in node.js, but we're only running
browser tests so skipping. If you'd like to run the tests in a browser
include this in your crate when testing:

    wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);

You'll likely want to put that in a `#[cfg(test)]` module or at the top of an
integration test.

Is there a way to allow either environment?

Additional Details

Looks like this behavior & flags were introduced in #1465 + rustwasm/wasm-pack#630, but I don't see a way to allow either environment.

@simlay
Copy link

simlay commented Jun 30, 2021

I think maybe you can do this with feature flags which was recently added to wasm-pack test in rustwasm/wasm-pack/pull/851.

@Saruniks
Copy link

Saruniks commented Jul 4, 2021

Adding to simlay comment:

Maybe as a somewhat of a workaround (from wasm-pack v0.10.0) could be done:

#[cfg(feature = "in_browser")]
wasm_bindgen_test_configure!(run_in_browser);

In Cargo.toml:

[features]
in_browser = []

And then

wasm-pack test --node
wasm-pack test --firefox --headless -- --features "in_browser"

@RReverser
Copy link
Member Author

Sure, something like that is possible, but I don't want to add feature and recompile tests just to switch testing. wasm-pack already has switch for Node.js vs browser (--node vs others), why can't it use that info to run same test Wasm in different environments if it's environment-agnostic?

@chinedufn
Copy link
Contributor

chinedufn commented Jul 4, 2021

There's no way to allow both environments to run right now as far as I know, but I don't think that there's a reason that this can't be done.

Right now the test runner looks at a custom section in the wasm binary to decide whether to run it using Node or the browser

let custom_section = wasm.customs.remove_raw("__wasm_bindgen_test_unstable");

/// A macro used to configured how this test is executed by the
/// `wasm-bindgen-test-runner` harness.
///
/// This macro is invoked as:
///
/// ```ignore
/// wasm_bindgen_test_configure!(foo bar baz);
/// ```
///
/// where all of `foo`, `bar`, and `baz`, would be recognized options to this
/// macro. The currently known options to this macro are:
///
/// * `run_in_browser` - requires that this test is run in a browser rather than
/// node.js, which is the default for executing tests.
///
/// This macro may be invoked at most one time per test suite (an entire binary
/// like `tests/foo.rs`, not per module)
#[macro_export]
macro_rules! wasm_bindgen_test_configure {

I think you could change things so that there is one custom section per target environment (node, browser, etc) then have the test runner look for each.

Then run all of the environments that are in the custom sections.

I haven't spent any time thinking about how to make that all into a good user experience and whatnot in terms of how errors are reported and whether or not tests run in parallel, etc..

But at a high level multiple target environments should be possible as far as I can tell.

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

4 participants