-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace usage of Jest globals with @jest/globals package #119
Conversation
b840575
to
da688eb
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great! 👏
I think this would also help with bun compatibility since it rewrites references to |
Hey @marchaos, can we somehow help to move this PR along? Is there something still missing for you? |
Hey, I'll take a look at this now. |
src/Mock.ts
Outdated
export interface CalledWithMock<T, Y extends any[]> extends jest.Mock<T, Y> { | ||
calledWith: (...args: Y | MatchersOrLiterals<Y>) => jest.Mock<T, Y>; | ||
export interface CalledWithMock<T extends FunctionLike> extends jest.Mock<T> { | ||
calledWith: (...args: [...Parameters<T>] | MatchersOrLiterals<[...Parameters<T>]>) => jest.Mock<T>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this change here is why the 2 // @ts-expect-error are required in Mock.spec.ts, and I suspect that is a breaking change.
Just seeing if I can improve this to avoid that.
…pes. Change ts-ignore in favour of ts-expect-error
I've pushed some changes:
The changed to I ran this branch against a private code base that uses Would be good for someone to take a look at my changes, and potentially try this branch out for themselves? I can release a beta (4.0.0-beta) to npm of that makes it easier? |
export interface CalledWithMock<T, Y extends any[]> extends jest.Mock<T, Y> { | ||
calledWith: (...args: Y | MatchersOrLiterals<Y>) => jest.Mock<T, Y>; | ||
export interface CalledWithMock<T extends FunctionLike> extends jest.Mock<T> { | ||
calledWith: (...args: [...MatchersOrLiterals<Parameters<T>>]) => jest.Mock<T>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed this to just MatchersOrLiterals
since that type will check literals for us - the original type for args was Y | MatchersOrLiterals<Y>
, which really could have just been MatchersOrLiterals<Y>
I've just tested this branch in one of my projects (which admittedly only uses |
I've pushed a version to npm |
Gave it a try. Had an additional type error
but I think it's actually correct/the problem is with the other types. Otherwise seems good. |
Attempts to fix #116
Removed
@types/jest
package.Added
@jest/globals
as peer dependency. I'm not sure about which versions to support, but Jest 28 introduced a breaking change injest.Mock
type definition so I'm not confident how it would work with earlier versions.Note that this definition is different between
@types/jest@29
and@jest/globals@29
The change is described in jestjs/jest#12479
tl;dr the generic type is changed from
jest.fn<ReturnType<MyFunctionType>, Parameters<MyFunctionType>>
tojest.fn<MyFunctionType>
I did my best to fix the new type errors in the library but I had to add a couple of
// @ts-expect-error
comments in the process.The new types work across internal tests and when used as a dependency in our own repo but I'm not sure if they are valid for every existing use case for this library.