jestjs/jest

Mocked module return undefined

armandabric opened this issue · 11 comments

Do you want to request a feature or report a bug?
Bug

What is the current behavior?
This do not works:

jest.mock('./doBar', () => {
    return jest.fn().mockReturnValue(42);
});

When using doBar() in another component it return undefined.

You could find a full example here: https://gist.github.com/Spy-Seth/abd9e98110d9ddc437fe99df0e86ff0b

This break with jest v17.0.3 but was working with jest v16.0.1

What is the expected behavior?
doBar() should return 42.

Environment:

jest version = 17.0.3
test framework = jasmine2
config = {
  "cacheDirectory": "/app/var/jest",
  "moduleFileExtensions": [
    "js",
    "jsx"
  ],
  "testPathDirs": [
    "/app/src",
    "/app/tests/units",
    "/app/tests/functionals"
  ],
  "testRegex": "((.*)\\.(test|spec)\\.(js|jsx)$)|((.*)Test\\.(js|jsx)$)",
  "resetMocks": true,
  "rootDir": "/app",
  "name": "-app",
  "setupFiles": [
    "/app/node_modules/babel-polyfill/lib/index.js"
  ],
  "testRunner": "/app/node_modules/jest-jasmine2/build/index.js",
  "transform": [
    [
      "^.+\\.jsx?$",
      "/app/node_modules/babel-jest/build/index.js"
    ]
  ],
  "usesBabelJest": true,
  "automock": false,
  "bail": false,
  "browser": false,
  "coveragePathIgnorePatterns": [
    "/node_modules/"
  ],
  "coverageReporters": [
    "json",
    "text",
    "lcov",
    "clover"
  ],
  "expand": false,
  "globals": {},
  "haste": {
    "providesModuleNodeModules": []
  },
  "mocksPattern": "__mocks__",
  "moduleDirectories": [
    "node_modules"
  ],
  "moduleNameMapper": {},
  "modulePathIgnorePatterns": [],
  "noStackTrace": false,
  "notify": false,
  "preset": null,
  "resetModules": false,
  "snapshotSerializers": [],
  "testEnvironment": "jest-environment-jsdom",
  "testPathIgnorePatterns": [
    "/node_modules/"
  ],
  "testURL": "about:blank",
  "timers": "real",
  "transformIgnorePatterns": [
    "/node_modules/"
  ],
  "useStderr": false,
  "verbose": null,
  "watch": false,
  "cache": true,
  "watchman": true
}

Node: v6.9.0

Can you double check if this works with 17.0.0 or not?

Same result with jest 17.0.0 😢

same result here even the code examples

jest.mock('../moduleName', () => {
  return jest.fn(() => 42);
});

const moduleName = require('../moduleName'); // This runs the function specified as second argument to `jest.mock`.
moduleName(); // Will return '42';

doesn't work anymore, my test using this technique in 16.0.2 worked fine.

Ping @cpojer

ping @Spy-Seth is it fixed by v18.0.0?

@Spy-Seth I've tested the case from attached gist on v17 and v18, test pass.
@acateland your example is valid too.

Closing this, but happy to reopen if you can provide a repo with failing test (can be something with configuration).

Adding the "resetMocks": true configuration allow me reproduce the buggy behavior in my application.

Yeah, so resetMocks and jest.resetAllMocks() works this way now, that it wipes all mocks data. Here's a relevant issue to bring back jest.clearAllMocks() back to Jest: #2134.

Hey, just wanted to add super late but I'm also running into this issue. Both require and jest.requireActual return undefined and cannot seem to diagnose it from require.cache either. I wish I had a repo to share but I also tried resetting all the mocks too. The only way to see the modules (react, prop-types) is to use jest.unmock(...)

  • Jest v24 via Gulp
  • Disabling hoisting does not seem to have any effect

jest.requireActual sometimes returns undefined. Any idea why?

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.