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: Clarify timer-mocks page #11324

Closed
wants to merge 3 commits into from
Closed
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
5 changes: 5 additions & 0 deletions docs/TimerMocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ test('do something with real timers', () => {
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:

```javascript
jest.useFakeTimers();

test('calls the callback after 1 second', () => {
const timerGame = require('../timerGame');
const callback = jest.fn();
Expand Down Expand Up @@ -155,6 +157,9 @@ module.exports = timerGame;
```

```javascript
// __tests__/timerGame-test.js
jest.useFakeTimers();

it('calls the callback after 1 second via advanceTimersByTime', () => {
const timerGame = require('../timerGame');
const callback = jest.fn();
Expand Down
9 changes: 8 additions & 1 deletion website/versioned_docs/version-25.x/TimerMocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = timerGame;
// __tests__/timerGame-test.js
'use strict';

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

test('waits 1 second before ending the game', () => {
const timerGame = require('../timerGame');
Expand All @@ -37,11 +37,15 @@ 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.

> All of the following functions need fake timers to be set, either by `jest.useFakeTimers()` or via `"timers": "fake"` in the config file.

## Run All Timers

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:

```javascript
jest.useFakeTimers();

test('calls the callback after 1 second', () => {
const timerGame = require('../timerGame');
const callback = jest.fn();
Expand Down Expand Up @@ -140,6 +144,9 @@ module.exports = timerGame;
```

```javascript
// __tests__/timerGame-test.js
jest.useFakeTimers();

it('calls the callback after 1 second via advanceTimersByTime', () => {
const timerGame = require('../timerGame');
const callback = jest.fn();
Expand Down
9 changes: 8 additions & 1 deletion website/versioned_docs/version-26.x/TimerMocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = timerGame;
// __tests__/timerGame-test.js
'use strict';

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

test('waits 1 second before ending the game', () => {
const timerGame = require('../timerGame');
Expand All @@ -37,11 +37,15 @@ 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.

> All of the following functions need fake timers to be set, either by `jest.useFakeTimers()` or via `"timers": "fake"` in the config file.

## Run All Timers

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:

```javascript
jest.useFakeTimers();

test('calls the callback after 1 second', () => {
const timerGame = require('../timerGame');
const callback = jest.fn();
Expand Down Expand Up @@ -140,6 +144,9 @@ module.exports = timerGame;
```

```javascript
// __tests__/timerGame-test.js
jest.useFakeTimers();

it('calls the callback after 1 second via advanceTimersByTime', () => {
const timerGame = require('../timerGame');
const callback = jest.fn();
Expand Down