-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
feat(@jest/globals, jest-mock): add jest.Spied*
utility types
#13440
feat(@jest/globals, jest-mock): add jest.Spied*
utility types
#13440
Conversation
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ups.. This change should not be here. My bad (;
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now I am happy about the example. The point was to show that typings can be useful in some test util.
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.
nice!
/** | ||
* 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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? (;
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.
Definitely separate 👍
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/jest
providesjest.SpyInstance<T>
andjest.SpiedFunction<T>
utility types. Jest currently does not exposed these on thejest
namespace.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 ofstring
ornumber
not a function signature. Hence user will have to usejest.SpiedGetter<T>
orjest.SpiedSetter<T>
.Test plan
Type test added.