首页 > 解决方案 > ReactJS 的默认测试框架是什么?

问题描述

tl;dr:运行基本测试运行非常困难

通过使用Create-React-App脚手架一个新项目,它将包含App.test.js以下内容的默认值:

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

it('renders without crashing', () => {
  const div = document.createElement('div');
  ReactDOM.render(<App />, div);

  //expect(div.innerHTML).toContain('Hi There!');

  ReactDOM.unmountComponentAtNode(div);
});

我试过运行命令yarn test,下面是我得到的响应

错误:找不到模块'/Users/isaaclem/Code/ReactJS/testing/node_modules/jest-cli'

如果我npm run test改为尝试,我也会遇到同样的错误,即找不到jest-cli. 有什么理由C-R-A给我们一个默认的测试文件但不包括必要的框架/模块?

以下是默认值package.json

{
  "name": "testing",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^16.4.1",
    "react-dom": "^16.4.1",
    "react-router-dom": "^4.3.1",
    "react-scripts": "1.1.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  }
}

更新:

然后我执行命令npm install --save jest-cli来安装缺少的依赖项并尝试再次运行测试并改为遇到以下错误:

Determining test suites to run...
--watch is not supported without git/hg, please use --watchAll
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! testing@0.1.0 test: `react-scripts test --env=jsdom`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the testing@0.1.0 test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

对这个错误的小研究,似乎我们需要git init解决这个问题,因此我运行了 git init 命令,它确实解决了这个问题,然后给我带来了另一个新问题。

在此处输入图像描述

标签: reactjstesting

解决方案


推荐阅读