"expect is not defined" when using with jest

See original GitHub issue
  • @testing-library/jest-dom version: 4.0.0
  • node version: 12.8.0
  • npm (or yarn) version: 6.10.2

Relevant code or config:

Jest config

module.exports = {
  snapshotSerializers: ['jest-emotion'],
  setupFilesAfterEnv: ['./config/jest.js']
};

jest.js file

require('@testing-library/jest-dom/extend-expect');

What you did:

run the npm test

What happened:

Getting error expect is not defined …

Determining test suites to run...

  ● Test suite failed to run

    ReferenceError: expect is not defined

      at Object.<anonymous> (node_modules/@testing-library/jest-dom/dist/extend-expect.js:7:1)
      at Object.newLoader [as .js] (node_modules/pirates/lib/index.js:104:7)
      at Object.<anonymous> (node_modules/@testing-library/jest-dom/extend-expect.js:2:1)

Reproduction:

https://github.com/tomitrescak/react-boilerplate/tree/Demo

Problem description:

I cannot run my tests

Suggested solution:

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10

github_iconTop GitHub Comments

240reactions
MaffooBristolcommented, Jun 27, 2020

Just to collate the above together without having to click about through links and do reading, my solution was:

In jest.config.js, replace:

setupFiles: [
  './src/__setup__/setupTests.js',
],

With:

setupFilesAfterEnv: [
  './src/__setup__/setupTests.js',
],
45reactions
tomitrescakcommented, Aug 14, 2019

Found the culprit. Sorry for bothering. The same setup file was run in the global setup.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React + Jest Testing Error - ReferenceError: expect is not ...
js ○ Test suite failed to run ReferenceError: expect is not defined 3 | import Adapter from "enzyme-adapter-react-16"; 4 | import '@testing- ...
Read more >
How to fix an error about “expect is not defined” | by Kyosuke Ito
When I tried to connect my app to firebase database for testing, I got an error about “expect is not defined”. I found...
Read more >
Expect - Jest
The expect function is used every time you want to test a value. You will rarely call expect by itself. Instead, you will...
Read more >
Expect · Jest
Use .toBeDefined to check that a variable is not undefined. For example, if you just want to check that a function fetchNewFlavorIdea() returns...
Read more >
From Jest to Vitest - Migration and Benchmark
Here's my path for migrating a project using Jest into using Vitest along with some benchmarking. ... describe is not defined. Well, Jest...
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