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

docs(jest.doMock): Add information for using ES6 modules with doMock #8573

Merged

Conversation

sargalias
Copy link
Contributor

Summary

Currently there is no information on how to use jest.doMock() with ES6 modules. Doing that requires a few additional steps that are non-intuitive.

In this PR I expanded on the jest.doMock() API documentation to solve that problem.

Test plan

The example code provided passes tests:

example-code

terminal-output

Please let me know if there is anything you'd like me to improve or if the PR isn't suitable.

@sargalias sargalias marked this pull request as ready for review June 17, 2019 20:42
Copy link
Contributor

@jeysal jeysal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice, thanks!

docs/JestObjectAPI.md Outdated Show resolved Hide resolved
@jeysal jeysal requested review from SimenB and thymikee June 17, 2019 22:35
@@ -314,6 +314,46 @@ test('moduleName 2', () => {
});
```

Using `jest.doMock()` with ES6 modules requires additional steps:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it doesn't. Using jest.doMock with ES6 imports does. Please clarify this, because people may get wrong idea that they need to install some babel plugin to actually test a module.

This should clearly state something like: "use if you don't want to require your ES6 modules"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, changed.

foo: 'foo1',
};
});
import('../moduleName').then(moduleName => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please either use async/await or return this promise from test – otherwise it's not gonna work properly. Same for the test below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes you're correct. Surprisingly these worked for me anyway (i.e. when changing the value in toEqual the test failed), possibly some implementation detail of mocking and Babel.

Changed.

The steps outlined in jest.doMock for ES6 imports are only necessary if
you don't want to use `require` in your tests.
Copy link
Contributor

@jeysal jeysal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM :)

@jeysal jeysal requested a review from thymikee June 18, 2019 21:16
@github-actions
Copy link

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.
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

Successfully merging this pull request may close these issues.

4 participants