feat(@jest/globals, jest-mock): add jest.Spied* utility types#13440
feat(@jest/globals, jest-mock): add jest.Spied* utility types#13440SimenB merged 14 commits intojestjs:mainfrom mrazauskas:feat-add-jest.Spied-type-utils
jest.Spied* utility types#13440Conversation
docs/JestObjectAPI.md
Outdated
| const utils = jest.createMockFromModule('../utils'); | ||
|
|
||
| utils.isAuthorized = jest.fn(secret => secret === 'not wizard'); | ||
| jest | ||
| .spyOn(utils, 'isAuthorized') | ||
| .mockImplementation(secret => secret === 'not wizard'); |
There was a problem hiding this comment.
it doesn't make sense to spyOn here - isAuthorized is already a mock function (that's what createMockFromModule does)
There was a problem hiding this comment.
Ups.. This change should not be here. My bad (;
There was a problem hiding this comment.
instead of utils.isAuthorized = jest.fn(secret => secret === 'not wizard'); it should probably be utils.isAuthorized.mockImplementation(secret => secret === 'not wizard');
There was a problem hiding this comment.
These lines got autocorrected, because I was playing with jest/prefer-spy-on ESLint rule. I was hoping to find better example for docs, but nothing came out. jest/prefer-spy-on rule will come in later as separate PR (;
|
|
||
| Constructs the type of a spied class or function (i.e. the return type of `jest.spyOn()`). | ||
|
|
||
| ```ts title="__utils__/setDateNow.ts" |
There was a problem hiding this comment.
Now I am happy about the example. The point was to show that typings can be useful in some test util.
| /** | ||
| * Constructs the type of a spied class or function. | ||
| */ | ||
| export type Spied<T extends ClassLike | FunctionLike> = JestSpied<T>; |
There was a problem hiding this comment.
this is copied manually, right? Should we set up a script which keeps it in sync with @jest/globals?
There was a problem hiding this comment.
Would be possible, but is it worth it? I mean, these types are only used in Jest repo. If something will be missing, typechecks of tests will catch it for us.
Perhaps some check that these types are still exposed in @jest/globals? Just another type test? Sound simple to do and to run automatically. Together with typechecks of test files that should be enough. I will do this as separate PR. If that’s fine? (;
Co-authored-by: Simen Bekkhus <sbekkhus91@gmail.com>
|
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Summary
@types/jestprovidesjest.SpyInstance<T>andjest.SpiedFunction<T>utility types. Jest currently does not exposed these on thejestnamespace.Note that
jest.SpyInstance<T>works only on methods, but can’t be used on class constructors, getters and setters. It is equal tojest.SpiedFunction<T>only thatjest.SpyInstance<T>takes two type arguments instead of one.To cover more use cases, I created
jest.SpiedClass<T>,jest.SpiedFunction<T>,jest.SpiedGetter<T>,jest.SpiedSetter<T>. Names are similar tojest.Mocked*utilities.jest.Mocked<T>is very smart type. I wanted to have somewhat similarjest.Spied<T>. It works only with classes and functions, because there is no way to differentiate between a getter and a setter. I mean, either of them would simply have type ofstringornumbernot a function signature. Hence user will have to usejest.SpiedGetter<T>orjest.SpiedSetter<T>.Test plan
Type test added.