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.

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 the beforeEach (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:open
  • Created 2 years ago
  • Reactions:3
  • Comments:5

github_iconTop GitHub Comments

1reaction
lehnerchristiancommented, Aug 19, 2021

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. Only beforeEach and setting the mocks in the test work, because the run after the resetMocks call?

Or in short:

1. beforeAll
2. resetMocks
3. beforeEach

Is this correct?

Read more comments on GitHub >

github_iconTop 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 >

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