question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Jest mock/spy returns undefined even when set up

See original GitHub issue

🐛 Bug Report

Mocking spy return does nothing

I’m about to lose my mind here; it seems the entire mock system is b0rked. I copy-paste the examples from the docs, and they don’t work.

To Reproduce

// __mocks__/someUtil.js
export default {
  m1: jest.fn(() => 42),
}
// someUtil.test.js
import someUtil from './someUtil';

jest.mock('./someUtil')

console.log(someUtil.m1) // expected spy stuff
console.log(someUtil.m1()) // undefined 🤯
Other variations that also do nothing
// __mocks__/someUtil.js

throw; // does throw

export default {
  m1: jest.fn(() => throw), // does NOT throw
}

// ---

export default {
  m1: jest.fn().mockImplementation(() => 42),
}

// ---

export default {
  m1: jest.fn().mockImplementation(() => throw), // does NOT throw
}

// ---

export default {
  m1: jest.fn().mockReturnValue(42),
}

// ---

export default () => ({
  m1: jest.fn().mockImplementation(() => 42),
})

// ---

export default () => ({
  m1: jest.fn().mockReturnValue(42),
})

Expected behavior

As documented: it should return 42

envinfo

jest version: 24.7.1

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:26
  • Comments:25

github_iconTop GitHub Comments

215reactions
dstapleton92commented, Aug 4, 2020

A little late here, but I was just having this exact issue. I discovered that someone had added resetMocks: true to the jest.config.js file. This means that the implementations of mock functions are reset before each test. So in our case, the mock function was being included in the mocked module at test runtime, but that mock had been reset, so it returned undefined.

Regarding the original issue build environment, it looks like react-scripts does indeed add resetMocks: true into the jest config. (https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/scripts/utils/createJestConfig.js#L69) But you can override it on the jest key of your package.json. (https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/scripts/utils/createJestConfig.js#L74)

23reactions
HenryCharlesAndersoncommented, Aug 26, 2020

resetMocks: true was the culprite for me too.

mock implementations can work with resetMocks: true if you setup the mocks in beforeEach, or directly inside the test/it callbacks.

If you set up the mocks at the top level of the module, in the describe callback (but outside of any it/test callback), or in beforeAll, they get overwritten by resetMocks AFAICT

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest Mock returns undefined instead of data - Stack Overflow
In your case data is undefined, because you haven't actually supplied a mocked implementation for the function or the mock hasn't worked and...
Read more >
Mock Functions - Jest
You can create a mock function with jest.fn(). If no implementation is given, the mock function will return undefined when invoked.
Read more >
Mock Functions · Jest
You can create a mock function with jest.fn() . If no implementation is given, the mock function will return undefined when invoked.
Read more >
jest cannot spy the property because it is not a function
It looks like you're mocking/spying getNextPos inside of the solution object, but in fact, solution is not an object, it's a function. For...
Read more >
Jest Object (API Reference) - w3resource
It will create a new mock function. The new function will have no formal parameters and when called returns undefined. This functionality will ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found