2017-06-03 47 views
2

我正在測試Jest for reactjs,尤其是快照測試。這是我的測試:Jest錯誤:測試套件未能運行,如何解決?

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 

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 

爲什麼我不能運行笑話?這裏有什麼問題?

回答

2

在您的測試文件,

import Hello from 'hello'; 

應改爲

import Hello from './hello'; 

,或者你可以設置在配置的WebPack文件的路徑。