Mocking Got in Jest test using Typescript / ts-jest

Viewed 585

I've been trying to mock the Got module in a typescript/jest test file like so:

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

And here's the module using 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)
}

But I get the following complaint from Typescript. Argument of type [whatever I put as a resolve value] is not assignable to parameter of type 'never'

Here's what I'm seeing in VSCode:

Typescript Warning

I've seen a lot of answers showing the axios example in typescript and I can't figure out what I'm doing wrong.

How can I make this mock resolve to whatever I want it to?

0 Answers
Related