【发布时间】:2020-01-31 05:15:59
【问题描述】:
我正在编写一些单元测试,需要从文件模拟命名导出,但当导出是返回承诺的函数时,它似乎不起作用。
例如,我有一个位于 lib/api.js 的模块,它导出一个名为 getFoo 的函数,该函数本身返回 axios.get('/foo')。
在我的测试中,我想将getFoo 的返回值模拟为带有 foo 有效负载的已解析承诺,因此我使用模块工厂进行模拟:
import * as api from 'lib/api';
jest.mock('lib/api', () => ({
getFoo: jest.fn(() => Promise.resolve('foo')),
});
然后在我的测试中,我想断言 getFoo 是作为更大的链式 Promise 过程的一部分调用的。
it('getBar should call getFoo', () => {
return getBar().then(() => {
expect(api.getFoo).toHaveBeenCalled();
});
})
我在这里得到的错误是cannot read property 'then' of undefined,好像在模拟模块工厂中使用的 jest 函数没有返回承诺。
如果getFoo 的返回值不是一个承诺,它会按预期工作。如果我使用jest.fn() 作为getFoo 的值,然后在测试本身模拟实现...
it('getBar should call getFoo', () => {
api.getFoo.mockImplementation(() => Promise.resolve("foo"));
return getBar().then(() => {
expect(api.getFoo).toHaveBeenCalled();
});
})
...然后就可以了。我的理解是 jest.fn(() => {}) 应该只是定义实现的一种更短的方式,所以我不确定为什么会出现错误。任何帮助表示赞赏。
谢谢。
更多信息:
lib/api 看起来像这样...
import axios from 'axios';
export const getFoo = (host: string, accessToken: string): Promise<{}> =>
axios.get(`${host}/foo`, {
headers: {
Authorization: `Bearer ${accessToken}`,
'Content-Type': 'application/json',
}
})
// return foo
.then((response) => response.data)
.catch(() => {
throw new Error('Failed to load foo');
});
当我在测试运行时控制台记录模拟的 getFoo 函数时,它显示为...
getFoo function mockConstructor() {
return fn.apply(this, arguments); }
【问题讨论】:
标签: javascript unit-testing jestjs