Jest错误:测试套件无法运行,如何解决?

时间:2017-06-03 23:23:27

标签: unit-testing reactjs jestjs

我正在测试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

为什么我不能开玩笑?这是什么问题?

1 个答案:

答案 0 :(得分:2)

在您的测试文件中,

import Hello from 'hello';

应改为

import Hello from './hello';

或者您可以在webpack配置文件中设置路径。