jest fails to resolve modules in a "setupFile" when using yarn pnp
Error output:
FAIL tests/js/spec/components/button.spec.jsx
● Test suite failed to run
Cannot find module 'app/utils/recreateRoute' from 'setup.js'
9 | import ConfigStore from 'app/stores/configStore';
10 | import theme from 'app/utils/theme';
> 11 |
| ^
12 | import {loadFixtures} from './helpers/loadFixtures';
13 |
14 | export * from './helpers/select';
at Resolver.resolveModule (.pnp/externals/pnp-237e04d5dff81c9816cd397d2a53c7492fa83ce4/node_modules/jest-resolve/build/index.js:202:17)
at Object.<anonymous> (tests/js/setup.js:11:6)
Relevant files:
app/utils/theme is within this modulePath: https://github.com/getsentry/sentry/blob/build/travis/yarn-pnp/jest.config.js#L19Steps to reproduce the behavior:
git clone https://github.com/getsentry/sentry/tree/build/travis/yarn-pnpyarn installyarn testTests should run
Sorry this isn't minimal, but you can look at https://github.com/getsentry/sentry/tree/build/travis/yarn-pnp
npx envinfo --preset jestPaste the results here:
System:
OS: macOS 10.14.3
CPU: (4) x64 Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
Binaries:
Node: 8.10.0 - ~/.notion/tools/image/node/8.10.0/5.6.0/bin/node
Yarn: 1.13.0 - ~/.notion/tools/image/yarn/1.13.0/bin/yarn
npm: 5.6.0 - ~/.notion/tools/image/node/8.10.0/5.6.0/bin/npm
@arcanis could you take a look?
I think the problem is with modulePaths. Since PnP controls by design the entire dependency tree based on the information obtained at install-time, you can't add new resolution directories at runtime (similarly, NODE_PATH isn't supported).
That said, in this case the fix is rather simple: instead of using modulePaths, just use a link: dependency in your package.json. This way PnP knows at install-time about this virtual mapping.
Example:
{
"dependencies": {
"app": "link:./src/sentry/static/sentry"
}
}
Can confirm that adding the path as a dependency fixes this.
Could jest warn in this case?
Ideally Jest should just forward the PnP errors. In this case it would have printed the following:
https://github.com/yarnpkg/berry/blob/master/packages/berry-pnp/sources/loader/makeApi.ts#L458
I think Jest catches all exceptions happening within the resolver and replaces them with a blanket "Module not found" error 🤔
That seems likely, yeah! Should probably fix that
@arcanis @SimenB
Sorry to revive this but I'm having a similar issue after upgrading to PnP. Previously I was defining:
"modulePaths": [
"client/scripts"
],
In my jest.config.js, which was working prior to upgrading to PnP. But after the upgrade, all of my local relative path files I'm referencing are failing to be reached. I tried adding something like recommended above to my package.json (i.e. "client": "link:./client",) but the error is the same.
Here's an example of an error:
FAIL client/__tests__/data/profile/reducers/index.spec.js
● Test suite failed to run
Cannot find module 'data/profile/reducers' from 'index.spec.js'
> 1 | import reducer from 'data/profile/reducers';
| ^
2 | import {
3 | PROFILE_UPDATE_REQUEST,
4 | PROFILE_UPDATE_SUCCESS,
at Resolver.resolveModule (../../Library/Caches/Yarn/v6/npm-jest-resolve-24.9.0-dff04c7687af34c4dd7e524892d9cf77e5d17321-integrity/node_modules/jest-resolve/build/index.js:259:17)
at Object.<anonymous> (client/__tests__/data/profile/reducers/index.spec.js:1:1)
I think modulePaths should be respected even with PnP. Webpack can combine resolve.modules and PnP, so why should not Jest be able to?
Adding paths as dependencies is NOT a solution here, I have 18 directories in src/, I'm not going to sprinkle them into the dependencies array.