gpt4 book ai didi

unit-testing - Jest 错误: Test suite failed to run,如何解决?

转载 作者:行者123 更新时间:2023-12-03 14:20:13 25 4
gpt4 key购买 nike

我正在测试 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 时,出现此错误:

    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

为什么我不能运行这个 Jest ?这是什么问题?

最佳答案

在您的测试文件中,

import Hello from 'hello';

应改为

import Hello from './hello';

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

关于unit-testing - Jest 错误: Test suite failed to run,如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44349403/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com