首页 > 解决方案 > TypeScript:Jest 测试中的类型上不存在属性“mockImplementation”

问题描述

我基本上有这个:

// Example.ts

class Example {
  returnSomething() {
    return false
  }
}

export default new Example()

然后在 TypeScript Jest 测试中我有这个:

// Example/__tests__/index.test.ts

jest.mock('Example', () => ({
  _esModule: true,
  returnSomething: jest.fn(() => true)
  isFeatureEnabled: jest.fn(),
}))

// then down below:
it('some test', () => {
  Example.returnSomething.mockImplementation(() => 'foo')
})

但我收到了这个错误:

Property 'mockImplementation' does not exist on type ...

对于这个测试,如何让函数的开玩笑包装的模拟在 TypeScript 中正常工作?

还有什么比这更好的吗?

(Example.returnSomething as jest.Mock).mockImplementation(() => 'foo')

标签: typescriptjestjs

解决方案


推荐阅读