【发布时间】:2017-08-03 19:53:38
【问题描述】:
我有一个基本功能:
组件/第一组件:
sayMyName = (fruit) => {
alert("Hello, I'm " + fruit);
return fruit;
}
当我尝试在 FirstComponent.test.js 中使用 Jest 对其进行测试时:
import FirstComponent from '../components/FirstComponent';
describe('<FirstComponent />', () => {
it('tests the only function', () => {
FirstComponent.sayMyName = jest.fn();
const value = FirstComponent.sayMyName('orange');
expect(value).toBe('orange');
});
});
测试说:比较两种不同类型的值。预期的字符串,但未定义。
显然我没有以正确的方式导入函数来测试?
我不够聪明,无法理解 Jest 文档如何测试组件的功能..
有没有一些简单的方法可以从组件中导入功能并进行测试?
编辑: 现在可以使用“react-test-renderer”
import FirstComponent from '../components/FirstComponent';
import renderer from 'react-test-renderer';
describe('<FirstComponent /> functions', () => {
it('test the only function', () => {
const wrapper = renderer.create(<FirstComponent />);
const inst = wrapper.getInstance();
expect(inst.sayMyName('orange')).toMatchSnapshot();
});
})
【问题讨论】:
标签: javascript unit-testing reactjs jestjs