Skip to content

Commit

Permalink
chore(docs): clarify timer-mocks page (#12241)
Browse files Browse the repository at this point in the history
  • Loading branch information
Biki-das committed Feb 5, 2022
1 parent b2db6cf commit 18dc88e
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 13 deletions.
4 changes: 3 additions & 1 deletion docs/TimerMocks.md
Expand Up @@ -58,6 +58,7 @@ 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 @@ -150,7 +151,8 @@ function timerGame(callback) {
module.exports = timerGame;
```

```javascript
```javascript title="__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
8 changes: 6 additions & 2 deletions website/versioned_docs/version-25.x/TimerMocks.md
Expand Up @@ -22,7 +22,7 @@ module.exports = timerGame;
```javascript title="__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 @@ -35,11 +35,14 @@ 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 @@ -134,7 +137,8 @@ function timerGame(callback) {
module.exports = timerGame;
```

```javascript
```javascript title="__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
8 changes: 6 additions & 2 deletions website/versioned_docs/version-26.x/TimerMocks.md
Expand Up @@ -22,7 +22,7 @@ module.exports = timerGame;
```javascript title="__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 @@ -35,11 +35,14 @@ 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 @@ -134,7 +137,8 @@ function timerGame(callback) {
module.exports = timerGame;
```

```javascript
```javascript title="__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
8 changes: 6 additions & 2 deletions website/versioned_docs/version-27.0/TimerMocks.md
Expand Up @@ -22,7 +22,7 @@ module.exports = timerGame;
```javascript title="__tests__/timerGame-test.js"
'use strict';

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

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

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 @@ -150,7 +153,8 @@ function timerGame(callback) {
module.exports = timerGame;
```

```javascript
```javascript title="__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
8 changes: 6 additions & 2 deletions website/versioned_docs/version-27.1/TimerMocks.md
Expand Up @@ -22,7 +22,7 @@ module.exports = timerGame;
```javascript title="__tests__/timerGame-test.js"
'use strict';

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

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

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 @@ -150,7 +153,8 @@ function timerGame(callback) {
module.exports = timerGame;
```

```javascript
```javascript title="__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
7 changes: 5 additions & 2 deletions website/versioned_docs/version-27.2/TimerMocks.md
Expand Up @@ -22,7 +22,7 @@ module.exports = timerGame;
```javascript title="__tests__/timerGame-test.js"
'use strict';

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

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

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:
Expand Down Expand Up @@ -150,7 +152,8 @@ function timerGame(callback) {
module.exports = timerGame;
```

```javascript
```javascript title="__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
8 changes: 6 additions & 2 deletions website/versioned_docs/version-27.4/TimerMocks.md
Expand Up @@ -22,7 +22,7 @@ module.exports = timerGame;
```javascript title="__tests__/timerGame-test.js"
'use strict';

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

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

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 @@ -150,7 +153,8 @@ function timerGame(callback) {
module.exports = timerGame;
```

```javascript
```javascript title="__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

0 comments on commit 18dc88e

Please sign in to comment.