-
-
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
spyOn getter requires explicit type on getter after Jest 24 #8137
Comments
This isn't an error with jest, this is a type error from typescript. See |
@SimenB It seems like this issue was introduced by this commit on Feb 10, which introduced the NonFunctionPropertyNames type to spyOn in jest-mock: This was working fine in Jest 23 and only occurs when returning a jest.fn(); I don't understand why that would not satisfy the new condition ((...args: any[]) => any) unless there is some foo surrounding the jest.fn() object If you can please help me understand a bit better why this condition might be failing, I can file an issue with TS. |
What you're getting is a type error. While Jest has indeed migrated to TS internally, we still don't distribute our types (it's probably confusing since we copied a lot of the types in However, if it's undesirable (I don't know) we should definitely change the types in Jest itself as well. I can reopen to track this, but there shouldn't really be a code change here until it's at least been discussed in the context of |
I too am running into this error. Rather I have a type on the method or not I get the same errors. I'm on Jest and "@types/jest": "^24.0.18", and "jest": "^24.9.0" in an Angular environment. Argument of type '"config"' is not assignable to parameter of type '"prototype"' |
After upgrading my nestjs project, I got similar error like this in jest testing codes. Jest version is 26.6.3. jest.spyOn(model, 'create').mockResolvedValue(toReturned);
~~~~~~~~~~
src/post/post.service.spec.ts:281:58 - error TS2345: Argument of type 'any' is not assignable to parameter of type 'never'.
281 jest.spyOn(commentModel, 'create').mockResolvedValue({
~
282 ...comment,
~~~~~~~~~~~~~~~~~
283 post: { _id: 'test' },
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
284 } as any); The |
I use PS: Mongoose 5.11 introduce its own types instead of the |
This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days. |
This issue was closed because it has been stalled for 7 days with no activity. Please open a new issue if the issue is still relevant, linking to this one. |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
🐛 Bug Report
I am trying to upgrade our repos to jest 24 and encountered a compilation issue with spying on a mocked getter which worked in jest 23. The invocation is:
... and the error is:
I can rectify the situation by explicitly setting the return type of the mock function, but it doesn't seem like that should be necessary.
To Reproduce
Steps to reproduce the behavior:
Does not work:
Does not work:
Works:
Works:
Expected behavior
A jest.fn() being returned does not appear to satisfy the condition
[K in keyof T]: T[K] extends (...args: any[]) => any
in NonFunctionPropertyNamesIf it is necessary to explicitly cast jest.fn() return from mocks, this should be reflected in the documentation.
Run
npx envinfo --preset jest
Paste the results here:
The text was updated successfully, but these errors were encountered: