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

Jest cache incorrectly resolving modules #4092

Closed
imjared opened this issue Jul 20, 2017 · 6 comments
Closed

Jest cache incorrectly resolving modules #4092

imjared opened this issue Jul 20, 2017 · 6 comments

Comments

@imjared
Copy link

imjared commented Jul 20, 2017

Before creating an issue please check the following:

  • you are using the latest version of Jest
    "jest": "^20.0.4",
  • try re-installing your node_modules folder
    ✅ so.many.times.
  • run Jest once with --no-cache to see if that fixes the problem you are experiencing.
    ✅ tried it.

Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.

"jest": {
  "verbose": true,
  "resetModules": true
}
  • jest version: 20.0.4 21.0.1
  • node version: 7.4.0 8.1.3
  • yarn version: 0.27.5 1.3.2
  • npm version: 4.0.5 5.6.0
  • OS = MacOS Sierra 10.12.5 (16F73) MacOS High Sierra 10.13.2

Repo with reproduction steps here:

https://github.com/imjared/jest-resolver-failure-repro

Description

We've been having issues with (what seems to be) Jest's module resolver and how it interacts with Jest's cache.

Our .babelrc uses the module-resolver plugin so we can use paths relative to our src directory (effectively the root of our project) rather than from each file. For example, import Bar from '../../components/foo/bar' might end up being import Bar from 'components/foo/bar'.

This test case is reduced to show our i18n helper that lives in src/utils/i18n.js. We use this in our translation JSON files to provide fallbacks for translations when a locale may be missing or a locale like Germany might provide translations for Austria and Switzerland.

When we run tests, our i18n helper runs as expected as long as it exists at what would be the same relative path. For example, in this repo, the path from Component1/translations/index.js to the i18n module would be:

import { i18n } from '../../../utils/i18n.js';

When we test a component that has a different relative path, the i18n module can not be found. For example, the path from Component2/NestedComponent/translations/index.js to the i18n module would be one level deeper:

import { i18n } from '../../../../utils/i18n.js';

This causes tests to fail.

Reproduction

  1. Clone this repo
  2. run yarn to install dependencies. Ensure you have jest-cli installed
  3. after running yarn, kick off the tests by running jest or yarn test. The first test run primes the Jest cache so tests should pass fine. Run tests again.

Expected Behavior:
Tests still pass. Paths resolve as they should.

Actual Behavior:
Tests fail. The error is as follows:

Cannot find module '../../../../utils/i18n' from 'index.js'
      
      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:179:17)
      at Object.<anonymous> (src/components/Component1/translations/index.js:1:229)

It seems that the module can't be found because the path that's four directories up is actually the path used from the NestedComponent's translation index file.

Running the tests again with either jest --no-cache or yarn test:nocache should pass.

@stewartyu
Copy link

stewartyu commented Jul 20, 2017

I'm having the same problem too!

@imjared
Copy link
Author

imjared commented Aug 1, 2017

I've been tinkering with this a bit more and found some additional behavior that might be helpful. If translations/index.js is the same file (has the same content) at various depths, we'll have the issue. If the file differs, Jest seems to cache it separately. For example:

In my demo repo, in Component2/NestedComponent/translations, add a file called en-CA.json with the following contents:

{
  "content": "Here is a nested component's Canadian content"
}

Component2/NestedComponent/translations/index.js, add the line 'en-CA': require('./en-CA.json'), to the default export.

Clear the jest cache. I did this by deleting a directory at /private/var/folders/__/_bx_5btj1p33ywck5vj1w5hw0000gn/T/jest_dx/jest-transform-cache-26ea998c02504a11f13e60853d300d2f-1a35dc2915d13d52ffe0e46ba61699d5/. If you're debugging along with me, I bet you know how to find your cache dir 😉 [1]

Run Jest once to prime the cache. Run Jest again and see that the tests still pass. If you investigate the cache directory that's been recreated, you'll see there are two instances of files called index_.... One should have the en-US and en-CA imports, one with only the en-US import.


[1] If anyone is reading along, you can find your cache directory by running jest --showConfig. It's the config.cacheDirectory value.

@mmahalwy
Copy link

same problem. any ideas?

@wschurman
Copy link
Contributor

@mmahalwy - may want to check that your NODE_PATH environment variable doesn't contain any non-existent directories (see #4453 (comment))

@imjared
Copy link
Author

imjared commented Feb 14, 2018

this seems to finally be resolved with "jest": "^22.3.0",

@imjared imjared closed this as completed Feb 14, 2018
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants