我正在尝试使用react给出的示例代码运行测试。
我已导入对我的测试文件的反应,使用:import React from' react&#39 ;; 这是我的package.json:
{
"name": "newjest",
"version": "1.0.0",
"description": "testing the test",
"main": "index.js",
"dependencies": {
"react": "15.4.2",
"react-dom": "15.4.2"
},
"devDependencies": {
"babel-jest": "^5.3.0",
"babel-preset-env": "^5.3.0",
"babel-preset-es2015": "^5.3.0",
"babel-preset-react": "^5.3.0",
"enzyme": "*",
"jest": "*",
"react-addons-test-utils": "^15.4.2",
"react-test-renderer": "^16.1.0"
},
"scripts": {
"test": "jest"
},
"author": "suparna",
"license": "ISC"
}
它因错误而失败,测试套件无法运行,无法找到模块'反应
> newjest@1.0.0 test /Users/suparnasoman/Desktop/newjest
> jest
FAIL __tests__/CheckboxWithLabel-test.js
● Test suite failed to run
Cannot find module 'react' from 'CheckboxWithLabel-test.js'
at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:191:17)
at Object.<anonymous> (__tests__/CheckboxWithLabel-test.js:3:14)
这是我的CheckboxWithLabel-test.js的代码:
import React from 'react';
import {shallow} from 'enzyme';
import CheckboxWithLabel from '../CheckboxWithLabel';
test('CheckboxWithLabel changes the text after click', () => {
// Render a checkbox with label in the document
const checkbox = shallow(
<CheckboxWithLabel labelOn="On" labelOff="Off" />
);
expect(checkbox.text()).toEqual('Off');
checkbox.find('input').simulate('change');
expect(checkbox.text()).toEqual('On');
});