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

前端 未结 6 786
刺人心
刺人心 2021-01-18 01:27

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 requir

相关标签:
6条回答
  • 2021-01-18 01:37

    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.

    0 讨论(0)
  • 2021-01-18 01:43

    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.

    0 讨论(0)
  • 2021-01-18 01:50

    For me, it was the module import binding. Basically, for me, it was the module export/import binding.

    Basically if it was exported using module.export, you should import using require. If it was exported using export or export default, you should import using import.

    No mixing is allowed here.

    0 讨论(0)
  • 2021-01-18 01:52

    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.
    0 讨论(0)
  • 2021-01-18 01:57

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

    0 讨论(0)
  • 2021-01-18 01:57

    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.

    0 讨论(0)
提交回复
热议问题