Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(fake-timers)!: allow jest.useFakeTimers() and projectConfig.fakeTimers to take an options bag #12572

Merged
merged 73 commits into from Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
6b663c8
feat!: allow useFakeTimers to take an options bag
mrazauskas Mar 12, 2022
344fda7
name `timersConfig` consistently
mrazauskas Mar 12, 2022
c42e93d
tweak defaults
mrazauskas Mar 12, 2022
d828b55
tweak error message
mrazauskas Mar 12, 2022
b6d30db
docs
mrazauskas Mar 13, 2022
3e487fc
add note on "strategy"
mrazauskas Mar 13, 2022
fa2c75f
add example lines
mrazauskas Mar 13, 2022
f480a6e
prettier
mrazauskas Mar 13, 2022
44fffd8
rename `timerLimit`
mrazauskas Mar 13, 2022
39b27f9
rename `fakeTimersConfig` and `FakeTimersConfig`
mrazauskas Mar 13, 2022
e4b0dc0
rethinking API
mrazauskas Mar 13, 2022
4ccc5bb
implement `enableGlobally` and `legacyFakeTimers`
mrazauskas Mar 14, 2022
5ccdff4
group e2e tests
mrazauskas Mar 14, 2022
e17d545
more docs
mrazauskas Mar 14, 2022
a671c6a
more cli docs
mrazauskas Mar 14, 2022
bb38197
clean up e2e tests
mrazauskas Mar 15, 2022
e3ec932
rename `fakeTimers`
mrazauskas Mar 15, 2022
1e33ec0
fix resetAllMocks and resetMocks e2e tests
mrazauskas Mar 15, 2022
8df6ae5
legacyFakeTimers test
mrazauskas Mar 15, 2022
78225a5
add timerLimit test
mrazauskas Mar 15, 2022
c61f4da
fix timerLimit
mrazauskas Mar 15, 2022
f9c511f
Merge branch 'main'
mrazauskas Mar 15, 2022
c96a4c9
add deprecation warning
mrazauskas Mar 15, 2022
3726701
implement advanceTimers option
mrazauskas Mar 15, 2022
84a2a0c
allow clearing not faked timers
mrazauskas Mar 15, 2022
5fef5ce
fix type tests
mrazauskas Mar 15, 2022
aa8d7a9
implement doNotFake option
mrazauskas Mar 16, 2022
f4758c5
fix messages
mrazauskas Mar 16, 2022
a1bc0f6
clean up CLI docs
mrazauskas Mar 16, 2022
4171178
even more docs
mrazauskas Mar 16, 2022
a3c7e4c
better errors
mrazauskas Mar 16, 2022
cd01710
add unit test
mrazauskas Mar 17, 2022
2b8ed49
changelog entry
mrazauskas Mar 17, 2022
0eabf14
better changelog entry
mrazauskas Mar 17, 2022
ebed0c1
remove performance from unit test
mrazauskas Mar 17, 2022
8b5d6b0
fix unit test
mrazauskas Mar 17, 2022
ef1434a
better Date.now() (?)
mrazauskas Mar 17, 2022
5ad74f4
expect.any(Number)
mrazauskas Mar 17, 2022
4ec6cce
mock Date.now()
mrazauskas Mar 17, 2022
7fb3de3
clean up
mrazauskas Mar 19, 2022
947ad20
fix config examples
mrazauskas Mar 19, 2022
268febd
clean up
mrazauskas Mar 19, 2022
5ba69dd
Update docs/JestObjectAPI.md
mrazauskas Mar 21, 2022
2b0a6fe
one more backticks
mrazauskas Mar 21, 2022
6c7e24c
clean up
mrazauskas Mar 22, 2022
a2dfb22
bump @types/sinonjs__fake-timers
mrazauskas Mar 23, 2022
1995027
rebase
mrazauskas Mar 24, 2022
01fabf4
docs
mrazauskas Mar 24, 2022
9e3d7bf
tweak types
mrazauskas Mar 24, 2022
73e092b
prettier
mrazauskas Mar 24, 2022
23196dd
fix docs
mrazauskas Mar 24, 2022
4354da8
now must be a number
mrazauskas Mar 24, 2022
8cc5b8c
true conditions first
mrazauskas Mar 24, 2022
c78df2e
more type tests
mrazauskas Mar 24, 2022
2168c49
more docs
mrazauskas Mar 25, 2022
d183309
refactor toSinonFakeTimersConfig
mrazauskas Mar 26, 2022
ca52b59
fix lint errors
mrazauskas Mar 26, 2022
fb61d57
tweak types
mrazauskas Mar 27, 2022
7113374
Merge branch 'main' into useFakeTimers-bag-of-options
mrazauskas Mar 28, 2022
aec5053
better mock
mrazauskas Mar 28, 2022
5c1daec
rework test
mrazauskas Mar 28, 2022
4fb53fd
fix bug
mrazauskas Apr 2, 2022
459b551
tweak docs
mrazauskas Apr 2, 2022
1009ff1
remove CLI option
mrazauskas Apr 3, 2022
72a6565
improve description
mrazauskas Apr 3, 2022
8ff79fa
snap
mrazauskas Apr 3, 2022
6d1c0d1
default configuration
mrazauskas Apr 4, 2022
cb6bede
fix resetMocks test
mrazauskas Apr 4, 2022
1b29f9e
fix resetMocks test again
mrazauskas Apr 4, 2022
20f1d34
fix logic as suggested
mrazauskas Apr 4, 2022
0c8402c
add migration guide
mrazauskas Apr 5, 2022
07ef70c
Revert "add migration guide"
SimenB Apr 5, 2022
e9e5cfa
Merge branch 'main' into useFakeTimers-bag-of-options
SimenB Apr 5, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion e2e/__tests__/__snapshots__/showConfig.test.ts.snap
Expand Up @@ -66,7 +66,9 @@ exports[`--showConfig outputs config info and exits 1`] = `
],
"testRegex": [],
"testRunner": "<<REPLACED_JEST_PACKAGES_DIR>>/jest-circus/runner.js",
"timers": "real",
"timers": {
"loopLimit": 100000
},
"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"}
}
}
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,9 @@ 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: {
// "loopLimit": 100000
// },

// 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: {loopLimit: 100_000},
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,9 @@ exports[`prints the config object 1`] = `
"\\\\.test\\\\.js$"
],
"testRunner": "myRunner",
"timers": "real",
"timers": {
"loopLimit": 100000
},
"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(timersConfig?: 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 using "timers" option in the Jest configuration file"`;