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.

[Bug]: Unable to use jest.mock

See original GitHub issue

Version

27.1.2

Steps to reproduce

  1. Clone this repo
  2. cd examples/ts-only
  3. npm i
  4. Add jest.mock('example'); to src/welcome-message.spec.ts
  5. 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:open
  • Created 2 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
ahnpnlcommented, Jan 4, 2022

jest.mock isn’t supported in ESM mode yet, see https://github.com/facebook/jest/pull/10976

Also 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.

Read more comments on GitHub >

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

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