1
测试功能,我有一个基本的功能:玩笑基础:从组件
组件/ FirstComponent:
sayMyName = (fruit) => {
alert("Hello, I'm " + fruit);
return fruit;
}
当我试着用玩笑来测试它里面FirstComponent.test.js:
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');
});
});
测试说:比较两种不同类型的值。预期的字符串,但收到未定义。
显然我没有导入函数来测试正确的方式?
我不是足够聪明,了解玩笑文件如何测试从组件功能..
有一些简单的方法来从组件导入功能,并测试它?
编辑: 这个作品现在使用“反应测试,渲染”
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();
});
})
嗯。我不断得到:TypeError:(0,_FirstComponent.sayMyName)不是函数 –
哦,我试过用酶。现在与'react-test-renderer' –
一起工作,你说[tag:jest] - not [tag:酶]。通常是:'const tree = renderer.create( ); tree.getInstance()。方法()' –