-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Add docs for using mocks in TypeScript #10415
Conversation
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.
Thanks for taking the time to write this! However, I don't think we need a completely new page for this - could you add it as a section to our mock docs?
https://jestjs.io/docs/en/mock-function-api
(also run prettier
on the file 🙂)
I've moved the TypeScript docs into the Mock Functions API page. |
Co-authored-by: Simen Bekkhus <sbekkhus91@gmail.com>
These techniques are solid. 👍🏻 Other cases which I found in my current codebase, full module mocking: jest.mock("./accounts")
import * as accounts from "./accounts"
const accountsMock = accounts as jest.Mocked<typeof accounts> and maybe not worth the mention, but ts-jest also does includes some types:
Which @alloy seems to prefer |
Yeah I used to do the same as this PR and @orta suggest, but I recently found this https://github.com/kulshekhar/ts-jest/blob/master/docs/user/test-helpers.md |
I've had mixed success with this one, but, yes we should get that added in too. It was probably my bad types making it fail 😄
That looks cool. I'm definitely going to check that out. Thanks! |
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.
Thanks, this is great!
Could you backport the changes the the versioned docs? Helpers from v24 won't make sense for for the older docs etc, but in general all the versions should be as similar as possible 🙂
OK, I think I've updated the versioned docs properly. I did check the different versions and all seemed good 😅 |
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 wonderful, thank you so much!
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. |
Related to #10399.
Summary
I thought it might be useful to add some documentation to go alongside the updated TypeScript examples in #10399. I find there is a general lack of knowledge when it comes to using writing tests with TypeScript and leveraging types for better tests.
Test plan