How can I set correctly types on jest.spyOn?

Viewed 604

I have written the following on my typescript jest tests:

beforeEach(() => {
    jest
      .spyOn<MyParentClass, any>(MyChildClass.prototype, "doRequest")
      .mockImplementation(async function (this:MyChildClass) {
              /// do my mock
      }  as () => never);

The part as () => never was added to skip type checker error on the async saying that

Argument of type '(this:MyChildClass) => Promise<any>' is not assignable to parameter of type '() => unknown'

I don't find a way on how to improve the following, narrowing any to a more precise type : .spyOn<MyParentClass, any>

By definition

function jest.spyOn<T extends {}, M extends jest.NonFunctionPropertyNames<Required<T>>>(object: T, method: M, accessType: "get"): jest.SpyInstance<Required<T>[M], []> (+3 overloads)

I tried other types that might fit but no success...

Tests are running successfully and I'm able to spyOn and mock, just wonder how to write a better typing def.

0 Answers
Related