diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ee2c7eeb31a..8b3db3fb58b7 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)) 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-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" + } +}