From aa0618a5067984054210922badd51e35d7012521 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 8 Sep 2019 15:12:05 +0200 Subject: [PATCH 1/3] make lolex default implementation --- docs/Configuration.md | 4 ++-- e2e/__tests__/fakeTime.test.ts | 23 +++++++++++++++++++ e2e/fake-promises/immediate/package.json | 2 +- .../legacy/from-config/__tests__/test.js | 14 +++++++++++ e2e/fake-time/legacy/from-config/package.json | 6 +++++ .../legacy/from-jest-object/__tests__/test.js | 16 +++++++++++++ .../legacy/from-jest-object/package.json | 5 ++++ .../modern/from-config/__tests__/test.js | 18 +++++++++++++++ e2e/fake-time/modern/from-config/package.json | 6 +++++ .../modern/from-jest-object/__tests__/test.js | 20 ++++++++++++++++ .../modern/from-jest-object/package.json | 5 ++++ 11 files changed, 116 insertions(+), 3 deletions(-) create mode 100644 e2e/__tests__/fakeTime.test.ts create mode 100644 e2e/fake-time/legacy/from-config/__tests__/test.js create mode 100644 e2e/fake-time/legacy/from-config/package.json create mode 100644 e2e/fake-time/legacy/from-jest-object/__tests__/test.js create mode 100644 e2e/fake-time/legacy/from-jest-object/package.json create mode 100644 e2e/fake-time/modern/from-config/__tests__/test.js create mode 100644 e2e/fake-time/modern/from-config/package.json create mode 100644 e2e/fake-time/modern/from-jest-object/__tests__/test.js create mode 100644 e2e/fake-time/modern/from-jest-object/package.json diff --git a/docs/Configuration.md b/docs/Configuration.md index d1eaa4c69fe9..7b9a0dc08757 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -1275,9 +1275,9 @@ This option sets the URL for the jsdom environment. It is reflected in propertie Default: `real` -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. +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. -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. +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). ### `transform` \[object<string, pathToTransformer | \[pathToTransformer, object]>] diff --git a/e2e/__tests__/fakeTime.test.ts b/e2e/__tests__/fakeTime.test.ts new file mode 100644 index 000000000000..c7cad2c3294a --- /dev/null +++ b/e2e/__tests__/fakeTime.test.ts @@ -0,0 +1,23 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +import runJest from '../runJest'; + +describe.each(['modern', 'legacy'])( + '%s implementation of fake timers', + implementation => { + it('should be possible to use from config', () => { + const result = runJest(`fake-time/${implementation}/from-config`); + expect(result.exitCode).toBe(0); + }); + + it('should be possible to use from jest-object', () => { + const result = runJest(`fake-time/${implementation}/from-jest-object`); + expect(result.exitCode).toBe(0); + }); + }, +); diff --git a/e2e/fake-promises/immediate/package.json b/e2e/fake-promises/immediate/package.json index 0f50640514e1..44363c0165db 100644 --- a/e2e/fake-promises/immediate/package.json +++ b/e2e/fake-promises/immediate/package.json @@ -1,6 +1,6 @@ { "jest": { - "timers": "fake", + "timers": "legacy", "setupFiles": [ "/fake-promises" ], diff --git a/e2e/fake-time/legacy/from-config/__tests__/test.js b/e2e/fake-time/legacy/from-config/__tests__/test.js new file mode 100644 index 000000000000..b2da4295d439 --- /dev/null +++ b/e2e/fake-time/legacy/from-config/__tests__/test.js @@ -0,0 +1,14 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +'use strict'; + +test('fake timers', () => { + expect(() => jest.setSystemTime(0)).toThrow( + 'setSystemTime is not available when not using modern timers', + ); +}); diff --git a/e2e/fake-time/legacy/from-config/package.json b/e2e/fake-time/legacy/from-config/package.json new file mode 100644 index 000000000000..3d94760ca58b --- /dev/null +++ b/e2e/fake-time/legacy/from-config/package.json @@ -0,0 +1,6 @@ +{ + "jest": { + "timers": "legacy", + "testEnvironment": "node" + } +} diff --git a/e2e/fake-time/legacy/from-jest-object/__tests__/test.js b/e2e/fake-time/legacy/from-jest-object/__tests__/test.js new file mode 100644 index 000000000000..982b23cbf809 --- /dev/null +++ b/e2e/fake-time/legacy/from-jest-object/__tests__/test.js @@ -0,0 +1,16 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +'use strict'; + +test('fake timers', () => { + jest.useFakeTimers('legacy'); + + expect(() => jest.setSystemTime(0)).toThrow( + 'setSystemTime is not available when not using modern timers', + ); +}); diff --git a/e2e/fake-time/legacy/from-jest-object/package.json b/e2e/fake-time/legacy/from-jest-object/package.json new file mode 100644 index 000000000000..148788b25446 --- /dev/null +++ b/e2e/fake-time/legacy/from-jest-object/package.json @@ -0,0 +1,5 @@ +{ + "jest": { + "testEnvironment": "node" + } +} diff --git a/e2e/fake-time/modern/from-config/__tests__/test.js b/e2e/fake-time/modern/from-config/__tests__/test.js new file mode 100644 index 000000000000..a32e5a5bc8e4 --- /dev/null +++ b/e2e/fake-time/modern/from-config/__tests__/test.js @@ -0,0 +1,18 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +'use strict'; + +test('fake timers', () => { + jest.setSystemTime(0); + + expect(Date.now()).toBe(0); + + jest.setSystemTime(1000); + + expect(Date.now()).toBe(1000); +}); diff --git a/e2e/fake-time/modern/from-config/package.json b/e2e/fake-time/modern/from-config/package.json new file mode 100644 index 000000000000..8d831ccf6947 --- /dev/null +++ b/e2e/fake-time/modern/from-config/package.json @@ -0,0 +1,6 @@ +{ + "jest": { + "timers": "fake", + "testEnvironment": "node" + } +} diff --git a/e2e/fake-time/modern/from-jest-object/__tests__/test.js b/e2e/fake-time/modern/from-jest-object/__tests__/test.js new file mode 100644 index 000000000000..192f2b72cd76 --- /dev/null +++ b/e2e/fake-time/modern/from-jest-object/__tests__/test.js @@ -0,0 +1,20 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +'use strict'; + +test('fake timers', () => { + jest.useFakeTimers(); + + jest.setSystemTime(0); + + expect(Date.now()).toBe(0); + + jest.setSystemTime(1000); + + expect(Date.now()).toBe(1000); +}); diff --git a/e2e/fake-time/modern/from-jest-object/package.json b/e2e/fake-time/modern/from-jest-object/package.json new file mode 100644 index 000000000000..148788b25446 --- /dev/null +++ b/e2e/fake-time/modern/from-jest-object/package.json @@ -0,0 +1,5 @@ +{ + "jest": { + "testEnvironment": "node" + } +} From 9ea7c0f92dece5bae170cd33a963f2b0df08ab1b Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 14 Mar 2021 15:03:20 +0100 Subject: [PATCH 2/3] changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 24c1e45f2734..54f3dd052818 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ - `[jest-config]` [**BREAKING**] Default to Node testing environment instead of browser (JSDOM) ([#9874](https://github.com/facebook/jest/pull/9874)) - `[jest-config]` [**BREAKING**] Use `jest-circus` as default test runner ([#10686](https://github.com/facebook/jest/pull/10686)) - `[jest-config, jest-runtime]` Support ESM for files other than `.js` and `.mjs` ([#10823](https://github.com/facebook/jest/pull/10823)) -- `[jest-config, jest-runtime]` [**BREAKING**] Use "modern" implementation as default for fake timers ([#10874](https://github.com/facebook/jest/pull/10874)) +- `[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)) - `[jest-core]` make `TestWatcher` extend `emittery` ([#10324](https://github.com/facebook/jest/pull/10324)) - `[jest-core]` Run failed tests interactively the same way we do with snapshots ([#10858](https://github.com/facebook/jest/pull/10858)) - `[jest-core]` more `TestSequencer` methods can be async ([#10980](https://github.com/facebook/jest/pull/10980)) From 5743bdcab9e30bd3a9f4d406970b1fa6599fae7a Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 14 Mar 2021 15:30:17 +0100 Subject: [PATCH 3/3] remove unnecessary change --- e2e/fake-promises/immediate/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/e2e/fake-promises/immediate/package.json b/e2e/fake-promises/immediate/package.json index 44363c0165db..0f50640514e1 100644 --- a/e2e/fake-promises/immediate/package.json +++ b/e2e/fake-promises/immediate/package.json @@ -1,6 +1,6 @@ { "jest": { - "timers": "legacy", + "timers": "fake", "setupFiles": [ "/fake-promises" ],