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: mock timers in example #12385

Merged
merged 3 commits into from
Feb 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/TimerMocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ module.exports = infiniteTimerGame;
'use strict';

jest.useFakeTimers();
jest.spyOn(global, 'setTimeout');

describe('infiniteTimerGame', () => {
test('schedules a 10-second timer after 1 second', () => {
Expand Down
25 changes: 21 additions & 4 deletions website/versioned_docs/version-25.x/TimerMocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module.exports = timerGame;
'use strict';

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

test('waits 1 second before ending the game', () => {
const timerGame = require('../timerGame');
Expand All @@ -33,9 +34,26 @@ test('waits 1 second before ending the game', () => {
});
```

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.
Here we enable fake timers by calling `jest.useFakeTimers()`. This mocks out `setTimeout` and other timer functions with mock functions. Timers can be restored to their normal behavior with `jest.useRealTimers()`.

All of the following functions need fake timers to be set, either by `jest.useFakeTimers()` or via `"timers": "fake"` in the config file.
While you can call `jest.useFakeTimers()` or `jest.useRealTimers()` from anywhere (top level, inside an `it` block, etc.), it is a **global operation** and will affect other tests within the same file. Additionally, you need to call `jest.useFakeTimers()` to reset internal counters before each test. If you plan to not use fake timers in all your tests, you will want to clean up manually, as otherwise the faked timers will leak across tests:

```javascript
afterEach(() => {
jest.useRealTimers();
});

test('do something with fake timers', () => {
jest.useFakeTimers();
// ...
});

test('do something with real timers', () => {
// ...
});
```

Currently, two implementations of the fake timers are included - `modern` and `legacy`, where `modern` is the default one. See [configuration](Configuration.md#timers-string) for how to configure it.

## Run All Timers

Expand Down Expand Up @@ -95,6 +113,7 @@ module.exports = infiniteTimerGame;
'use strict';

jest.useFakeTimers();
jest.spyOn(global, 'setTimeout');

describe('infiniteTimerGame', () => {
test('schedules a 10-second timer after 1 second', () => {
Expand Down Expand Up @@ -125,8 +144,6 @@ describe('infiniteTimerGame', () => {

## Advance Timers by Time

##### renamed from `runTimersToTime` to `advanceTimersByTime` in Jest **22.0.0**

Another possibility is use `jest.advanceTimersByTime(msToRun)`. When this API is called, all timers are advanced by `msToRun` milliseconds. All pending "macro-tasks" that have been queued via setTimeout() or setInterval(), and would be executed during this time frame, will be executed. Additionally, if those macro-tasks schedule new macro-tasks that would be executed within the same time frame, those will be executed until there are no more macro-tasks remaining in the queue that should be run within msToRun milliseconds.

```javascript title="timerGame.js"
Expand Down
1 change: 1 addition & 0 deletions website/versioned_docs/version-26.x/TimerMocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ module.exports = infiniteTimerGame;
'use strict';

jest.useFakeTimers();
jest.spyOn(global, 'setTimeout');

describe('infiniteTimerGame', () => {
test('schedules a 10-second timer after 1 second', () => {
Expand Down
1 change: 1 addition & 0 deletions website/versioned_docs/version-27.0/TimerMocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ module.exports = infiniteTimerGame;
'use strict';

jest.useFakeTimers();
jest.spyOn(global, 'setTimeout');

describe('infiniteTimerGame', () => {
test('schedules a 10-second timer after 1 second', () => {
Expand Down
1 change: 1 addition & 0 deletions website/versioned_docs/version-27.1/TimerMocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ module.exports = infiniteTimerGame;
'use strict';

jest.useFakeTimers();
jest.spyOn(global, 'setTimeout');

describe('infiniteTimerGame', () => {
test('schedules a 10-second timer after 1 second', () => {
Expand Down
1 change: 1 addition & 0 deletions website/versioned_docs/version-27.2/TimerMocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ module.exports = infiniteTimerGame;
'use strict';

jest.useFakeTimers();
jest.spyOn(global, 'setTimeout');

describe('infiniteTimerGame', () => {
test('schedules a 10-second timer after 1 second', () => {
Expand Down
1 change: 1 addition & 0 deletions website/versioned_docs/version-27.4/TimerMocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ module.exports = infiniteTimerGame;
'use strict';

jest.useFakeTimers();
jest.spyOn(global, 'setTimeout');

describe('infiniteTimerGame', () => {
test('schedules a 10-second timer after 1 second', () => {
Expand Down
1 change: 1 addition & 0 deletions website/versioned_docs/version-27.5/TimerMocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ module.exports = infiniteTimerGame;
'use strict';

jest.useFakeTimers();
jest.spyOn(global, 'setTimeout');

describe('infiniteTimerGame', () => {
test('schedules a 10-second timer after 1 second', () => {
Expand Down