【问题标题】:ts-jest providing different instances of a mock the test and the modulets-jest 提供模拟测试和模块的不同实例
【发布时间】:2020-05-30 16:20:25
【问题描述】:

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

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

【问题讨论】:

    标签: mocking jestjs ts-jest


    【解决方案1】:

    问题在于,在 Controller 中,调用 serialize 函数的方式导致存储在 WeakMap 中的 mockConfig 开玩笑地从弱映射中删除。因此我移动了调用,因此 GC 确实收集了引用。

    【讨论】:

      猜你喜欢
      • 2021-09-07
      • 2021-08-27
      • 1970-01-01
      • 2018-06-21
      • 1970-01-01
      • 2017-02-23
      • 1970-01-01
      • 1970-01-01
      • 2017-10-23
      相关资源
      最近更新 更多