You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to use toHaveBeenCalledBefore (or ...After) with the result of a jest.fn(). However, I get a typescript error.
declareconst myFn: (argument: string)=>void;expect(jest.fn()).toHaveBeenCalledAfter(jest.fn(myFn));// ----- error is here -----------------^^^^^^^^^^^^^
This results in an error:
Argument of type 'Mock<void, [argument: string]>' is not assignable to parameter of type 'MockInstance<unknown, unknown[]>'.
The types returned by 'getMockImplementation()' are incompatible between these types.
Type '((argument: string) => void) | undefined' is not assignable to type '((...args: unknown[]) => unknown) | undefined'.
Type '(argument: string) => void' is not assignable to type '(...args: unknown[]) => unknown'.
Types of parameters 'argument' and 'args' are incompatible.
Type 'unknown' is not assignable to type 'string'.(2345)
Here's a typescript playground link that basically reproduces the issue: playground link
I think the problem was introduced in #477 where the argument of toHaveBeenCalledAfter was changed from jest.MockInstance<any, any[]> to jest.MockInstance<unknown, unknown[]>.
If I change it back to the anys in my playground, I don't get the error.
Workaround
I fixed it temporarily in our project by creating my own .d.ts file with the following content.
However, it would be great if the library could fix it.
test('toHaveBeenCalledBefore and toHaveBeenCalledAfter empty',()=>{constmock=jest.fn();expect(mock).not.toHaveBeenCalledAfter(mock);expect(mock).not.toHaveBeenCalledBefore(mock);});
But this does not
test('toHaveBeenCalledBefore and toHaveBeenCalledAfter with mockFunction',()=>{constmyFn=(arg: string)=>void{};expect(jest.fn()).not.toHaveBeenCalledAfter(jest.fn(myFn));expect(jest.fn()).not.toHaveBeenCalledBefore(jest.fn(myFn));});
Bug
package
version: 3.2.3node
version: 18.13.0npm
(oryarn
) version: 8.19.3I want to use
toHaveBeenCalledBefore
(or...After
) with the result of ajest.fn()
. However, I get a typescript error.This results in an error:
Here's a typescript playground link that basically reproduces the issue: playground link
I think the problem was introduced in #477 where the argument of
toHaveBeenCalledAfter
was changed fromjest.MockInstance<any, any[]>
tojest.MockInstance<unknown, unknown[]>
.If I change it back to the
any
s in my playground, I don't get the error.Workaround
I fixed it temporarily in our project by creating my own
.d.ts
file with the following content.However, it would be great if the library could fix it.
The text was updated successfully, but these errors were encountered: