Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"expect is not defined" when using with jest #122

Closed
tomitrescak opened this issue Aug 14, 2019 · 11 comments
Closed

"expect is not defined" when using with jest #122

tomitrescak opened this issue Aug 14, 2019 · 11 comments

Comments

@tomitrescak
Copy link

  • @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:

@tomitrescak
Copy link
Author

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

@martinemmert
Copy link

If anyone else who hits a roadblock here like me, have a look at this comment to solve the issue: enzymejs/enzyme-matchers#86 (comment)

@ealemda2
Copy link

ealemda2 commented Jun 12, 2020

Ran into this issue on CI server runnning node v10.10 after upgrading the following dependencies:

* "jest": "^25.1.0",  --> "^26.0.1"
* "@testing-library/jest-dom": "^5.1.1" --> "^5.9.0"
* "@testing-library/react": "^9.5.0" --> "^10.2.1"

and having switched from running tests with --env=jsdomto --env=jest-environment-jsdom-sixteen

Can confirm that upgrading node to v10.15 fixed the problem, assuming the jest setup file config is correctly defined.

@MaffooBristol
Copy link

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',
],

@elado
Copy link

elado commented Jul 1, 2020

had the same issue, only in CI, but the setup file was under setupFilesAfterEnv, as expected. upgraded node from 10 -> 12 and it was resolved.

@mayconmesquita
Copy link

Thanks, @MaffooBristol! Your solution works!

@pjamessteven
Copy link

If anyone is using Vue Test Utils and still having this problem after trying these solutions, make sure all of your packages are up to date in package.json then delete your node modules folder and run npm install again.

"@vue/cli-plugin-babel": "~4.5.0",
"@vue/cli-plugin-unit-jest": "^4.5.4",
"@vue/cli-service": "~4.5.0",
"@vue/test-utils": "^1.0.3",

@finnmerlett
Copy link

Just to add my findings: you can do the exact same thing as a command line option when running an npm script. I needed this because I was using react-scripts, and wanted to change the default name (setupTests.js) to snake case to fit the rest of my codebase (setup-tests.js), without adding a jest.config.js file. Ahh it's the little things isn't it.

In my package.json scripts entries I had
"test": "react-scripts test --setupFiles <test setup filepath>
and it was giving me this error. Fixed by changing to
"test": "react-scripts test --setupFilesAfterEnv <test setup filepath>

@krysalead
Copy link

If anyone is using Vue Test Utils and still having this problem after trying these solutions, make sure all of your packages are up to date in package.json then delete your node modules folder and run npm install again.

"@vue/cli-plugin-babel": "~4.5.0",
"@vue/cli-plugin-unit-jest": "^4.5.4",
"@vue/cli-service": "~4.5.0",
"@vue/test-utils": "^1.0.3",

Sorry this was not working for me I was forced to import the expect function from Chai

@ksharm95
Copy link

https://stackoverflow.com/a/69222856/9518005
in my case i solve by using this

darreneng added a commit to appfolio/react-gears that referenced this issue Dec 30, 2021
- Note: using `npm install --save-dev` will also sort dependencies
automatically.

add:

- @testing-library/jest-dom
  - Need to import in test/helpers.js:
    https://github.com/testing-library/jest-dom#installation
  - Need to use `setupFilesAfterEnv` instead:
    testing-library/jest-dom#122

update:

- axe-core
- jest, @types/jest
  - When upgrading to v27, we need to explicitly specify jsdom env:
    https://jestjs.io/blog/2021/05/25/jest-27#flipping-defaults
- @testing-library/react
- @testing-library/react-hooks
- @testing-library/user-event

squash
darreneng added a commit to appfolio/react-gears that referenced this issue Jan 3, 2022
- Note: using `npm install --save-dev` will also sort dependencies
automatically.

add:

- @testing-library/jest-dom
  - Need to import in test/helpers.js:
    https://github.com/testing-library/jest-dom#installation
  - Need to use `setupFilesAfterEnv` instead:
    testing-library/jest-dom#122

update:

- axe-core
- jest, @types/jest
  - When upgrading to v27, we need to explicitly specify jsdom env:
    https://jestjs.io/blog/2021/05/25/jest-27#flipping-defaults
- @testing-library/react
- @testing-library/react-hooks
- @testing-library/user-event
- nyc
  - Needed to be updated to handle issue when testing w/ node 14:
  https://github.com/appfolio/react-gears/runs/4693696609?check_suite_focus=true
@jessepinho
Copy link

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',
],

Note that, if you don't have other setup to do after env, you can just include this in your Jest config:

    setupFilesAfterEnv: [
      "@testing-library/jest-native/extend-expect"
    ],

...i.e., no need to create a separate setupTests.js file if all it does is include extend-expect.

Note: if you're using TypeScript, it will complain that the matchers don't exist. (TypeScript doesn't know about your Jest config, obviously.) To fix this, I created a src/jest.d.ts file with the following:

import '@testing-library/jest-native/extend-expect'

Since it's a .d.ts file, it won't run any code; it'll just import the types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests