TypeError: environment.setup is not a function in React Testing

后端 未结 7 1817
伪装坚强ぢ
伪装坚强ぢ 2021-02-12 08:44

I was trying to implement the example shown in Jest website: Getting started with Jest.

While running npm test on I was getting the following error:

<
相关标签:
7条回答
  • 2021-02-12 08:52

    This is an older issue but I was facing the same problem. We have react-scripts package and it uses Jest version 20.x.x which has problems with coverageTreshold. I couldn't use newer react-scripts version and the only thing helped was to use yarn resolutions, read more here https://yarnpkg.com/lang/en/docs/selective-version-resolutions/.

    0 讨论(0)
  • 2021-02-12 08:56

    Just remove jest from your dev dependencies. To do so, run the following command:

    npm remove --save-dev jest
    
    0 讨论(0)
  • 2021-02-12 08:56

    Add

    "jest": {
        "testEnvironment": "node"
      }
    

    to your package.json file, your package.json would look like this:

    {
      "name": "jest-demo-test",
      "version": "0.1.0",
      "private": true,
      "dependencies": {
        "react": "^16.2.0",
        "react-dom": "^16.2.0",
        "react-scripts": "1.0.17"
      },
      "scripts": {
        "start": "react-scripts start",
        "build": "react-scripts build",
        "test": "jest",
        "eject": "react-scripts eject"
      },
      "devDependencies": {
        "jest": "^22.0.4"
      },
       "jest": {
        "testEnvironment": "node"
      }
    }
    

    UPDATE

    for the people using create app and having this problem, there are two solutions.

    1- update testcommand (or add new one) in package.json to jest instead of react-scripts test, because react-scripts test will not allow jest options in package.json as @Xinyang_Li stated in comments.

    2-if you don't want to change test command in package.json, and use default create-react-app test; remove jest options from packge.json (if exists)

     "jest": {
        "testEnvironment": "node"
      }
    

    then remove node_modules and then run npm install to install them all again, and after that your tests should run normally.

    0 讨论(0)
  • 2021-02-12 08:56

    You should downgrade installed jest to v20.0.4 npm install -D jest@20.0.4

    0 讨论(0)
  • 2021-02-12 08:57

    Jest is included with react-scripts. The error is due to a conflict that arose when you installed Jest in a project started with react-scripts. The "Getting started with Jest" guide expects a 'clean' project.

    Simply remove Jest from your (dev)dependencies and it should work.

    If you want to use Jest for testing React components (which you probably do), you need to modify your test script in package.json to react-scripts test --env=jsdom, as it was.

    0 讨论(0)
  • 2021-02-12 09:01

    My issue solve with this:
    .
    If you have both react-scripts and jest in your package.json, delete jest from it.
    Then delete package-lock.json, yarn.lock and node_modules.
    Then run npm install (or yarn if you use it). ~Dan Abramov~ . issues #5119 .

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