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

后端 未结 7 1804
伪装坚强ぢ
伪装坚强ぢ 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: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.

提交回复
热议问题