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

时间:2018-09-25 20:25:15

标签: javascript reactjs unit-testing enzyme

我有一个导出的函数,可以在我的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; }

在我的测试套件中,可以正常测试未导入的React组件,但是 但我不确定如何在其他文件中调用导出的“帮助器”功能。

describe('App', () => {

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

});

产生

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

如何在测试套件中正确定位并调用函数“ getSum”?

1 个答案:

答案 0 :(得分:0)

我最终只是从帮助程序文件中导入函数并对其进行调用

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

describe('App', () => {

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

});

这有效,但是,似乎没有优化,因为我认为react组件的实例应该已经包含了它;但这是一个可行的解决方案。