Jest: `jest-runtime` makes each module to have its own modules cache

Created on 23 Nov 2017  路  3Comments  路  Source: facebook/jest

Do you want to request a _feature_ or report a _bug_?

Bug

What is the current behavior?

Printing the content of require.cache in several files, they are shown as being isolated ones from others instead of being shared as Node.js require() function does. Jest-runtime has its own require function, and in fact this behaviour seems to be on purposse to prevent modules do side-effects, but in fact that side-effects is exactly what does the react-native-mock-render module, so it doesn't work at all.

What is the expected behavior?

require.cache being shared by all the modules as does Node.js implementation, so replacing entries on it affect the full Node.js instance.

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

  "jest": {
    "collectCoverageFrom": [
      "*.js",
      "config/**/*.js",
      "scripts/*.js",
      "src/**/*.js"
    ],
    "preset": "jest-react-native",
    "setupTestFrameworkScriptFile": "./src/setupTests.js",
    "snapshotSerializers": [
      "enzyme-to-json/serializer"
    ],
    "transformIgnorePatterns": [
      "node_modules/?!(react-router-native)"
    ]
  },

Jest: 21.2.1
Node.js: 9.2.0
npm: 5.5.1

Most helpful comment

Jest does not support require.cache. If you'd like to set a module, you can add it to setupFiles in a way that does jest.mock('module-name', () => { return moduleContents }, {virtual: true}).

All 3 comments

Jest does not support require.cache. If you'd like to set a module, you can add it to setupFiles in a way that does jest.mock('module-name', () => { return moduleContents }, {virtual: true}).

Jest does not support require.cache. If you'd like to set a module, you can add it to setupFiles in a way that does jest.mock('module-name', () => { return moduleContents }, {virtual: true}).

Thanks for the advice, I'll give it a try.

Confirmed, it works. Thank you! :-D

Was this page helpful?
0 / 5 - 0 ratings