mocks don't seem to be registered correctly
See original GitHub issue🐛 Bug Report
When setting up a mockResolvedValue
, a mockImplementation
, etc. for an async function (such as data fetching) inside beforeEach
or a test the mock works as expected.
However when the mock is set up in beforeAll
the defined return value of the mock is not returned and an error is thrown. The same error is thrown when setting up the mock at the top of the module.
Since we’re setting up only one test the semantics of beforeEach
and beforeAll
should be the same.
Error message:
Error: Uncaught [TypeError: Cannot read property ‘then’ of undefined]
To Reproduce
See test in this repo
Expected behavior
When setting up the mock
- at the top of the module, the mock implementation / resolved value should still work
- in the
beforeAll
hook the mock should work the same way as thebeforeEach
(at least in our context, because we only have one test in this case)
Link to repl or repo (highly encouraged)
https://github.com/lehnerchristian/jest-test
envinfo
npx: installed 1 in 0.795s
System:
OS: Linux 5.8 Ubuntu 20.04.2 LTS (Focal Fossa)
CPU: (16) x64 Intel(R) Xeon(R) W-10885M CPU @ 2.40GHz
Binaries:
Node: 16.0.0 - ~/.nvm/versions/node/v16.0.0/bin/node
Yarn: 1.22.10 - ~/.nvm/versions/node/v16.0.0/bin/yarn
npm: 6.14.13 - ~/.nvm/versions/node/v16.0.0/bin/npm
Issue Analytics
- State:
- Created 2 years ago
- Reactions:3
- Comments:5
Top Results From Across the Web
Manual mock not working in with Jest - Stack Overflow
Module mocks are hoisted when possible with babel-jest transform, so this will result in mocked module: import ModuleA from ".
Read more >Mocks Aren't Stubs - Martin Fowler
Mocks Aren't Stubs. The term 'Mock Objects' has become a popular one to describe special case objects that mimic real objects for testing....
Read more >Why your mock doesn't work - Ned Batchelder
Mocking is a powerful technique for isolating tests. But yours isn't working. Why!?
Read more >Mocks failing to work correctly in large test runs? - Telerik
They seem to be failing because mocks are not correctly intercepting method/property calls. Are there any known issue with the mocks ...
Read more >When to Mock - Enterprise Craftsmanship
The use of mocks in unit testing is a controversial topic (maybe less so ... You obviously don't want to mock the system...
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
https://github.com/facebook/create-react-app/issues/10571#issuecomment-784923197 @lehnerchristian
This works indeed. Thanks for the link. Not sure how useful it is to have
any mocks having their fake implementations removed
(see the docs), because why write a fake implementation in the first place then.So this means that if I try to set a mock in the
beforeAll
test lifecycle it gets overridden if this option is set before running the first test. This also applies for setting the mocks globally within the test file. OnlybeforeEach
and setting the mocks in the test work, because the run after theresetMocks
call?Or in short:
Is this correct?