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(ES6ClassMocks): add clarity for module factory limitations #12453

Merged
merged 8 commits into from Feb 22, 2022

Conversation

john-u
Copy link
Contributor

@john-u john-u commented Feb 21, 2022

Summary

Modify verbiage and include another code example to make it clear that there is a language-level limitation outside of Jest when using the module factory pattern.

@john-u john-u marked this pull request as ready for review February 21, 2022 20:06
@john-u
Copy link
Contributor Author

john-u commented Feb 21, 2022

Should I also update jest-website-v1?

@Biki-das
Copy link
Contributor

Should I also update jest-website-v1?

no this is good, thanks

@@ -140,7 +140,11 @@ jest.mock('./sound-player', () => {
});
```

A limitation with the factory parameter is that, since calls to `jest.mock()` are hoisted to the top of the file, it's not possible to first define a variable and then use it in the factory. An exception is made for variables that start with the word 'mock'. It's up to you to guarantee that they will be initialized on time! For example, the following will throw an out-of-scope error due to the use of 'fake' instead of 'mock' in the variable declaration:
:::caution
Since calls to `jest.mock()` are hoisted to the top of the file, Jest prevents access to out-of-scope variables. By default, you cannot first define a variable and then use it in the factory. Jest will disable this check for variables that start with the word 'mock'. However, it is still up to you to guarantee that they will be initialized on time. Be aware of [Temporal Dead Zone](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let#temporal_dead_zone_tdz).
Copy link
Member

Choose a reason for hiding this comment

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

temporal dead zone, I didn't know this had a name! 😀 Awesome stuff

docs/Es6ClassMocks.md Outdated Show resolved Hide resolved
Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

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

thank you, this is awesome!

docs/Es6ClassMocks.md Outdated Show resolved Hide resolved
website/versioned_docs/version-25.x/Es6ClassMocks.md Outdated Show resolved Hide resolved
website/versioned_docs/version-25.x/Es6ClassMocks.md Outdated Show resolved Hide resolved
website/versioned_docs/version-26.x/Es6ClassMocks.md Outdated Show resolved Hide resolved
website/versioned_docs/version-27.0/Es6ClassMocks.md Outdated Show resolved Hide resolved
website/versioned_docs/version-27.5/Es6ClassMocks.md Outdated Show resolved Hide resolved
website/versioned_docs/version-27.4/Es6ClassMocks.md Outdated Show resolved Hide resolved
website/versioned_docs/version-27.2/Es6ClassMocks.md Outdated Show resolved Hide resolved
website/versioned_docs/version-27.1/Es6ClassMocks.md Outdated Show resolved Hide resolved
website/versioned_docs/version-27.0/Es6ClassMocks.md Outdated Show resolved Hide resolved
@SimenB SimenB merged commit 34308ae into jestjs:main Feb 22, 2022
@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 Mar 25, 2022
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.

[Bug]: mock factory hoisting fails with 'mock' prepended variables
4 participants