【发布时间】:2017-06-23 23:45:11
【问题描述】:
我有这个代码:
import * as a from 'a-a';
jest.mock('a-a');
describe('a-a', () => {
beforeAll(async () => {
const x = await a.x(1); // Calls the mock
console.log(x); // 1
console.log(a.x.mock) // Undefined
});
});
模拟函数是:
export async function x(data) {
cache.push(data);
console.log('HERE'); // this is printed
return data;
}
模块的mock在__mocks__目录下。
a.x() 调用模拟函数,但 a.x.mock 未定义。
这怎么可能? .mock 属性在哪里?
【问题讨论】:
-
登录
a.x会得到什么 -
@AndreasKöberle
[Function: x]。也许相关,x是async -
这样模拟它是否是一种解决方案:
jest.mock('a-a', () => ({x: jest.fn()}))。写的有点多,但至少这会奏效。 -
我也想知道您的示例是如何工作的,因为我认为它会使用返回
undefined的函数来模拟模块。 -
@AndreasKöberle 我更喜欢 automock。没有模拟函数实际上返回一些东西,我更新了问题
标签: jestjs automocking