Skip to content
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

Documentation: guide for using jest.mock #8543

Closed
sargalias opened this issue Jun 9, 2019 · 6 comments
Closed

Documentation: guide for using jest.mock #8543

sargalias opened this issue Jun 9, 2019 · 6 comments

Comments

@sargalias
Copy link
Contributor

🚀 Feature Proposal

A guide on basic mocking with jest using jest.mock. The guide would be on the sidebar after "Timer mocks".

It would explain how to use jest.mock properly, what the different parameters do and provide examples for using them.

Motivation

When I was a beginner I didn't understand how to use jest.mockat all, particularly the second parameter of jest.mock. This was even more fiddly because I use ES6 modules, giving the default export another "gotcha" when used in tests.

As this is quite an essential feature for unit testing I think a guide will be beneficial to many users.

Please let me know your thoughts. If it sounds good I can make a PR.

@jeysal
Copy link
Contributor

jeysal commented Jun 9, 2019

Hey @sargalias, thanks for your suggestion. This sounds a lot like the "Manual Mocks" guide, maybe it would make sense to extend that guide - I agree that more info on the signature of jest.mock and on default exports would be great!

@sargalias
Copy link
Contributor Author

Hey @jeysal, that sounds like a good idea. I'll whip something up.

@sargalias
Copy link
Contributor Author

Hi @jeysal, based on our conversation in #8544 I think it might be a better idea to proceed differently.

As a recap, in #8544 it was suggested that it's fine for potentially missing information to be added to the API docs, but the guides should ideally only show best practices.

If we consider manual mocks to be the preferred practice, then it may be better to add an example to the API docs for jest.mock factory instead. It would show how to use it with ES6 modules and it would be short.

Please let me know if that sounds good. I'm happy to create a PR.

@jeysal
Copy link
Contributor

jeysal commented Jun 10, 2019

The second jest.mock argument and mocking default exports don't seem like bad practices though :)
I think both the guide and the API docs are legitimate places to add the information, fine with either or both 😄

@sargalias
Copy link
Contributor Author

Sounds good, thanks :). I've opened a small PR with just the addition to the jest.mock API docs for now. I wasn't too sure where to place a "Mocking without manual mocks" section in the "Manual Mocks" page 😅. #8550

sargalias added a commit to sargalias/jest that referenced this issue Jun 10, 2019
sargalias added a commit to sargalias/jest that referenced this issue Jun 11, 2019
sargalias added a commit to sargalias/jest that referenced this issue Jun 11, 2019
thymikee pushed a commit that referenced this issue Jun 12, 2019
* docs(JestObjectAPI jest-mock): Add example for ES6 modules

Issue #8543

* chore(CHANGELOG): Update CHANGELOG

* docs(JestObjectAPI jest-mock): Add explanation for __esModule: true

Issue #8543

* docs(JestObjectAPI jest-mock): Update versioned_docs

Issue #8543

* Revert "docs(JestObjectAPI jest-mock): Update versioned_docs"

This reverts commit ba17c08.

Updating the versioned_docs for version 24.6 is not necessary as we can
update an earlier version and the changes will propagate to the new
version.

* docs(JestObjectAPI jest-mock): Update previous versioned_docs

The example for using __esModule: true is also relevant for those
versions, so it should be present.
@github-actions
Copy link

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.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants