reactjs - 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 命令,它确实解决了这个问题,然后给我带来了另一个新问题。
解决方案
推荐阅读
- c# - 更改所选记录的索引时,DataGrid 的行号不更新
- reactiveui - 使用 ReactiveUI/DynamicData,如何观察 SourceList 中项目属性的变化
- java - javax.persistence.NonUniqueResultException:查询未返回唯一结果
- html - 在性能方面是添加youtube链接还是下载并链接网站上的视频文件更好?
- javascript - SAP UI5 中的函数声明
- java - 隐藏没有动画的android标题栏
- git - git '~outside repository at~' 发生错误
- sharepoint - Sharepoint Online - 如果列表中的列值选择为 NO,则弹出消息?
- javascript - Vue 不与页面交互就不会更新数据
- node.js - 从具有特定索引的数组中删除对象(MongoDB、NodeJS、React)