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:
- Created 4 years ago
- Reactions:26
- Comments:25
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
A little late here, but I was just having this exact issue. I discovered that someone had added
resetMocks: true
to thejest.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 returnedundefined
.Regarding the original issue build environment, it looks like
react-scripts
does indeed addresetMocks: 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 thejest
key of yourpackage.json
. (https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/scripts/utils/createJestConfig.js#L74)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 thetest
/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