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
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -53,6 +53,7 @@
- `[jest-transform]` Update `write-file-atomic` to v4 ([#12357](https://github.com/facebook/jest/pull/12357))
- `[jest-types]` [**BREAKING**] Remove `Config.Glob` and `Config.Path` ([#12406](https://github.com/facebook/jest/pull/12406))
- `[jest]` Use `index.ts` instead of `jest.ts` as main export ([#12329](https://github.com/facebook/jest/pull/12329))
- `[docs, website]` Add clarity for module factory limitations ([#12453](https://github.com/facebook/jest/pull/12453))

### Performance

Expand Down
19 changes: 18 additions & 1 deletion docs/Es6ClassMocks.md
Expand Up @@ -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

SimenB marked this conversation as resolved.
Show resolved Hide resolved
:::

For example, the following will throw an out-of-scope error due to the use of 'fake' instead of 'mock' in the variable declaration.

```javascript
// Note: this will fail
Expand All @@ -153,6 +157,19 @@ jest.mock('./sound-player', () => {
});
```

The following will throw a ReferenceError despite using 'mock' in the variable declaration, as the mockSoundPlayer is not wrapped in an arrow function and thus accessed before initialization after hoisting.
SimenB marked this conversation as resolved.
Show resolved Hide resolved

```javascript
import SoundPlayer from './sound-player';
const mockSoundPlayer = jest.fn().mockImplementation(() => {
return {playSoundFile: mockPlaySoundFile};
});
// results in a ReferenceError
jest.mock('./sound-player', () => {
return mockSoundPlayer;
});
```

### Replacing the mock using [`mockImplementation()`](MockFunctionAPI.md#mockfnmockimplementationfn) or [`mockImplementationOnce()`](MockFunctionAPI.md#mockfnmockimplementationoncefn)

You can replace all of the above mocks in order to change the implementation, for a single test or all tests, by calling `mockImplementation()` on the existing mock.
Expand Down
19 changes: 18 additions & 1 deletion website/versioned_docs/version-25.x/Es6ClassMocks.md
Expand Up @@ -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).
SimenB marked this conversation as resolved.
Show resolved Hide resolved
:::

For example, the following will throw an out-of-scope error due to the use of 'fake' instead of 'mock' in the variable declaration.

```javascript
// Note: this will fail
Expand All @@ -153,6 +157,19 @@ jest.mock('./sound-player', () => {
});
```

The following will throw a ReferenceError despite using 'mock' in the variable declaration, as the mockSoundPlayer is not wrapped in an arrow function and thus accessed before initialization after hoisting.
SimenB marked this conversation as resolved.
Show resolved Hide resolved

```javascript
import SoundPlayer from './sound-player';
const mockSoundPlayer = jest.fn().mockImplementation(() => {
return {playSoundFile: mockPlaySoundFile};
});
// results in a ReferenceError
jest.mock('./sound-player', () => {
return mockSoundPlayer;
});
```

### Replacing the mock using [`mockImplementation()`](MockFunctionAPI.md#mockfnmockimplementationfn) or [`mockImplementationOnce()`](MockFunctionAPI.md#mockfnmockimplementationoncefn)

You can replace all of the above mocks in order to change the implementation, for a single test or all tests, by calling `mockImplementation()` on the existing mock.
Expand Down
19 changes: 18 additions & 1 deletion website/versioned_docs/version-26.x/Es6ClassMocks.md
Expand Up @@ -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).
SimenB marked this conversation as resolved.
Show resolved Hide resolved
:::

For example, the following will throw an out-of-scope error due to the use of 'fake' instead of 'mock' in the variable declaration.

```javascript
// Note: this will fail
Expand All @@ -153,6 +157,19 @@ jest.mock('./sound-player', () => {
});
```

The following will throw a ReferenceError despite using 'mock' in the variable declaration, as the mockSoundPlayer is not wrapped in an arrow function and thus accessed before initialization after hoisting.
SimenB marked this conversation as resolved.
Show resolved Hide resolved

```javascript
import SoundPlayer from './sound-player';
const mockSoundPlayer = jest.fn().mockImplementation(() => {
return {playSoundFile: mockPlaySoundFile};
});
// results in a ReferenceError
jest.mock('./sound-player', () => {
return mockSoundPlayer;
});
```

### Replacing the mock using [`mockImplementation()`](MockFunctionAPI.md#mockfnmockimplementationfn) or [`mockImplementationOnce()`](MockFunctionAPI.md#mockfnmockimplementationoncefn)

You can replace all of the above mocks in order to change the implementation, for a single test or all tests, by calling `mockImplementation()` on the existing mock.
Expand Down
19 changes: 18 additions & 1 deletion website/versioned_docs/version-27.0/Es6ClassMocks.md
Expand Up @@ -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).
SimenB marked this conversation as resolved.
Show resolved Hide resolved
:::

For example, the following will throw an out-of-scope error due to the use of 'fake' instead of 'mock' in the variable declaration.

```javascript
// Note: this will fail
Expand All @@ -153,6 +157,19 @@ jest.mock('./sound-player', () => {
});
```

The following will throw a ReferenceError despite using 'mock' in the variable declaration, as the mockSoundPlayer is not wrapped in an arrow function and thus accessed before initialization after hoisting.
SimenB marked this conversation as resolved.
Show resolved Hide resolved

```javascript
import SoundPlayer from './sound-player';
const mockSoundPlayer = jest.fn().mockImplementation(() => {
return {playSoundFile: mockPlaySoundFile};
});
// results in a ReferenceError
jest.mock('./sound-player', () => {
return mockSoundPlayer;
});
```

### Replacing the mock using [`mockImplementation()`](MockFunctionAPI.md#mockfnmockimplementationfn) or [`mockImplementationOnce()`](MockFunctionAPI.md#mockfnmockimplementationoncefn)

You can replace all of the above mocks in order to change the implementation, for a single test or all tests, by calling `mockImplementation()` on the existing mock.
Expand Down
19 changes: 18 additions & 1 deletion website/versioned_docs/version-27.1/Es6ClassMocks.md
Expand Up @@ -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).
SimenB marked this conversation as resolved.
Show resolved Hide resolved
:::

For example, the following will throw an out-of-scope error due to the use of 'fake' instead of 'mock' in the variable declaration.

```javascript
// Note: this will fail
Expand All @@ -153,6 +157,19 @@ jest.mock('./sound-player', () => {
});
```

The following will throw a ReferenceError despite using 'mock' in the variable declaration, as the mockSoundPlayer is not wrapped in an arrow function and thus accessed before initialization after hoisting.
SimenB marked this conversation as resolved.
Show resolved Hide resolved

```javascript
import SoundPlayer from './sound-player';
const mockSoundPlayer = jest.fn().mockImplementation(() => {
return {playSoundFile: mockPlaySoundFile};
});
// results in a ReferenceError
jest.mock('./sound-player', () => {
return mockSoundPlayer;
});
```

### Replacing the mock using [`mockImplementation()`](MockFunctionAPI.md#mockfnmockimplementationfn) or [`mockImplementationOnce()`](MockFunctionAPI.md#mockfnmockimplementationoncefn)

You can replace all of the above mocks in order to change the implementation, for a single test or all tests, by calling `mockImplementation()` on the existing mock.
Expand Down
19 changes: 18 additions & 1 deletion website/versioned_docs/version-27.2/Es6ClassMocks.md
Expand Up @@ -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).
SimenB marked this conversation as resolved.
Show resolved Hide resolved
:::

For example, the following will throw an out-of-scope error due to the use of 'fake' instead of 'mock' in the variable declaration.

```javascript
// Note: this will fail
Expand All @@ -153,6 +157,19 @@ jest.mock('./sound-player', () => {
});
```

The following will throw a ReferenceError despite using 'mock' in the variable declaration, as the mockSoundPlayer is not wrapped in an arrow function and thus accessed before initialization after hoisting.
SimenB marked this conversation as resolved.
Show resolved Hide resolved

```javascript
import SoundPlayer from './sound-player';
const mockSoundPlayer = jest.fn().mockImplementation(() => {
return {playSoundFile: mockPlaySoundFile};
});
// results in a ReferenceError
jest.mock('./sound-player', () => {
return mockSoundPlayer;
});
```

### Replacing the mock using [`mockImplementation()`](MockFunctionAPI.md#mockfnmockimplementationfn) or [`mockImplementationOnce()`](MockFunctionAPI.md#mockfnmockimplementationoncefn)

You can replace all of the above mocks in order to change the implementation, for a single test or all tests, by calling `mockImplementation()` on the existing mock.
Expand Down
19 changes: 18 additions & 1 deletion website/versioned_docs/version-27.4/Es6ClassMocks.md
Expand Up @@ -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).
SimenB marked this conversation as resolved.
Show resolved Hide resolved
:::

For example, the following will throw an out-of-scope error due to the use of 'fake' instead of 'mock' in the variable declaration.

```javascript
// Note: this will fail
Expand All @@ -153,6 +157,19 @@ jest.mock('./sound-player', () => {
});
```

The following will throw a ReferenceError despite using 'mock' in the variable declaration, as the mockSoundPlayer is not wrapped in an arrow function and thus accessed before initialization after hoisting.
SimenB marked this conversation as resolved.
Show resolved Hide resolved

```javascript
import SoundPlayer from './sound-player';
const mockSoundPlayer = jest.fn().mockImplementation(() => {
return {playSoundFile: mockPlaySoundFile};
});
// results in a ReferenceError
jest.mock('./sound-player', () => {
return mockSoundPlayer;
});
```

### Replacing the mock using [`mockImplementation()`](MockFunctionAPI.md#mockfnmockimplementationfn) or [`mockImplementationOnce()`](MockFunctionAPI.md#mockfnmockimplementationoncefn)

You can replace all of the above mocks in order to change the implementation, for a single test or all tests, by calling `mockImplementation()` on the existing mock.
Expand Down
19 changes: 18 additions & 1 deletion website/versioned_docs/version-27.5/Es6ClassMocks.md
Expand Up @@ -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).
SimenB marked this conversation as resolved.
Show resolved Hide resolved
:::

For example, the following will throw an out-of-scope error due to the use of 'fake' instead of 'mock' in the variable declaration.

```javascript
// Note: this will fail
Expand All @@ -153,6 +157,19 @@ jest.mock('./sound-player', () => {
});
```

The following will throw a ReferenceError despite using 'mock' in the variable declaration, as the mockSoundPlayer is not wrapped in an arrow function and thus accessed before initialization after hoisting.
SimenB marked this conversation as resolved.
Show resolved Hide resolved

```javascript
import SoundPlayer from './sound-player';
const mockSoundPlayer = jest.fn().mockImplementation(() => {
return {playSoundFile: mockPlaySoundFile};
});
// results in a ReferenceError
jest.mock('./sound-player', () => {
return mockSoundPlayer;
});
```

### Replacing the mock using [`mockImplementation()`](MockFunctionAPI.md#mockfnmockimplementationfn) or [`mockImplementationOnce()`](MockFunctionAPI.md#mockfnmockimplementationoncefn)

You can replace all of the above mocks in order to change the implementation, for a single test or all tests, by calling `mockImplementation()` on the existing mock.
Expand Down