'Error running tests - jest.mock is not a function

I am trying to run tests using Jest, however I am getting this error:

jest.mock is not a function

What am I doing wrong? Here's my CodeSandbox: https://codesandbox.io/s/polling-hook-demo-9yo8c

Note that I am configuring Jest inside my package.json:

"setupFilesAfterEnv": [
  "./src/test/setupTestsAfterEnv.ts"
]

setupTestsAfterEnv.ts extends Jest expect using jest-dom:

import "@testing-library/jest-dom/extend-expect";

I am also using react-testing-library.

Update

This appears to be an issue with my CodeSandbox configuration. I tried the same thing with create-react-app locally and the problem went away!

Here's my Github repo where Jest runs perfectly well: https://github.com/nareshbhatia/hooked-on-polling.

I then imported this repo in a new CodeSandbox: https://codesandbox.io/s/hooked-on-polling-2em7g. When I run tests in this sandbox, I again get the same error: jest.mock is not a function



Solution 1:[1]

Confirmed that this is an issue with CodeSandbox: https://github.com/codesandbox/codesandbox-client/issues/513

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Naresh