[Bug]: Unable to use jest.mock
See original GitHub issueVersion
27.1.2
Steps to reproduce
- Clone this repo
cd examples/ts-only
npm i
- Add
jest.mock('example');
tosrc/welcome-message.spec.ts
- Run
npm run test-esm
Expected behavior
Jest to run without an error and for the jest mocking to work correctly.
Actual behavior
I get the following error: ReferenceError: jest is not defined
Debug log
I can’t post a debug log because GitHub Issues throws an error with There was an error creating your Issue: body is too long (maximum is 65536 characters).
Additional context
I’ve tried importing jest from @jest/globals
which resolved the error but then the mocking does not work.
It would be useful if you could provide a working mocking example.
Environment
System:
OS: macOS 11.6
CPU: (8) arm64 Apple M1
Binaries:
Node: 16.13.1 - ~/.nvm/versions/node/v16.13.1/bin/node
npm: 8.3.0 - ~/.nvm/versions/node/v16.13.1/bin/npm
npmPackages:
jest: ^27.3.1 => 27.4.3
Issue Analytics
- State:
- Created 2 years ago
- Comments:7
Top Results From Across the Web
[Bug]: Unable to mock ESM function used indirectly by another ...
In attempting to use unstable_mockModule and mocking a single function within the file I have to somehow import the rest of the functions....
Read more >Cannot mock a module with jest, and test function calls
In my case, I got this error because I failed to implement the mock correctly. My failing code:.
Read more >Mock Functions - Jest
Mock functions are also known as "spies", because they let you spy on the behavior of a function that is called indirectly by...
Read more >Mocking a JavaScript Class with Jest, two ways to make it easier
A guide on how to mock a JavaScript class using Jest, ... any chance of the test failing due to external factors out...
Read more >Fix jest mock 'Cannot access before initialization' error
"since calls to jest.mock() are hoisted to the top of the file, it's not possible to first define a variable and then use...
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
Feel free to submit a PR 😃 The location for docs will be:
jest.mock
isn’t supported in ESM mode yet, see https://github.com/facebook/jest/pull/10976Also you should take a look at https://github.com/facebook/jest/issues/9430 and search for keyword
jest.(do|un)mock
I can conclude this is not an issue for
ts-jest
but for Jest itself.