Jest: Mock modules are prioritised over node_modules

Created on 13 Oct 2016  ·  3Comments  ·  Source: facebook/jest

Do you want to request a _feature_ or report a _bug_?
Report a bug.

What is the current behavior?
If a project has a dependency on a module with the same name as an internal file with a mock, that mock implementation is retrieved for requests of the global module, instead of the global module, requiring explicit unmocking (which then prints a warning, despite displaying a tangible change in behaviour).

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal repository on GitHub that we can npm install and npm test.

  1. Clone https://github.com/ticky/jest-mock-bug, npm install dependencies
  2. Run npm test

The test expecting to be able to import the global module (with no explicit opt-out › globalPad should not be a mock function) fails because the module returned is in fact the manual mock.

(Note that left-pad is used here as it’s a small module with no compilation requirements.)

What is the expected behavior?

The import statement should return the global module, as it does in explicit-opt-out.js

Run Jest again with --debug and provide the full configuration it prints. Please mention your node and npm version and operating system.

jest version = 16.0.1
test framework = jasmine2
config = {
  "rootDir": "/Users/jessica/Repositories/jest-mock-bug",
  "name": "-Users-jessica-Repositories-jest-mock-bug",
  "setupFiles": [],
  "testRunner": "/Users/jessica/Repositories/jest-mock-bug/node_modules/jest-jasmine2/build/index.js",
  "scriptPreprocessor": "/Users/jessica/Repositories/jest-mock-bug/node_modules/babel-jest/build/index.js",
  "usesBabelJest": true,
  "automock": false,
  "bail": false,
  "browser": false,
  "cacheDirectory": "/var/folders/rl/98bml8qx57n3b6xgsgyvbxtr0000gn/T/jest",
  "clearMocks": false,
  "coveragePathIgnorePatterns": [
    "/node_modules/"
  ],
  "coverageReporters": [
    "json",
    "text",
    "lcov",
    "clover"
  ],
  "globals": {},
  "haste": {
    "providesModuleNodeModules": []
  },
  "mocksPattern": "__mocks__",
  "moduleDirectories": [
    "node_modules"
  ],
  "moduleFileExtensions": [
    "js",
    "json",
    "jsx",
    "node"
  ],
  "moduleNameMapper": {},
  "modulePathIgnorePatterns": [],
  "noStackTrace": false,
  "notify": false,
  "preset": null,
  "preprocessorIgnorePatterns": [
    "/node_modules/"
  ],
  "resetModules": false,
  "testEnvironment": "jest-environment-jsdom",
  "testPathDirs": [
    "/Users/jessica/Repositories/jest-mock-bug"
  ],
  "testPathIgnorePatterns": [
    "/node_modules/"
  ],
  "testRegex": "(/__tests__/.*|\\.(test|spec))\\.jsx?$",
  "testURL": "about:blank",
  "timers": "real",
  "useStderr": false,
  "verbose": null,
  "watch": false,
  "cache": true,
  "watchman": true,
  "testcheckOptions": {
    "times": 100,
    "maxSize": 200
  }
}

Most helpful comment

Yes, manual mocks take precedence over node modules. The manual mocks system is a bit sketchy. For now, we are removing the warning in the next release, so jest.unmock('left-pad') should be all you need. See #2022

One idea we had was to make a separate system for manual node module mocks but that should likely be discussed in a separate proposal, so I'll close this issue out.

All 3 comments

I think this case is similar to what happen in my project

In my case I have <rootDir>/__mocks__/left-pad.js. All tests that import left-pad will automatically import the mocked left-pad even though I didn't explicitly call jest.mock('left-pad')

My current workaround is explicitly mock and then explicitly unmock them. It's ugly, but it works

jest.mock('left-pad');
jest.unmock('left-pad');

Aha, thanks! That’s a cleaner workaround than mine, though still definitely not ideal!

Yes, manual mocks take precedence over node modules. The manual mocks system is a bit sketchy. For now, we are removing the warning in the next release, so jest.unmock('left-pad') should be all you need. See #2022

One idea we had was to make a separate system for manual node module mocks but that should likely be discussed in a separate proposal, so I'll close this issue out.

Was this page helpful?
0 / 5 - 0 ratings