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命令,确实可以解决问题,然后又引发了另一个新问题。 >