Skip to content

Commit

Permalink
feat!: allow useFakeTimers to take an options bag
Browse files Browse the repository at this point in the history
  • Loading branch information
mrazauskas committed Mar 12, 2022
1 parent 54420eb commit 3ab18f7
Show file tree
Hide file tree
Showing 35 changed files with 298 additions and 125 deletions.
2 changes: 1 addition & 1 deletion e2e/__tests__/__snapshots__/showConfig.test.ts.snap
Expand Up @@ -66,7 +66,7 @@ exports[`--showConfig outputs config info and exits 1`] = `
],
"testRegex": [],
"testRunner": "<<REPLACED_JEST_PACKAGES_DIR>>/jest-circus/runner.js",
"timers": "real",
"timers": {},
"transform": [
[
"\\\\.[jt]sx?$",
Expand Down
4 changes: 2 additions & 2 deletions e2e/fake-promises/asap/package.json
@@ -1,9 +1,9 @@
{
"jest": {
"timers": "fake",
"setupFiles": [
"<rootDir>/fake-promises"
],
"testEnvironment": "node"
"testEnvironment": "node",
"timers": {"strategy": "modern"}
}
}
4 changes: 2 additions & 2 deletions e2e/fake-promises/immediate/package.json
@@ -1,9 +1,9 @@
{
"jest": {
"timers": "fake",
"setupFiles": [
"<rootDir>/fake-promises"
],
"testEnvironment": "node"
"testEnvironment": "node",
"timers": {"strategy": "modern"}
}
}
4 changes: 2 additions & 2 deletions e2e/fake-time/legacy/from-config/package.json
@@ -1,6 +1,6 @@
{
"jest": {
"timers": "legacy",
"testEnvironment": "node"
"testEnvironment": "node",
"timers": {"strategy": "legacy"}
}
}
2 changes: 1 addition & 1 deletion e2e/fake-time/legacy/from-jest-object/__tests__/test.js
Expand Up @@ -8,7 +8,7 @@
'use strict';

test('fake timers', () => {
jest.useFakeTimers('legacy');
jest.useFakeTimers({strategy: 'legacy'});

expect(() => jest.setSystemTime(0)).toThrow(
'setSystemTime is not available when not using modern timers',
Expand Down
Expand Up @@ -10,7 +10,7 @@
'use strict';

test('requestAnimationFrame', () => {
jest.useFakeTimers('legacy');
jest.useFakeTimers({strategy: 'legacy'});
let frameTimestamp = -1;
requestAnimationFrame(timestamp => {
frameTimestamp = timestamp;
Expand Down
4 changes: 2 additions & 2 deletions e2e/fake-time/modern/from-config/package.json
@@ -1,6 +1,6 @@
{
"jest": {
"timers": "fake",
"testEnvironment": "node"
"testEnvironment": "node",
"timers": {"strategy": "modern"}
}
}
3 changes: 2 additions & 1 deletion e2e/fake-time/modern/requestAnimationFrame/__tests__/test.js
Expand Up @@ -10,7 +10,8 @@
'use strict';

test('requestAnimationFrame', () => {
jest.useFakeTimers('modern');
jest.useFakeTimers();

let frameTimestamp = -1;
requestAnimationFrame(timestamp => {
frameTimestamp = timestamp;
Expand Down
4 changes: 2 additions & 2 deletions e2e/modern-fake-timers/from-config/package.json
@@ -1,6 +1,6 @@
{
"jest": {
"timers": "modern",
"testEnvironment": "node"
"testEnvironment": "node",
"timers": {"strategy": "modern"}
}
}
4 changes: 2 additions & 2 deletions e2e/modern-fake-timers/from-jest-object/__tests__/test.js
Expand Up @@ -8,7 +8,7 @@
'use strict';

test('fake timers with number argument', () => {
jest.useFakeTimers('modern');
jest.useFakeTimers();

jest.setSystemTime(0);

Expand All @@ -20,7 +20,7 @@ test('fake timers with number argument', () => {
});

test('fake timers with Date argument', () => {
jest.useFakeTimers('modern');
jest.useFakeTimers();

jest.setSystemTime(new Date(0));

Expand Down
2 changes: 1 addition & 1 deletion e2e/set-immediate/package.json
@@ -1,6 +1,6 @@
{
"jest": {
"testEnvironment": "node",
"timers": "fake"
"timers": {"strategy": "modern"}
}
}
4 changes: 2 additions & 2 deletions e2e/timer-reset-mocks/with-reset-mocks/package.json
@@ -1,7 +1,7 @@
{
"jest": {
"testEnvironment": "node",
"resetMocks": true,
"timers": "fake"
"testEnvironment": "node",
"timers": {"strategy": "modern"}
}
}
Expand Up @@ -8,7 +8,6 @@
describe('timers', () => {
it('should work before calling resetAllMocks', () => {
const f = jest.fn();
jest.useFakeTimers();
setTimeout(f, 0);
jest.runAllTimers();
expect(f).toHaveBeenCalledTimes(1);
Expand Down
2 changes: 1 addition & 1 deletion e2e/timer-use-real-timers/package.json
@@ -1,6 +1,6 @@
{
"jest": {
"testEnvironment": "node",
"timers": "fake"
"timers": {"strategy": "modern"}
}
}
Expand Up @@ -38,10 +38,10 @@ const jestAdapter = async (
testPath,
});

if (config.timers === 'fake' || config.timers === 'modern') {
if (config.timers.strategy === 'modern') {
// during setup, this cannot be null (and it's fine to explode if it is)
environment.fakeTimersModern!.useFakeTimers();
} else if (config.timers === 'legacy') {
environment.fakeTimersModern!.useFakeTimers(config.timers);
} else if (config.timers.strategy === 'legacy') {
environment.fakeTimers!.useFakeTimers();
}

Expand Down
Expand Up @@ -284,7 +284,7 @@ module.exports = {
// testRunner: "jest-circus/runner",
// Setting this value to "fake" allows the use of fake timers for functions such as "setTimeout"
// timers: "real",
// timers: {},
// A map from regular expressions to paths to transformers
// transform: undefined,
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-config/src/Defaults.ts
Expand Up @@ -72,7 +72,7 @@ const defaultOptions: Config.DefaultOptions = {
testRegex: [],
testRunner: 'jest-circus/runner',
testSequencer: '@jest/test-sequencer',
timers: 'real',
timers: {},
transformIgnorePatterns: [NODE_MODULES_REGEXP, `\\.pnp\\.[^\\${sep}]+$`],
useStderr: false,
watch: false,
Expand Down
1 change: 1 addition & 0 deletions packages/jest-config/src/Descriptions.ts
Expand Up @@ -84,6 +84,7 @@ const descriptions: {[key in keyof Config.InitialOptions]: string} = {
testResultsProcessor:
'This option allows the use of a custom results processor',
testRunner: 'This option allows use of a custom test runner',
// TODO
timers:
'Setting this value to "fake" allows the use of fake timers for functions such as "setTimeout"',
transform: 'A map from regular expressions to paths to transformers',
Expand Down
29 changes: 28 additions & 1 deletion packages/jest-config/src/ValidConfig.ts
Expand Up @@ -132,7 +132,34 @@ const initialOptions: Config.InitialOptions = {
testRunner: 'circus',
testSequencer: '@jest/test-sequencer',
testTimeout: 5000,
timers: 'real',
timers: multipleValidOptions(
{loopLimit: 1000, strategy: 'legacy'} as const,
{
advanceTimeDelta: 1000,
loopLimit: 1000,
now: multipleValidOptions(0, new Date()),
shouldAdvanceTime: false,
shouldClearNativeTimers: false,
strategy: 'modern',
toFake: [
'setImmediate',
'clearImmediate',
'setInterval',
'clearInterval',
'setTimeout',
'clearTimeout',
'requestAnimationFrame',
'cancelAnimationFrame',
'requestIdleCallback',
'cancelIdleCallback',
'Date',
'hrtime',
'nextTick',
'performance',
'queueMicrotask', // ?
] as Array<Config.FakeableTimerAPIs>,
} as const,
),
transform: {
'\\.js$': '<rootDir>/preprocessor.js',
},
Expand Down
Expand Up @@ -52,7 +52,7 @@ exports[`prints the config object 1`] = `
"\\\\.test\\\\.js$"
],
"testRunner": "myRunner",
"timers": "real",
"timers": {},
"transform": [],
"transformIgnorePatterns": [],
"watchPathIgnorePatterns": []
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-environment/src/index.ts
Expand Up @@ -304,7 +304,7 @@ export interface Jest {
/**
* Instructs Jest to use fake versions of the standard timer functions.
*/
useFakeTimers(implementation?: 'modern' | 'legacy'): Jest;
useFakeTimers(config?: Config.TimersConfig): Jest;
/**
* Instructs Jest to use the real versions of the standard timer functions.
*/
Expand Down
1 change: 1 addition & 0 deletions packages/jest-fake-timers/package.json
Expand Up @@ -25,6 +25,7 @@
"jest-util": "^28.0.0-alpha.7"
},
"devDependencies": {
"@jest/test-utils": "^28.0.0-alpha.7",
"@types/sinonjs__fake-timers": "^8.1.1"
},
"engines": {
Expand Down
@@ -1,3 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`FakeTimers runAllTimers warns when trying to advance timers while real timers are used 1`] = `"A function to advance timers was called but the timers API is not mocked with fake timers. Call \`jest.useFakeTimers()\` in this test or enable fake timers globally by setting \`"timers": "fake"\` in the configuration file"`;
exports[`FakeTimers runAllTimers warns when trying to advance timers while real timers are used 1`] = `"A function to advance timers was called but the timers API is not mocked with fake timers. Call \`jest.useFakeTimers()\` in this test or enable fake timers globally by setting \`"timers": {"strategy": "modern"}\` in the configuration file"`;

0 comments on commit 3ab18f7

Please sign in to comment.