jest: TypeError: environment.teardown is not a function at node_modules/jest-runner/build/run_test.js

šŸ› Bug Report

Hello, I have used create React app and can’t make the tests work. Initially, I got an error message with jest and jest-cli so installed jest with npm install --save-dev jest and then jest-cli with npm install jest-cli. However, I still get the following message: Test suite failed to run TypeError: environment.teardown is not a function at node_modules/jest-runner/build/run_test.js

Run npx envinfo --preset jest

Paste the results here: System: OS: macOS High Sierra 10.13.2 CPU: x64 IntelĀ® Coreā„¢ i5-5350U CPU @ 1.80GHz Binaries: Node: 9.4.0 - ~/.nvm/versions/node/v9.4.0/bin/node Yarn: 1.6.0 - /usr/local/bin/yarn npm: 6.1.0 - ~/.nvm/versions/node/v9.4.0/bin/npm npmPackages: jest: ^23.1.0 => 23.1.0


About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 17
  • Comments: 44 (2 by maintainers)

Commits related to this issue

Most upvoted comments

I managed to make it work after deleting package-lock.json, yarn.lock, node_modules, removing jest from the dependencies in package.json, then doing npm install and yarn install!

Before I could run the tests though the script was complaining that it was missing the jest-cli.

I installed that, then got this error.

If you use CRA, you shouldn’t install Jest yourself - it comes with CRA out of the box šŸ™‚

See e.g. https://github.com/facebook/jest/issues/5119#issuecomment-356120965

Thanks, @revanthCopart! I managed to get back on track by doing the following steps:

  1. Delete package-lock.json, yarn.lock, node_modules.

  2. Deleting jest from package.json didn’t help me because npm install and yarn install re-installed 2 versions of jest when inspecting package-lock.json. I removed jest and jest-cli completely by: npm uninstall jest jest-cli and yarn remove jest jest-cli.

  3. npm install then yarn install.

  4. At this point, I have the issue mentioned in #1767. brew install watchman fixed it. šŸŽ‰

I am using jest version 21.2.1. In my case I had to install jest-cli 21.2.1 in order to fix this

@yoryos

  • remove jest-cli from your deps
  • remove *.lock files,
  • remove node_modules
  • run yarn install or npm i, relates from you using tool.

Following the above steps but using version 20.0.4 worked for me.

  1. Delete jest and jest-cli from node modules.
  2. Delete package.lock file
  3. Update package.json file for following:
dependencies": {
    "jest": "20.0.4",
    "jest-cli": "20.0.4",
  1. Reinstall using npm install

I had to also install jest-cli as without doing so I got the error as documented below. But here’s the kicker - it could not find my tests (0 matches) after doing so. I’ve never had trouble with jest/cra on windows like this until this most recent project.

Error Stack

$ npm run test

> 3karena-landing@0.0.0 test C:\Users\...
> react-scripts test --env=jsdom

module.js:545
    throw err;
    ^

Error: Cannot find module 'C:\Users\...\node_modules\jest-cli'
    at Function.Module._resolveFilename (module.js:543:15)
    at Function.Module._load (module.js:470:25)
    at Module.require (module.js:593:17)
    at require (internal/module.js:11:18)
    at getJest (C:\Users\...\node_modules\jest\node_modules\jest-cli\build\cli\getJest.js:22:12)
    at Object.run (C:\Users\...\node_modules\jest\node_modules\jest-cli\build\cli\index.js:39:48)
    at Object.<anonymous> (C:\Users\...\node_modules\react-scripts\scripts\test.js:104:6)
    at Module._compile (module.js:649:30)
    at Object.Module._extensions..js (module.js:660:10)
    at Module.load (module.js:561:32)

Environment

  • win10
  • yarn 1.3.2
  • npm 6.1.0
  • node v9.9.0
  • create-react-app 1.5.2
  • react-scripts 1.1.4

EDIT: Solved Precisely as in https://github.com/facebook/jest/issues/6393#issuecomment-399615757 everything worked after installing jest-cli@20.0.4

Do not simply run npm install --save-dev jest-cli as ^23 will be installed and things will still break.

Again, don’t install Jest if you use CRA

@louishoang I had to install jest-cli version 20.0.4.

For me a workaround was removing the config option testEnvironment: 'node'. Is the node test environment not bundled in the latest jest?

Same issue with clean CRA.

FAIL src/App.test.js ā— Test suite failed to run

TypeError: environment.teardown is not a function

  at node_modules/jest-runner/build/run_test.js:191:25

Ran npx envinfo --preset jest System: OS: macOS High Sierra 10.13.3 CPU: x64 IntelĀ® Coreā„¢ i7-6700HQ CPU @ 2.60GHz Binaries: Node: 8.9.1 - ~/.nvm/versions/node/v8.9.1/bin/node Yarn: 1.3.2 - /usr/local/bin/yarn npm: 5.5.1 - ~/.nvm/versions/node/v8.9.1/bin/npm npmPackages: jest: ^23.1.0 => 23.1.0

@pwlmaciejewski thank you for that.

I was encountering this using an ejected CRA (without typescript) where I had npm i jest-cli after ejecting.

When I aligned my versions of jest and jest-cli jest started successfully.

Just FYI.

In my TS project that doesn’t use CRA I was getting

yarn run v1.7.0
$ jest
 FAIL  tests/xxx.test.ts
  ā— Test suite failed to run

    TypeError: environment.teardown is not a function

      at node_modules/jest-runner/build/run_test.js:191:25

The solution was to align jest with ts-jest MAJOR.MINOR and install jest-cli with matching version:

"jest": "~23.0.1",
"jest-cli": "~23.0.1",
"ts-jest": "~23.0.1",

The higher version of jest-cli was throwing the error.

This problem is coming up again.

I removed yarn.lock, package-lock.json and node_modules I have no jest dependency in package.json with yarn it installs version 20.0.3 of jest-environment-jsdom (and problem occurs) with npm version 23.4.0 is installed and no more problems.

Before installing jest and jest-cli and I had exact the same error as @louishoang.

Now I’m having the ā€œTypeError: environment.teardown is not a functionā€ issue.

I’m running OSX 10.12.6

Worked when I used the same jest version as the react scripts jest version. Which in my case is 20.0.4. Lot of them mentioned that we should not be installing jest ourselves since it comes with react-scripts but that didn’t work for me.

Same issue for me with or without standalone Jest. I tried @FaabLondon 's suggestion but no luck. The issue tends to happen after installing a new package.

System: OS: macOS High Sierra 10.13.5 CPU: x64 IntelĀ® Coreā„¢ i7-7700HQ CPU @ 2.80GHz Binaries: Node: 10.3.0 - ~/.nvm/versions/node/v10.3.0/bin/node Yarn: 1.7.0 - /usr/local/bin/yarn npm: 6.1.0 - ~/Desktop/maximax/wada-front/node_modules/.bin/npm npmPackages: jest: ^23.1.0 => 23.1.0

Was able to fix using @FaabLondon’s suggestion above.