我正在测试Jest for reactjs,尤其是快照测试。这是我的考验:
import React, { Component } from 'react';
import { shallow } from 'enzyme';
import toJson from 'enzyme-to-json';
import Hello from 'hello';
test('if am then return goodmorning', () => {
const wrapper = shallow(
<Hello timeofday="am">
<strong>Hello World!</strong>
</Hello>
);
expect(toJson(wrapper)).toMatchSnapshot();
});
我正在测试的组件:
import React from 'react';
export default class Hello extends React.Component {
constructor(props) {
super();
this.props = props;
//todo remove
console.log('testing props', props);
}
render() {
let greet ;
if (this.props) {
greet= this.props.timeofday ==='am' ? 'morning' : 'evening'
}
return <h1>Good {greet}</h1>
}
}
当我从命令行运行jest时,我收到此错误:
jest
FAIL app/components/hello.test.js
● Test suite failed to run
Cannot find module 'hello' from 'hello.test.js'
at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:179:17)
at Object.<anonymous> (app/components/hello.test.js:4:14)
Test Suites: 1 failed, 1 total
为什么我不能开玩笑?这是什么问题?
答案 0 :(得分:2)
在您的测试文件中,
import Hello from 'hello';
应改为
import Hello from './hello';
或者您可以在webpack配置文件中设置路径。