-
-
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
docs(ES6ClassMocks): add clarity for module factory limitations #12453
Conversation
a4f125b
to
7a51d09
Compare
- close jestjs#11862 - related to jestjs#11455
7a51d09
to
133b50d
Compare
Should I also update |
no this is good, thanks |
docs/Es6ClassMocks.md
Outdated
@@ -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). |
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.
temporal dead zone, I didn't know this had a name! 😀 Awesome stuff
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.
thank you, this is awesome!
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. |
ReferenceError: Cannot access 'myMock' before initialization
if test itself and 'myMock' are in different files #11455Summary
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.