ts-jest 提供模拟测试和模块的不同实例

ts-jest providing different instances of a mock the test and the module

我正在使用导出函数的文件的自动模拟,实际文件看起来像

export const serialize = (type:boolean) => (data) => {....}

在调试器中,我可以看到在我正在测试的模块(控制器)中,serialize 被调用并且是一个模拟。

在我的测试中,我有

import {controller} from '../../src/controller'
import {serialize} from '../../src/serialize'
jest.mock('../../src/serialize');

describe('test', () => {
    it('serialize'), (done) => { 
       controller.put();  //<-- calls serialize
       expect(serialize).toHaveBeenCalled();  // <-- this fails, with 0 calls
    }
})

问题在于,在 Controller 中,序列化函数的调用方式导致存储在 WeakMap 中的 mockConfig in jest 从弱映射中移除。因此,我移动了调用,以便 GC 确实收集了参考。