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

Can't be imported under Jest #64

Closed
adamnovak opened this issue Jan 10, 2024 · 3 comments · Fixed by #65
Closed

Can't be imported under Jest #64

adamnovak opened this issue Jan 10, 2024 · 3 comments · Fixed by #65

Comments

@adamnovak
Copy link

adamnovak commented Jan 10, 2024

I'm trying to use this module in a create-react-app project, and I'm trying to test my project with the built-in Jest test system.

Jest doesn't run Node in ESM mode; it can handle ESM code with import statements, but behind the scenes it does transpilation to run on require().

To use Jest with dependencies that are ESM modules, you have to add them to a negative regular expression in jest.transformIgnorePatterns in your package.json, so that Jest knows it needs to transpile them:

  "jest": {
    "transformIgnorePatterns": [
      "node_modules/(?!(@bjorn3/browser_wasi_shim)/)"
    ]
  }

But even though Jest can transform this module just fine, it can't deal with the conditional export in this project's package.json, which only specifies a file to expose in "import" mode:

"types": "./typings/index.d.ts",
"import": "./dist/index.js"

I get a Jest error like this when I try to run my tests:


> sequence-tube-maps@0.1.0 test
> react-scripts test --reporters default jest-compact-reporter --seed=1



Jest v27.5.1 node v18.7.0 darwin jest-min-reporter v0.1.0
Browserslist: caniuse-lite is outdated. Please run:
  npx update-browserslist-db@latest
  Why you should do it regularly: https://github.com/browserslist/update-db#readme
 FAIL  src/GBZBaseAPI.test.js
  ● Test suite failed to run

    Cannot find module '@bjorn3/browser_wasi_shim' from 'src/GBZBaseAPI.mjs'

    Require stack:
      src/GBZBaseAPI.mjs
      src/GBZBaseAPI.test.js

      1 | import { APIInterface } from "./APIInterface.mjs";
      2 | import { readFile } from "fs-extra";
    > 3 | import { WASI, File, OpenFile, PreopenDirectory } from "@bjorn3/browser_wasi_shim";
        | ^
      4 |
      5 | // TODO: The Webpack way to get the WASM would be something like:
      6 | //import QueryWasm from "gbz-base/target/wasm32-wasi/release/query.wasm";

      at Resolver.resolveModule (node_modules/jest-resolve/build/resolver.js:324:11)
      at Object.<anonymous> (src/GBZBaseAPI.mjs:3:1)

If I add a "default": "./dist/index.js" export to this project's package.json in my node_modules, Jest can find it and import and transpile it.

There might be a way to give Jest a custom module resolver to work around this, but I think the configuration fields required are not among those that Facebook permits you to use in a create-react-app project.

It would be good if this project exposed an export like "default" that would work in this situation.

@bjorn3
Copy link
Owner

bjorn3 commented Jan 10, 2024

I'm not familiar with how the node.js rules to determine which file to load for a module and whether to do so as ESM or CJS code. Would replacing the "import" with "default" break anything?

@adamnovak
Copy link
Author

I think it would allow require() to try and import the module and fail if it can't be parsed. Right now require() shouldn't be able to see the module at all.

@bjorn3
Copy link
Owner

bjorn3 commented Jan 10, 2024

I went with the more conservative option of only adding the default key rather than replacing the module key with it in #65. Could you check if it works for you?

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

Successfully merging a pull request may close this issue.

2 participants