简体   繁体   English

使用反应/酶对输出功能进行单元测试

[英]Unit testing of exported function using react / enzyme

I have an exported function, which I import and use in my react component: 我有一个导出的函数,可以在我的react组件中导入并使用它:

//in App.js
import { getSum } from "../helpers/helpers.js;
//some react component code here.

//in helpers.js
export function getSum(arg1, arg2){return arg1+arg2; }

in my test suite, normal testing of non-imported react components is fine, but but I am not sure how to call the exported "helper" function in the other file. 在我的测试套件中,可以正常测试未导入的React组件,但是我不确定如何在另一个文件中调用导出的“ helper”函数。

describe('App', () => {

it('correctly calculates the sum of 1 and 3', () => {
        const wrapper = shallow(<App />);
        assert.equal(wrapper.instance().getSum(1, 3), 4);
    });

});

results in 结果是

 TypeError: wrapper.instance(...).getSum is not a function
        at Context.<anonymous> (client/test/index.js:191:5325)

How would I properly locate and call the function "getSum" in my test suite? 如何在测试套件中正确定位并调用函数“ getSum”?

I ended up just importing the function from the helper file and calling it 我最终只是从助手文件中导入函数并对其进行调用

import { getSum } from "../helpers/helpers.js;"

describe('App', () => {

it('correctly calculates the sum of 1 and 3', () => {
        assert.equal(getSum(1, 3), 4);
    });

});

This works, however, it doesn't seem that optimized as the I feel that an instance of the react component should already contain this; 这有效,但是,似乎没有优化,因为我觉得react组件的实例应该已经包含了它。 but this is a working solution. 但这是一个可行的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM