Skip to content

Commit 0709aa8

Browse files
committed
feat: use modern fake timers by default
1 parent 5e2e8ed commit 0709aa8

5 files changed

Lines changed: 18 additions & 17 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- `[jest-config]` [**BREAKING**] Default to Node testing environment instead of browser (JSDOM) ([#9874](https://github.com/facebook/jest/pull/9874))
66
- `[jest-config]` [**BREAKING**] Use `jest-circus` as default test runner ([#10686](https://github.com/facebook/jest/pull/10686))
77
- `[jest-config, jest-runtime]` Support ESM for files other than `.js` and `.mjs` ([#10823](https://github.com/facebook/jest/pull/10823))
8+
- `[jest-config, jest-runtime]` [**BREAKING**] Use "modern" implementation as default for fake timers ([#10874](https://github.com/facebook/jest/pull/10874))
89
- `[jest-repl, jest-runner]` [**BREAKING**] Run transforms over environment ([#8751](https://github.com/facebook/jest/pull/8751))
910
- `[jest-runner]` [**BREAKING**] set exit code to 1 if test logs after teardown ([#10728](https://github.com/facebook/jest/pull/10728))
1011
- `[jest-snapshot]` [**BREAKING**] Make prettier optional for inline snapshots - fall back to string replacement ([#7792](https://github.com/facebook/jest/pull/7792))

docs/JestObjectAPI.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -582,9 +582,9 @@ Restores all mocks back to their original value. Equivalent to calling [`.mockRe
582582

583583
### `jest.useFakeTimers(implementation?: 'modern' | 'legacy')`
584584

585-
Instructs Jest to use fake versions of the standard timer functions (`setTimeout`, `setInterval`, `clearTimeout`, `clearInterval`, `nextTick`, `setImmediate` and `clearImmediate`).
585+
Instructs Jest to use fake versions of the standard timer functions (`setTimeout`, `setInterval`, `clearTimeout`, `clearInterval`, `nextTick`, `setImmediate` and `clearImmediate` as well as `Date`).
586586

587-
If you pass `'modern'` as an argument, [`@sinonjs/fake-timers`](https://github.com/sinonjs/fake-timers) will be used as implementation instead of Jest's own fake timers. This also mocks additional timers like `Date`. `'modern'` will be the default behavior in Jest 27.
587+
If you pass `'legacy'` as an argument, Jest's legacy implementation will be used rather than one based on [`@sinonjs/fake-timers`](https://github.com/sinonjs/fake-timers).
588588

589589
Returns the `jest` object for chaining.
590590

packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapter.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ const jestAdapter = async (
4141
testPath,
4242
});
4343

44-
if (config.timers === 'fake' || config.timers === 'legacy') {
44+
if (config.timers === 'fake' || config.timers === 'modern') {
4545
// during setup, this cannot be null (and it's fine to explode if it is)
46-
environment.fakeTimers!.useFakeTimers();
47-
} else if (config.timers === 'modern') {
4846
environment.fakeTimersModern!.useFakeTimers();
47+
} else if (config.timers === 'legacy') {
48+
environment.fakeTimers!.useFakeTimers();
4949
}
5050

5151
globals.beforeEach(() => {
@@ -60,7 +60,7 @@ const jestAdapter = async (
6060
if (config.resetMocks) {
6161
runtime.resetAllMocks();
6262

63-
if (config.timers === 'fake') {
63+
if (config.timers === 'legacy') {
6464
// during setup, this cannot be null (and it's fine to explode if it is)
6565
environment.fakeTimers!.useFakeTimers();
6666
}

packages/jest-jasmine2/src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ export default async function jasmine2(
8686
environment.global.describe.skip = environment.global.xdescribe;
8787
environment.global.describe.only = environment.global.fdescribe;
8888

89-
if (config.timers === 'fake' || config.timers === 'legacy') {
90-
environment.fakeTimers!.useFakeTimers();
91-
} else if (config.timers === 'modern') {
89+
if (config.timers === 'fake' || config.timers === 'modern') {
9290
environment.fakeTimersModern!.useFakeTimers();
91+
} else if (config.timers === 'legacy') {
92+
environment.fakeTimers!.useFakeTimers();
9393
}
9494

9595
env.beforeEach(() => {
@@ -104,7 +104,7 @@ export default async function jasmine2(
104104
if (config.resetMocks) {
105105
runtime.resetAllMocks();
106106

107-
if (config.timers === 'fake' || config.timers === 'legacy') {
107+
if (config.timers === 'legacy') {
108108
environment.fakeTimers!.useFakeTimers();
109109
}
110110
}

packages/jest-runtime/src/index.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,9 @@ export default class Runtime {
243243
this._transitiveShouldMock = new Map();
244244

245245
this._fakeTimersImplementation =
246-
config.timers === 'modern'
247-
? this._environment.fakeTimersModern
248-
: this._environment.fakeTimers;
246+
config.timers === 'legacy'
247+
? this._environment.fakeTimers
248+
: this._environment.fakeTimersModern;
249249

250250
this._unmockList = unmockRegExpCache.get(config);
251251
if (!this._unmockList && config.unmockedModulePathPatterns) {
@@ -1558,11 +1558,11 @@ export default class Runtime {
15581558

15591559
return this._fakeTimersImplementation!;
15601560
};
1561-
const useFakeTimers = (type: string = 'legacy') => {
1562-
if (type === 'modern') {
1563-
this._fakeTimersImplementation = this._environment.fakeTimersModern;
1564-
} else {
1561+
const useFakeTimers = (type: string = 'modern') => {
1562+
if (type === 'legacy') {
15651563
this._fakeTimersImplementation = this._environment.fakeTimers;
1564+
} else {
1565+
this._fakeTimersImplementation = this._environment.fakeTimersModern;
15661566
}
15671567
this._fakeTimersImplementation!.useFakeTimers();
15681568
return jestObject;

0 commit comments

Comments
 (0)