Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing Jest错误:测试套件无法运行,如何解决?_Unit Testing_Reactjs_Jestjs - Fatal编程技术网

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

Unit testing Jest错误:测试套件无法运行,如何解决?,unit-testing,reactjs,jestjs,Unit Testing,Reactjs,Jestjs,我正在测试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

我正在测试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,{Component}来自'React';
从“酶”导入{shall};
从“酶到json”导入toJson;
从“Hello”导入Hello;
测试('if am then return goodmorning',()=>{
常数包装=浅(
你好,世界!
);
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>
    }
}
从“React”导入React;
导出默认类Hello.Component{
建造师(道具){
超级();
this.props=props;
//待办事项删除
console.log(“测试道具”,道具);
}
render(){
让我们打招呼;
如果(这个道具){
greet=this.props.timeofday==='am'?'morning':'night'
}
回敬{问候}
}
}
从命令行运行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
开玩笑
app/components/hello.test.js失败
● 测试套件无法运行
无法从“hello.test.js”中找到模块“hello”
在Resolver.resolveModule(node_modules/jest resolve/build/index.js:179:17)
反对。(app/components/hello.test.js:4:14)
测试套件:1个失败,共1个
为什么我不能开玩笑?这里的问题是什么

在您的测试文件中

import Hello from 'hello';
应改为

import Hello from './hello';
或者您可以在webpack配置文件中设置路径