Skip to content

Commit a03b6fe

Browse files
authored
chore: update docs for fake timers (#11197)
1 parent 0794849 commit a03b6fe

File tree

11 files changed

+116
-3
lines changed

11 files changed

+116
-3
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
- `[jest-config]` [**BREAKING**] Default to Node testing environment instead of browser (JSDOM) ([#9874](https://github.com/facebook/jest/pull/9874))
1010
- `[jest-config]` [**BREAKING**] Use `jest-circus` as default test runner ([#10686](https://github.com/facebook/jest/pull/10686))
1111
- `[jest-config, jest-runtime]` Support ESM for files other than `.js` and `.mjs` ([#10823](https://github.com/facebook/jest/pull/10823))
12-
- `[jest-config, jest-runtime]` [**BREAKING**] Use "modern" implementation as default for fake timers ([#10874](https://github.com/facebook/jest/pull/10874))
12+
- `[jest-config, jest-runtime]` [**BREAKING**] Use "modern" implementation as default for fake timers ([#10874](https://github.com/facebook/jest/pull/10874) & [#11197](https://github.com/facebook/jest/pull/11197))
1313
- `[jest-core]` make `TestWatcher` extend `emittery` ([#10324](https://github.com/facebook/jest/pull/10324))
1414
- `[jest-core]` Run failed tests interactively the same way we do with snapshots ([#10858](https://github.com/facebook/jest/pull/10858))
1515
- `[jest-core]` more `TestSequencer` methods can be async ([#10980](https://github.com/facebook/jest/pull/10980))

docs/Configuration.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1275,9 +1275,9 @@ This option sets the URL for the jsdom environment. It is reflected in propertie
12751275

12761276
Default: `real`
12771277

1278-
Setting this value to `legacy` or `fake` allows the use of fake timers for functions such as `setTimeout`. Fake timers are useful when a piece of code sets a long timeout that we don't want to wait for in a test.
1278+
Setting this value to `fake` or `modern` allows the use of fake timers for functions such as `setTimeout`. Fake timers are useful when a piece of code sets a long timeout that we don't want to wait for in a test.
12791279

1280-
If the value is `modern`, [`@sinonjs/fake-timers`](https://github.com/sinonjs/fake-timers) will be used as implementation instead of Jest's own legacy implementation. This will be the default fake implementation in Jest 27.
1280+
If the value is `legacy`, the old implementation will be used as implementation instead of one backed by [`@sinonjs/fake-timers`](https://github.com/sinonjs/fake-timers).
12811281

12821282
### `transform` \[object<string, pathToTransformer | \[pathToTransformer, object]>]
12831283

e2e/__tests__/fakeTime.test.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import runJest from '../runJest';
9+
10+
describe.each(['modern', 'legacy'])(
11+
'%s implementation of fake timers',
12+
implementation => {
13+
it('should be possible to use from config', () => {
14+
const result = runJest(`fake-time/${implementation}/from-config`);
15+
expect(result.exitCode).toBe(0);
16+
});
17+
18+
it('should be possible to use from jest-object', () => {
19+
const result = runJest(`fake-time/${implementation}/from-jest-object`);
20+
expect(result.exitCode).toBe(0);
21+
});
22+
},
23+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
'use strict';
9+
10+
test('fake timers', () => {
11+
expect(() => jest.setSystemTime(0)).toThrow(
12+
'setSystemTime is not available when not using modern timers',
13+
);
14+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"jest": {
3+
"timers": "legacy",
4+
"testEnvironment": "node"
5+
}
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
'use strict';
9+
10+
test('fake timers', () => {
11+
jest.useFakeTimers('legacy');
12+
13+
expect(() => jest.setSystemTime(0)).toThrow(
14+
'setSystemTime is not available when not using modern timers',
15+
);
16+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"jest": {
3+
"testEnvironment": "node"
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
'use strict';
9+
10+
test('fake timers', () => {
11+
jest.setSystemTime(0);
12+
13+
expect(Date.now()).toBe(0);
14+
15+
jest.setSystemTime(1000);
16+
17+
expect(Date.now()).toBe(1000);
18+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"jest": {
3+
"timers": "fake",
4+
"testEnvironment": "node"
5+
}
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
'use strict';
9+
10+
test('fake timers', () => {
11+
jest.useFakeTimers();
12+
13+
jest.setSystemTime(0);
14+
15+
expect(Date.now()).toBe(0);
16+
17+
jest.setSystemTime(1000);
18+
19+
expect(Date.now()).toBe(1000);
20+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"jest": {
3+
"testEnvironment": "node"
4+
}
5+
}

0 commit comments

Comments
 (0)