使用Typescript / ts-jest模拟嘲笑测试

时间:2020-10-13 03:41:43

标签: typescript unit-testing jestjs

我一直试图像这样在打字稿/笑话测试文件中模拟Got module

import { mocked } from 'ts-jest/utils'
import got from 'got'
import { getDom } from '../src/my-module'

jest.mock('got')
const mockedGot = mocked(got)

describe('getDom', () => {
  test('gets html from url', async () => {
    mockedGot.mockResolvedValue({
      text() {
        return 'some html'
      }
    })
    await getDom('foo')

    expect(mockedGot).toHaveBeenCalledWith('foo')
  })
})

这是使用Got的模块:

import got from 'got'
import cheerio from 'cheerio'

export async function getDom(url: string): Promise<cheerio.Root> {
  const html = await got(url).text()
  return cheerio.load(html)
}

但是我收到来自Typescript的以下投诉。 Argument of type [whatever I put as a resolve value] is not assignable to parameter of type 'never'

这是我在VSCode中看到的内容:

Typescript Warning

我已经看到很多答案,可以在打字稿中显示axios示例,但我不知道自己在做什么错。

我该如何解决这个模拟问题?

0 个答案:

没有答案