How to solve TypeError: environment.teardown is not a function

11,238

Solution 1

You shouldn't need to install jest-cli yourself. It should come out of the box.

Try the following:

  1. Delete package-lock.json, yarn.lock and node_modules
  2. Remove jest from the dependencies in package.json
  3. Then do npm install or yarn install.

Solution 2

Add a fresh version of "jest-environment-jsdom" to file "package.json".

Solution 3

If you have not used create-react-app for the project:

  • Just remove jest, jest-cli, jest-environment-jsdom from package.json.
  • Delete folder node-modules, file package-lock.json and file yarn.lock.
  • Use npm --save i or yarn add to install all of them so that each of them have same version.

This should work!

In my case I was missing the jest-environment-jsdom package.

Solution 4

In my case it throws this error because I create my React project using create-react-app and install jest. In certain way it conflicts.

I solve it by removing jest from my project and using the create-react-app test script.

Solution 5

After an evening of banging my head on this issue, I think I've finally resolved it. First, let me describe the problem:

I was also getting:

TypeError: environment.teardown is not a function

Because I was trying to upgrade Jest to the latest version. When starting an app with Create-React-App, it installs an earlier version of Jest that does not include all of the features. I was interested in using the inlineSnapshots feature, which allows you to assert rendered components against HTML markup inside your test file.

There are a few things not included in the documentation of either Create-React-App or Jest which you should be aware of. If you want to use newer Jest features, like inlineSnapshots, then you need to do the following:

  • yarn adds a fresh version of Jest and jest-environment-jsdom
  • yarn add prettier --dev (This is to facilitate rewriting back into the source file.)
  • Add a Jest env docblock to the very top of each test script.

A docblock is a JavaScript block comment with a pragma mark "@". For setting the Jest environment, you would add this to the top of every test file:

/**
 * @jest-environment jsdom
 */

This instructs jest to run the test in the jsdom environment, which should match what is passed in the test scripts section of your package.json file. Mine looks something like this (ignore the parts with react-app-rewired):

   "scripts": {
      "start": "yarn run flow && react-app-rewired start",
      "build": "yarn run flow && react-app-rewired build",
      "test": "react-scripts test --env=jsdom"
   }

After doing all of this, I was finally able to get inlineSnapshots to work.

Share:
11,238
Admin
Author by

Admin

Updated on July 22, 2022

Comments

  • Admin
    Admin almost 2 years

    I can't test an application which was created with create-react-app.

    All guides says that test is working by default, but when I try "yarn test", it requires install 'jest-cli' and after installation gives an error:

    TypeError: environment.teardown is not a function

  • Weijing Jay Lin
    Weijing Jay Lin almost 6 years
    then how to test a single file? npm test?
  • MstrQKN
    MstrQKN almost 6 years
    Not really sure Weijing, but I think your answer should be here: github.com/facebook/create-react-app/blob/master/packages/…
  • Wilber Hinojosa
    Wilber Hinojosa almost 6 years
    @WeijingJayLin , jest test files based on patterns to match files. If you want to test a single file, you should specify an specific pattern for its file name. yarn run test /specificfolder/otherspecific/file.spec.js If that pattern only matches one file. Then you will test only a single file. the one you want!
  • Ryan Walker
    Ryan Walker over 5 years
    if you used create-react-app make sure "test": "react-scripts test --env=jsdom" is your test script.
  • Rucha Bhatt Joshi
    Rucha Bhatt Joshi over 5 years
    Hello,Try to avoid comment in answer unless you have "specific detail" answer. Please use comment box below and Once you have sufficient reputation you will be able to comment on any post. :)
  • pzrq
    pzrq over 4 years
    This inspired me and worked, npm i -D jest-environment-jsdom@24
  • Peter Mortensen
    Peter Mortensen over 3 years
    module.export? Not module.exports?