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

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

Closed
FaabLondon opened this issue Jun 4, 2018 · 44 comments

Comments

@FaabLondon
Copy link

🐛 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(R) Core(TM) 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

@iostate
Copy link

iostate commented Jun 5, 2018

I'm also experiencing the same issue.

@FaabLondon
Copy link
Author

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!

@SimenB
Copy link
Member

SimenB commented Jun 5, 2018

If you use CRA, you shouldn't install Jest yourself - it comes with CRA out of the box 🙂

See e.g. #5119 (comment)

@SimenB SimenB closed this as completed Jun 5, 2018
@yoryos
Copy link

yoryos commented Jun 5, 2018

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(R) Core(TM) 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

@yoryos
Copy link

yoryos commented Jun 5, 2018

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.

@jwickens
Copy link

jwickens commented Jun 6, 2018

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

@SimenB
Copy link
Member

SimenB commented Jun 6, 2018

Again, don't install Jest if you use CRA

@iostate
Copy link

iostate commented Jun 7, 2018

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

@louishoang
Copy link

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(R) Core(TM) 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

@iostate
Copy link

iostate commented Jun 9, 2018

@louishoang I have a few questions in order to gauge your development environment and when the issue is occurring on your machine.

  1. The error occurs after installing a new NPM package, so therefore, it worked before installing an NPM package? Which NPM package was installed immediately before jest broke?
  2. The issue tends to happen after installing a new package or new packages (plural)? If the latter, how did you get jest to work again?
  3. Can you post your entire package.json?
  4. Was jest actually in your package.json?
  5. Did you create your React app using CRA?
  6. Did you go about deleting all 3 files/directories (package-lock.json, yarn.lock, node_modules), removing jest from the dependencies in package.json?

@louishoang
Copy link

@iostate , please have a look at this gist

  1. I was trying to install lodash and then react-slick. After installing any package, the error will happen.
  2. I had to do a fresh install of npm to solve the issue and it's kinda tedious.
  3. Yes, my app was generated using CRA.
  4. I deleted package-lock.json, yarn.lock and node_modules, run npm install and yarn install. This is another error that I'm seeing:
yarn run v1.7.0
$ react-scripts test --env=jsdom --modulePaths=src
2018-06-11 15:44 node[36736] (FSEvents.framework) FSEventStreamStart: register_with_server: ERROR: f2d_register_rpc() => (null) (-22)
2018-06-11 15:44 node[36736] (FSEvents.framework) FSEventStreamStart: register_with_server: ERROR: f2d_register_rpc() => (null) (-22)
events.js:167
      throw er; // Unhandled 'error' event
      ^

Error: EMFILE: too many open files, watch
    at FSEvent.FSWatcher._handle.onchange (fs.js:1370:28)
Emitted 'error' event at:
    at FSEvent.FSWatcher._handle.onchange (fs.js:1376:12)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

@danielmiclos
Copy link

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

@jdnichollsc
Copy link

Same issue from AppCenter:

[command]/usr/local/bin/npm test

> employeeApp@0.0.1 test /Users/vsts/agent/2.134.2/work/1/s/react-native
> jest

FAIL src/components/Catalog/__tests__/ProductList-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

FAIL src/components/Catalog/__tests__/CategoryList-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

Test Suites: 2 failed, 2 total
Tests:       0 total
Snapshots:   0 total
Time:        5.679s
Ran all test suites.
npm ERR! Test failed.  See above for more details.
##[error]npm failed with error: /usr/local/bin/npm failed with return code: 1

@revanthCopart
Copy link

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.

@louishoang
Copy link

@revanthCopart Did you have to install jest-cli? And if you do, what is the version that works for you?

@revanthCopart
Copy link

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

@louishoang
Copy link

louishoang commented Jun 23, 2018

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 Watch mode stopped working on macOS Sierra #1767. brew install watchman fixed it. 🎉

@greyscaled
Copy link

greyscaled commented Jun 25, 2018

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 #6393 (comment) 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.

@VladFiliucov
Copy link

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

@citius
Copy link

citius commented Jul 19, 2018

@yoryos

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

mdvanes added a commit to code-star/codestar-website-react that referenced this issue Jul 21, 2018
cheshire137 added a commit to cheshire137/competiwatch-desktop that referenced this issue Jul 23, 2018
@pwlmaciejewski
Copy link

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.

@NickTomlin
Copy link

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

@prashantalhat
Copy link

I followed these steps

  1. Delete jest from node modules
  2. Delete package.lock file
  3. Delete jest entry from package.json file
  4. ReInstall jest npm package
    to get it working

@tratnayake
Copy link

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

@davis-mwangi
Copy link

@tratnayake your solutions works like a charm.

@jstowers
Copy link

@tratnayake, good job!

@jstowers
Copy link

@citius, good job! I followed your instructions and deleted jest-cli. Tests are working now.

@mohcinenazrhan
Copy link

I followed these steps

Delete node modules folder
ReInstall packages
to get it working

@Divivek
Copy link

Divivek commented Aug 22, 2018

Same with me delete and reload the node Modules and it's working.

@denisx
Copy link

denisx commented Aug 31, 2018

try this https://stackoverflow.com/a/52118404/9074387

@luiscvalmeida
Copy link

luiscvalmeida commented Sep 5, 2018

I fixed this by removing @jest-environment jsdom from the comment header on the top of my spec files. I had this from a previous jest version.

@RrNn
Copy link

RrNn commented Sep 10, 2018

had the same issue @FaabLondon's solution above helped me fix it.

@psyanite
Copy link

I installed some jest packages, and like everyone said, it kept throwing the same error, UNTIL I deleted package-lock.json, deleted node_modules, and ran npm install luckily it literally only took 1 minute. BUT NOW IT WORKS! well it's throwing another error now, at least.

@cliff76
Copy link

cliff76 commented Sep 25, 2018

I'm having this problem currently because I am attempting to use the inlineSnapshots feature which is not available in the version installed via CRA. I've submitted a feature request to CRA to upgrade but in the interim, is there any workaround to allow use of inlineSnapshots in a CRA project?

@thymikee
Copy link
Collaborator

eject or react-app-rewired

@ghost
Copy link

ghost commented Sep 29, 2018

@tratnayake Thanks mate!

@lepirlouit
Copy link

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.

@PavanGangireddy
Copy link

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

AndrewIsh pushed a commit to PTFS-Europe/koha-react that referenced this issue Nov 13, 2018
We're not supposed to install it explicity <sigh>. Installing it is not
a fix for the "environment.teardown is not a function" error. This is:

jestjs/jest#6393 (comment)
@Maweypeyyu
Copy link

In my case there was one of my dependencies that depended on an old jest version that caused it. After doing a

grep -A3 _requiredBy node_modules/jest-environment-jsdom/package.json

I found out which package was the cause of it ( which included jest as dependency instead of devDependency ). Moving jest to devDeps on that package fixed the issue.

@victorwvieira
Copy link

I deleted the "jest-cli" dependency and run "yarn install". Now it works!

cheshire137 added a commit to cheshire137/competiwatch-desktop that referenced this issue May 9, 2019
Got this error when I ran `npm test`:

> TypeError: environment.teardown is not a function

Followed this advice: jestjs/jest#6393 (comment)
@sftwre
Copy link

sftwre commented Jul 3, 2019

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!
I believe you should only be using one package manager at a time (either npm or yarn). Using both has given me a lot problems in the past, because of unsynced dependencies between the two .lock files.

@jzebgul
Copy link

jzebgul commented Apr 3, 2020

hi i remove node-modules and yarn and package-lock.json && then install yarn first then yarn add jset@20.0.4 and in the end npm install and it works for me .

@sayax
Copy link

sayax commented Sep 8, 2020

Hi, i fixed my issue with the following steps:

  1. my jest.config.ts has this lines
    module.exports = {
    testMatch: ['**/+(*.)+(spec|test).+(ts|js)?(x)'],
    transform: {
    '^.+\.(ts|js|html)$': 'ts-jest',
    },
    moduleFileExtensions: ['ts', 'js', 'html'],
    coverageReporters: ['html'],
    };
    i have a separate jest.config.ts for each library used with nrwl
    module.exports = {
    name: 'project-name',
    preset: '../../../../jest.config.js',
    coverageDirectory: '../../../../coverage/libs/libarary',
    snapshotSerializers: [
    'jest-preset-angular/build/AngularNoNgAttributesSnapshotSerializer.js',
    'jest-preset-angular/build/AngularSnapshotSerializer.js',
    'jest-preset-angular/build/HTMLCommentSerializer.js',
    ],
    // setupFilesAfterEnv: ['/src/test-setup.ts'],
    globals: {
    'ts-jest': {
    tsConfig: '/tsconfig.spec.json',
    stringifyContentPathRegex: '\.(html|svg)$',
    astTransformers: [
    'jest-preset-angular/build/InlineFilesTransformer',
    'jest-preset-angular/build/StripStylesTransformer',
    ],
    },
    },
    };
  2. installed latest versions of jest
    "jest": "^26.4.2",
    "jest-environment-node": "^26.3.0",
    "jest-environment-jsdom": "^26.3.0",
    then it worked

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests