0

我有这个代码:

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;
}

模块的模拟在__mocks__目录中。

a.x()调用模拟函数,但a.x.mock未定义。

这怎么可能?房产在哪里.mock

4

1 回答 1

0

所以,经过一番调查,我发现目录中声明的函数默认情况下__mocks__没有包装jest.fn()

我个人觉得这件事有点令人困惑。

所以你可以同时做

function x(data) {
    cache.push(data);

    return cache;
}

jest.mock('a-a', () => ({x: x}))

如果您在同一个文件中执行所有操作,或者

jest.mock('a-a');

然后在__mocks__/a-a.js文件中

export const x = jest.fn(async (data) => {
    cache.push(data);

    return cache;
});
于 2017-06-23T15:45:11.977 回答