You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/TimerMocks.md
+3-1
Original file line number
Diff line number
Diff line change
@@ -58,6 +58,7 @@ test('do something with real timers', () => {
58
58
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:
59
59
60
60
```javascript
61
+
jest.useFakeTimers();
61
62
test('calls the callback after 1 second', () => {
62
63
consttimerGame=require('../timerGame');
63
64
constcallback=jest.fn();
@@ -150,7 +151,8 @@ function timerGame(callback) {
150
151
module.exports= timerGame;
151
152
```
152
153
153
-
```javascript
154
+
```javascript title="__tests__/timerGame-test.js"
155
+
jest.useFakeTimers();
154
156
it('calls the callback after 1 second via advanceTimersByTime', () => {
Copy file name to clipboardExpand all lines: website/versioned_docs/version-25.x/TimerMocks.md
+6-2
Original file line number
Diff line number
Diff line change
@@ -22,7 +22,7 @@ module.exports = timerGame;
22
22
```javascript title="__tests__/timerGame-test.js"
23
23
'use strict';
24
24
25
-
jest.useFakeTimers();
25
+
jest.useFakeTimers();// or you can set "timers": "fake" globally in configuration file
26
26
27
27
test('waits 1 second before ending the game', () => {
28
28
consttimerGame=require('../timerGame');
@@ -35,11 +35,14 @@ test('waits 1 second before ending the game', () => {
35
35
36
36
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.
37
37
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
+
38
40
## Run All Timers
39
41
40
42
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:
41
43
42
44
```javascript
45
+
jest.useFakeTimers();
43
46
test('calls the callback after 1 second', () => {
44
47
consttimerGame=require('../timerGame');
45
48
constcallback=jest.fn();
@@ -134,7 +137,8 @@ function timerGame(callback) {
134
137
module.exports= timerGame;
135
138
```
136
139
137
-
```javascript
140
+
```javascript title="__tests__/timerGame-test.js"
141
+
jest.useFakeTimers();
138
142
it('calls the callback after 1 second via advanceTimersByTime', () => {
Copy file name to clipboardExpand all lines: website/versioned_docs/version-26.x/TimerMocks.md
+6-2
Original file line number
Diff line number
Diff line change
@@ -22,7 +22,7 @@ module.exports = timerGame;
22
22
```javascript title="__tests__/timerGame-test.js"
23
23
'use strict';
24
24
25
-
jest.useFakeTimers();
25
+
jest.useFakeTimers();// or you can set "timers": "fake" globally in configuration file
26
26
27
27
test('waits 1 second before ending the game', () => {
28
28
consttimerGame=require('../timerGame');
@@ -35,11 +35,14 @@ test('waits 1 second before ending the game', () => {
35
35
36
36
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.
37
37
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
+
38
40
## Run All Timers
39
41
40
42
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:
41
43
42
44
```javascript
45
+
jest.useFakeTimers();
43
46
test('calls the callback after 1 second', () => {
44
47
consttimerGame=require('../timerGame');
45
48
constcallback=jest.fn();
@@ -134,7 +137,8 @@ function timerGame(callback) {
134
137
module.exports= timerGame;
135
138
```
136
139
137
-
```javascript
140
+
```javascript title="__tests__/timerGame-test.js"
141
+
jest.useFakeTimers();
138
142
it('calls the callback after 1 second via advanceTimersByTime', () => {
Copy file name to clipboardExpand all lines: website/versioned_docs/version-27.0/TimerMocks.md
+6-2
Original file line number
Diff line number
Diff line change
@@ -22,7 +22,7 @@ module.exports = timerGame;
22
22
```javascript title="__tests__/timerGame-test.js"
23
23
'use strict';
24
24
25
-
jest.useFakeTimers();
25
+
jest.useFakeTimers();// or you can set "timers": "fake" globally in configuration file
26
26
jest.spyOn(global, 'setTimeout');
27
27
28
28
test('waits 1 second before ending the game', () => {
@@ -53,11 +53,14 @@ test('do something with real timers', () => {
53
53
});
54
54
```
55
55
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
+
56
58
## Run All Timers
57
59
58
60
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:
59
61
60
62
```javascript
63
+
jest.useFakeTimers();
61
64
test('calls the callback after 1 second', () => {
62
65
consttimerGame=require('../timerGame');
63
66
constcallback=jest.fn();
@@ -150,7 +153,8 @@ function timerGame(callback) {
150
153
module.exports= timerGame;
151
154
```
152
155
153
-
```javascript
156
+
```javascript title="__tests__/timerGame-test.js"
157
+
jest.useFakeTimers();
154
158
it('calls the callback after 1 second via advanceTimersByTime', () => {
Copy file name to clipboardExpand all lines: website/versioned_docs/version-27.1/TimerMocks.md
+6-2
Original file line number
Diff line number
Diff line change
@@ -22,7 +22,7 @@ module.exports = timerGame;
22
22
```javascript title="__tests__/timerGame-test.js"
23
23
'use strict';
24
24
25
-
jest.useFakeTimers();
25
+
jest.useFakeTimers();// or you can set "timers": "fake" globally in configuration file
26
26
jest.spyOn(global, 'setTimeout');
27
27
28
28
test('waits 1 second before ending the game', () => {
@@ -53,11 +53,14 @@ test('do something with real timers', () => {
53
53
});
54
54
```
55
55
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
+
56
58
## Run All Timers
57
59
58
60
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:
59
61
60
62
```javascript
63
+
jest.useFakeTimers();
61
64
test('calls the callback after 1 second', () => {
62
65
consttimerGame=require('../timerGame');
63
66
constcallback=jest.fn();
@@ -150,7 +153,8 @@ function timerGame(callback) {
150
153
module.exports= timerGame;
151
154
```
152
155
153
-
```javascript
156
+
```javascript title="__tests__/timerGame-test.js"
157
+
jest.useFakeTimers();
154
158
it('calls the callback after 1 second via advanceTimersByTime', () => {
Copy file name to clipboardExpand all lines: website/versioned_docs/version-27.2/TimerMocks.md
+5-2
Original file line number
Diff line number
Diff line change
@@ -22,7 +22,7 @@ module.exports = timerGame;
22
22
```javascript title="__tests__/timerGame-test.js"
23
23
'use strict';
24
24
25
-
jest.useFakeTimers();
25
+
jest.useFakeTimers();// or you can set "timers": "fake" globally in configuration file
26
26
jest.spyOn(global, 'setTimeout');
27
27
28
28
test('waits 1 second before ending the game', () => {
@@ -53,6 +53,8 @@ test('do something with real timers', () => {
53
53
});
54
54
```
55
55
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
+
56
58
## Run All Timers
57
59
58
60
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) {
150
152
module.exports= timerGame;
151
153
```
152
154
153
-
```javascript
155
+
```javascript title="__tests__/timerGame-test.js"
156
+
jest.useFakeTimers();
154
157
it('calls the callback after 1 second via advanceTimersByTime', () => {
Copy file name to clipboardExpand all lines: website/versioned_docs/version-27.4/TimerMocks.md
+6-2
Original file line number
Diff line number
Diff line change
@@ -22,7 +22,7 @@ module.exports = timerGame;
22
22
```javascript title="__tests__/timerGame-test.js"
23
23
'use strict';
24
24
25
-
jest.useFakeTimers();
25
+
jest.useFakeTimers();// or you can set "timers": "fake" globally in configuration file
26
26
jest.spyOn(global, 'setTimeout');
27
27
28
28
test('waits 1 second before ending the game', () => {
@@ -53,11 +53,14 @@ test('do something with real timers', () => {
53
53
});
54
54
```
55
55
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
+
56
58
## Run All Timers
57
59
58
60
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:
59
61
60
62
```javascript
63
+
jest.useFakeTimers();
61
64
test('calls the callback after 1 second', () => {
62
65
consttimerGame=require('../timerGame');
63
66
constcallback=jest.fn();
@@ -150,7 +153,8 @@ function timerGame(callback) {
150
153
module.exports= timerGame;
151
154
```
152
155
153
-
```javascript
156
+
```javascript title="__tests__/timerGame-test.js"
157
+
jest.useFakeTimers();
154
158
it('calls the callback after 1 second via advanceTimersByTime', () => {
0 commit comments