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

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

我正在尝试实现 Jest 网站中显示的示例:Getting started with Jest

虽然 运行 npm test 我收到以下错误:

FAIL  src/sum.test.js
  ● Test suite failed to run

    TypeError: environment.setup is not a function

      at node_modules/jest-runner/build/run_test.js:112:23

sum.js:

function sum(a, b){
  return a+b;
}
module.exports = sum;

sum.test.js:

const sum = require('./sum');

test('adding sum function', () => {
  expect(sum(234,4)).toBe(238);
})

sum.jssum.test.jsGetting started with Jest.

中所示示例的精确副本

package.json:

{
  "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"
  }
}

那么,我怎样才能摆脱 TypeError: environment.setup is not a function 错误?

基于这个 github 问题:@jest-environment node not working in v22,

我将 sum.test.js 更新为:

/**
 * @jest-environment node
 */

const sum = require('./sum');

test('adding sum function', () => {
  expect(sum(234,4)).toBe(238);
})

现在我摆脱了 TypeError: environment.setup is not a function

输出:

PASS  src/sum.test.js
  ✓ adding sum function (2ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.438s
Ran all test suites.

已更新

看了几个答案,今天重现了场景

第 1 步: 我使用 create-react-app.

创建了一个新项目

命令:create-react-app demo-app

package.json 文件:

{
  "name": "demo-app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^16.3.2",
    "react-dom": "^16.3.2",
    "react-scripts": "1.1.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  }
}

此文件显示 jest 未包含在依赖项中。

第 2 步: 我将 sum.jssum.test.js 文件包含在 src 文件夹中,如官方 getting started guide.

所示

sum.js:

function sum(a, b) {
  return a + b;
}
module.exports = sum;

sum.test.js:

const sum = require('./sum');

test('adds 1 + 2 to equal 3', () => {
  expect(sum(1, 2)).toBe(3);
});

第 3 步: 我 运行 yarn test 命令没有在 package.json 文件中包含 jest。 因此,此测试命令使用 react-scripts test --env=jsdom,如 package.json.

所示

测试输出:

PASS  src/sum.test.js
PASS  src/App.test.js

Test Suites: 2 passed, 2 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        0.594s, estimated 1s
Ran all test suites related to changed files.

第 4 步: 我更新了 package.json 文件以使用 jest 作为测试脚本:

{
  "name": "demo-app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^16.3.2",
    "react-dom": "^16.3.2",
    "react-scripts": "1.1.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "jest",
    "eject": "react-scripts eject"
  }
}

我再次 运行 yarn test 并且输出是:

$ jest
 PASS  src/sum.test.js
 FAIL  src/App.test.js
  ● Test suite failed to run

    /<PATH>/demo-app/src/App.test.js: Unexpected token (7:18)
         5 | it('renders without crashing', () => {
         6 |   const div = document.createElement('div');
      >  7 |   ReactDOM.render(<App />, div);
           |                   ^
         8 |   ReactDOM.unmountComponentAtNode(div);
         9 | });
        10 | 

Test Suites: 1 failed, 1 passed, 2 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.645s, estimated 1s
Ran all test suites.

太长;未读 (TL;DR)

  1. 如果使用 create-react-app 命令,请不要使用 yarn add --dev jestnpm install --save-dev jest 添加 jest。如果您使用 create-react-appjest 包含在项目的 node-modules 中。

来自official Jest documentation

Setup with Create React App

If you are just getting started with React, we recommend using Create React App. It is ready to use and ships with Jest! You don't need to do any extra steps for setup, and can head straight to the next section.

  1. 不要更改 package.json 文件 { look at this issue } 中的测试脚本。保留默认的 "test": "react-scripts test --env=jsdom",

您应该将已安装的 jest 降级为 v20.0.4 npm install -D jest@20.0.4

添加

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

到您的 package.json 文件,您的 package.json 将如下所示:

{
  "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"
  }
}

更新

对于使用create app遇到这个问题的人,有两种解决方法。

1- 将 package.json 中的 test 命令(或添加新命令)更新为 jest 而不是 react-scripts test,因为 react-scripts test 不允许 package.json 中的开玩笑选项,如评论中所述@Xinyang_Li。

2-如果你不想改变package.json中的测试命令,使用默认的create-react-app测试;从 packge.json 中删除笑话选项(如果存在)

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

然后 删除 node_modules 然后 运行 npm install 将它们全部安装再次,然后你的测试应该 运行 正常。

Jest 包含在 react-scripts 中。该错误是由于在以 react-scripts 开头的项目中安装 Jest 时出现的冲突。 "Getting started with Jest" 指南需要一个 'clean' 项目。

只需从您的(开发)依赖项中删除 Jest,它就可以工作了。

如果您想使用 Jest 来测试 React 组件(您可能会这样做),您需要将 package.json 中的测试脚本修改为 react-scripts test --env=jsdom

只需从您的开发依赖项中删除 jest。为此,运行 以下命令:

npm remove --save-dev jest

我的问题是这样解决的:
.
如果你的 package.json 中同时有 react-scripts 和 jest,请从中删除 jest。
然后删除package-lock.json, yarn.lock and node_modules.
然后 运行 npm install (或者 yarn 如果你使用它)。 ~丹·阿布拉莫夫~ . issues #5119 .

这是一个较旧的问题,但我遇到了同样的问题。我们有 react-scripts 包,它使用 Jest 版本 20.x.x,它与 coverageTreshold 有问题。我无法使用较新的 react-scripts 版本,唯一有帮助的是使用纱线分辨率,请在此处阅读更多内容 https://yarnpkg.com/lang/en/docs/selective-version-resolutions/