开玩笑测试模拟函数 __mocks__ TypeError: x is not a function

时间:2021-01-16 23:21:54

标签: jestjs typeerror

我有一个名为 call.js 的类,它有一个 toggleAudio 函数。我试图模拟这个函数,以便我可以在我的测试中使用它。代码如下:

我的测试:

test('toggleAudio function', () => {
    toggleAudio = require('./call');
    jest.mock('./call')
    expect(toggleAudio(true)).toBe(true);
});

我在 mocks/call.js

中的模拟文件
function toggleAudio(b) {
    return b;
};

获取错误:

TypeError: toggleAudio is not a function

      13 |     toggleAudio = require('./call');
      14 |     jest.mock('./call')
    > 15 |     expect(toggleAudio()).toBe(true);
         |            ^
      16 | });
      17 | 

1 个答案:

答案 0 :(得分:0)

问题在于函数名称和正文之间的额外空格..

function toggleAudio(b){
    return b;
};
相关问题