Skip to content

Commit 18dc88e

Browse files
authored
chore(docs): clarify timer-mocks page (#12241)
1 parent b2db6cf commit 18dc88e

File tree

7 files changed

+38
-13
lines changed

7 files changed

+38
-13
lines changed

docs/TimerMocks.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ test('do something with real timers', () => {
5858
Another test we might want to write for this module is one that asserts that the callback is called after 1 second. To do this, we're going to use Jest's timer control APIs to fast-forward time right in the middle of the test:
5959

6060
```javascript
61+
jest.useFakeTimers();
6162
test('calls the callback after 1 second', () => {
6263
const timerGame = require('../timerGame');
6364
const callback = jest.fn();
@@ -150,7 +151,8 @@ function timerGame(callback) {
150151
module.exports = timerGame;
151152
```
152153

153-
```javascript
154+
```javascript title="__tests__/timerGame-test.js"
155+
jest.useFakeTimers();
154156
it('calls the callback after 1 second via advanceTimersByTime', () => {
155157
const timerGame = require('../timerGame');
156158
const callback = jest.fn();

website/versioned_docs/version-25.x/TimerMocks.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module.exports = timerGame;
2222
```javascript title="__tests__/timerGame-test.js"
2323
'use strict';
2424

25-
jest.useFakeTimers();
25+
jest.useFakeTimers(); // or you can set "timers": "fake" globally in configuration file
2626

2727
test('waits 1 second before ending the game', () => {
2828
const timerGame = require('../timerGame');
@@ -35,11 +35,14 @@ test('waits 1 second before ending the game', () => {
3535

3636
Here we enable fake timers by calling `jest.useFakeTimers();`. This mocks out setTimeout and other timer functions with mock functions. If running multiple tests inside of one file or describe block, `jest.useFakeTimers();` can be called before each test manually or with a setup function such as `beforeEach`. Not doing so will result in the internal usage counter not being reset.
3737

38+
All of the following functions need fake timers to be set, either by `jest.useFakeTimers()` or via `"timers": "fake"` in the config file.
39+
3840
## Run All Timers
3941

4042
Another test we might want to write for this module is one that asserts that the callback is called after 1 second. To do this, we're going to use Jest's timer control APIs to fast-forward time right in the middle of the test:
4143

4244
```javascript
45+
jest.useFakeTimers();
4346
test('calls the callback after 1 second', () => {
4447
const timerGame = require('../timerGame');
4548
const callback = jest.fn();
@@ -134,7 +137,8 @@ function timerGame(callback) {
134137
module.exports = timerGame;
135138
```
136139

137-
```javascript
140+
```javascript title="__tests__/timerGame-test.js"
141+
jest.useFakeTimers();
138142
it('calls the callback after 1 second via advanceTimersByTime', () => {
139143
const timerGame = require('../timerGame');
140144
const callback = jest.fn();

website/versioned_docs/version-26.x/TimerMocks.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module.exports = timerGame;
2222
```javascript title="__tests__/timerGame-test.js"
2323
'use strict';
2424

25-
jest.useFakeTimers();
25+
jest.useFakeTimers(); // or you can set "timers": "fake" globally in configuration file
2626

2727
test('waits 1 second before ending the game', () => {
2828
const timerGame = require('../timerGame');
@@ -35,11 +35,14 @@ test('waits 1 second before ending the game', () => {
3535

3636
Here we enable fake timers by calling `jest.useFakeTimers();`. This mocks out setTimeout and other timer functions with mock functions. If running multiple tests inside of one file or describe block, `jest.useFakeTimers();` can be called before each test manually or with a setup function such as `beforeEach`. Not doing so will result in the internal usage counter not being reset.
3737

38+
All of the following functions need fake timers to be set, either by `jest.useFakeTimers()` or via `"timers": "fake"` in the config file.
39+
3840
## Run All Timers
3941

4042
Another test we might want to write for this module is one that asserts that the callback is called after 1 second. To do this, we're going to use Jest's timer control APIs to fast-forward time right in the middle of the test:
4143

4244
```javascript
45+
jest.useFakeTimers();
4346
test('calls the callback after 1 second', () => {
4447
const timerGame = require('../timerGame');
4548
const callback = jest.fn();
@@ -134,7 +137,8 @@ function timerGame(callback) {
134137
module.exports = timerGame;
135138
```
136139

137-
```javascript
140+
```javascript title="__tests__/timerGame-test.js"
141+
jest.useFakeTimers();
138142
it('calls the callback after 1 second via advanceTimersByTime', () => {
139143
const timerGame = require('../timerGame');
140144
const callback = jest.fn();

website/versioned_docs/version-27.0/TimerMocks.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module.exports = timerGame;
2222
```javascript title="__tests__/timerGame-test.js"
2323
'use strict';
2424

25-
jest.useFakeTimers();
25+
jest.useFakeTimers(); // or you can set "timers": "fake" globally in configuration file
2626
jest.spyOn(global, 'setTimeout');
2727

2828
test('waits 1 second before ending the game', () => {
@@ -53,11 +53,14 @@ test('do something with real timers', () => {
5353
});
5454
```
5555

56+
All of the following functions need fake timers to be set, either by `jest.useFakeTimers()` or via `"timers": "fake"` in the config file.
57+
5658
## Run All Timers
5759

5860
Another test we might want to write for this module is one that asserts that the callback is called after 1 second. To do this, we're going to use Jest's timer control APIs to fast-forward time right in the middle of the test:
5961

6062
```javascript
63+
jest.useFakeTimers();
6164
test('calls the callback after 1 second', () => {
6265
const timerGame = require('../timerGame');
6366
const callback = jest.fn();
@@ -150,7 +153,8 @@ function timerGame(callback) {
150153
module.exports = timerGame;
151154
```
152155

153-
```javascript
156+
```javascript title="__tests__/timerGame-test.js"
157+
jest.useFakeTimers();
154158
it('calls the callback after 1 second via advanceTimersByTime', () => {
155159
const timerGame = require('../timerGame');
156160
const callback = jest.fn();

website/versioned_docs/version-27.1/TimerMocks.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module.exports = timerGame;
2222
```javascript title="__tests__/timerGame-test.js"
2323
'use strict';
2424

25-
jest.useFakeTimers();
25+
jest.useFakeTimers(); // or you can set "timers": "fake" globally in configuration file
2626
jest.spyOn(global, 'setTimeout');
2727

2828
test('waits 1 second before ending the game', () => {
@@ -53,11 +53,14 @@ test('do something with real timers', () => {
5353
});
5454
```
5555

56+
All of the following functions need fake timers to be set, either by `jest.useFakeTimers()` or via `"timers": "fake"` in the config file.
57+
5658
## Run All Timers
5759

5860
Another test we might want to write for this module is one that asserts that the callback is called after 1 second. To do this, we're going to use Jest's timer control APIs to fast-forward time right in the middle of the test:
5961

6062
```javascript
63+
jest.useFakeTimers();
6164
test('calls the callback after 1 second', () => {
6265
const timerGame = require('../timerGame');
6366
const callback = jest.fn();
@@ -150,7 +153,8 @@ function timerGame(callback) {
150153
module.exports = timerGame;
151154
```
152155

153-
```javascript
156+
```javascript title="__tests__/timerGame-test.js"
157+
jest.useFakeTimers();
154158
it('calls the callback after 1 second via advanceTimersByTime', () => {
155159
const timerGame = require('../timerGame');
156160
const callback = jest.fn();

website/versioned_docs/version-27.2/TimerMocks.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module.exports = timerGame;
2222
```javascript title="__tests__/timerGame-test.js"
2323
'use strict';
2424

25-
jest.useFakeTimers();
25+
jest.useFakeTimers(); // or you can set "timers": "fake" globally in configuration file
2626
jest.spyOn(global, 'setTimeout');
2727

2828
test('waits 1 second before ending the game', () => {
@@ -53,6 +53,8 @@ test('do something with real timers', () => {
5353
});
5454
```
5555

56+
All of the following functions need fake timers to be set, either by `jest.useFakeTimers()` or via `"timers": "fake"` in the config file.
57+
5658
## Run All Timers
5759

5860
Another test we might want to write for this module is one that asserts that the callback is called after 1 second. To do this, we're going to use Jest's timer control APIs to fast-forward time right in the middle of the test:
@@ -150,7 +152,8 @@ function timerGame(callback) {
150152
module.exports = timerGame;
151153
```
152154

153-
```javascript
155+
```javascript title="__tests__/timerGame-test.js"
156+
jest.useFakeTimers();
154157
it('calls the callback after 1 second via advanceTimersByTime', () => {
155158
const timerGame = require('../timerGame');
156159
const callback = jest.fn();

website/versioned_docs/version-27.4/TimerMocks.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module.exports = timerGame;
2222
```javascript title="__tests__/timerGame-test.js"
2323
'use strict';
2424

25-
jest.useFakeTimers();
25+
jest.useFakeTimers(); // or you can set "timers": "fake" globally in configuration file
2626
jest.spyOn(global, 'setTimeout');
2727

2828
test('waits 1 second before ending the game', () => {
@@ -53,11 +53,14 @@ test('do something with real timers', () => {
5353
});
5454
```
5555

56+
All of the following functions need fake timers to be set, either by `jest.useFakeTimers()` or via `"timers": "fake"` in the config file.
57+
5658
## Run All Timers
5759

5860
Another test we might want to write for this module is one that asserts that the callback is called after 1 second. To do this, we're going to use Jest's timer control APIs to fast-forward time right in the middle of the test:
5961

6062
```javascript
63+
jest.useFakeTimers();
6164
test('calls the callback after 1 second', () => {
6265
const timerGame = require('../timerGame');
6366
const callback = jest.fn();
@@ -150,7 +153,8 @@ function timerGame(callback) {
150153
module.exports = timerGame;
151154
```
152155

153-
```javascript
156+
```javascript title="__tests__/timerGame-test.js"
157+
jest.useFakeTimers();
154158
it('calls the callback after 1 second via advanceTimersByTime', () => {
155159
const timerGame = require('../timerGame');
156160
const callback = jest.fn();

0 commit comments

Comments
 (0)