首页 > 解决方案 > 我如何测试对象内部的函数调用?

问题描述

我有这个js文件


const apiConfig = {
    addWipMappingStates: {
        method: 'post',
        url: env.WIP_STATES_KEY,
        getResponseActions: () => {
            toastr.success(`Status has been successfully added`);

            return [
                actions.apiCallStarted('wipMappingStates'),
                actions.resetFormValue(['newWipStatesForm']),
                actions.apiCallReset('addWipMappingStates'),
            ];
        }
}

export default apiConfig

我是开玩笑和酶测试的新手。我找不到方法 - 我如何测试getResponseActions:我的对象中的方法。

我的测试文件

我试过了

import sut from '../../constants/apiConfig';

const apiConfig = {
    addWipMappingStates: {
        getResponseActions: jest.fn(),
    },
};
describe('apiConfig', () => {
    test('addWipMappingStates', () => {
        const result = sut(apiConfig);
    });
});

但是当我运行时npm run test 出现错误

TypeError: (0 , _apiConfig.default) is not a function

       8 | describe('apiConfig', () => {
       9 |      test('addWipMappingStates', () => {
    > 10 |              const result = sut(apiConfig);
         |                             ^
      11 |      });
      12 | });

我不知道如何继续这个。请帮忙

标签: reactjsunit-testingjestjsenzyme

解决方案


推荐阅读