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

Jest fake-timer support #62

Merged
merged 6 commits into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ The lists below are not comprehensive: feel free to [start a discussion](https:/

- Jest globals: `expect`, `test`, `it`, `describe`, `beforeAll`, `afterAll`, `beforeEach`, `afterEach`
- Jest function mocks: `jest.fn`, `jest.spyOn`, `jest.clearAllMocks`, `jest.resetAllMocks`
- Jest timer mocks: `jest.useFakeTimers`, `jest.useRealTimers`, `jest.setSystemTime`, `jest.advanceTimersByTime`
- Inline and external snapshots
- Jest cli options: `--testNamePattern`/`-t`, `--maxWorkers`, `--runInBand`
- Jest config options: `setupFiles`, `setupFilesAfterEnv`, `snapshotSerializers`, `maxWorkers`, `snapshotFormat`, `snapshotResolver`
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
},
"dependencies": {
"@jest/expect": "^29.0.1",
"@jest/fake-timers": "^29.2.2",
"jest-circus": "^29.0.1",
"jest-each": "^29.0.1",
"jest-mock": "^29.0.1",
Expand Down
25 changes: 25 additions & 0 deletions src/global-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@

import mock from "jest-mock";
import { jestExpect as expect } from "@jest/expect";
import { ModernFakeTimers } from "@jest/fake-timers";
import * as circus from "jest-circus";

const jestMock = new mock.ModuleMocker(globalThis);
const jestTimer = new ModernFakeTimers({
config: {
fakeTimers: {
enableGlobally: true,
},
},
global: globalThis,
});

globalThis.expect = expect;
globalThis.test = circus.test;
Expand All @@ -20,4 +29,20 @@ globalThis.jest = {
clearAllMocks: jestMock.clearAllMocks.bind(jestMock),
resetAllMocks: jestMock.resetAllMocks.bind(jestMock),
restoreAllMocks: jestMock.restoreAllMocks.bind(jestMock),
useFakeTimers() {
jestTimer.useFakeTimers();
return this;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this defined here? should it be return globalThis.jest?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this in method of object equals the object?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uh well, this is defined if you call jest.useFakeTimers().something. Does it work in Jest if you do (jest.useFakeTimers, 0)().something?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this in method of object equals the object?

Hah, TIL 😀

Does it work in Jest if you do (jest.useFakeTimers, 0)().something?

(jest.useFakeTimers , 0) is not a function which seems fair? I get the same doing (document.createElement, 0)() in a browser

Copy link
Contributor

@fisker fisker Feb 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

He mean (0, jest.useFakeTimers)().something.

The following won't work either 😄

const {useFakeTimers} = jest
useFakeTimers().something
jest.useFakeTimers.call(null).something
const foo = {useFakeTimers: jest.useFakeTimers}
foo.useFakeTimers().something

},
setSystemTime(time) {
jestTimer.setSystemTime(time);
return this;
},
advanceTimersByTime(ms) {
jestTimer.advanceTimersByTime(ms);
return this;
},
useRealTimers() {
jestTimer.useRealTimers();
return this;
},
};
82 changes: 82 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,20 @@ __metadata:
languageName: node
linkType: hard

"@jest/fake-timers@npm:^29.2.2":
version: 29.2.2
resolution: "@jest/fake-timers@npm:29.2.2"
dependencies:
"@jest/types": ^29.2.1
"@sinonjs/fake-timers": ^9.1.2
"@types/node": "*"
jest-message-util: ^29.2.1
jest-mock: ^29.2.2
jest-util: ^29.2.1
checksum: aa50573ed899d14ce6afef19ea7f607fd2a091e22b37b17eec4a3b046b5236385f8221cff701329439d758badcec5a3b4d9bfc22fc6c7128d34d1b1c21333c73
languageName: node
linkType: hard

"@jest/globals@npm:^29.0.1":
version: 29.0.1
resolution: "@jest/globals@npm:29.0.1"
Expand Down Expand Up @@ -568,6 +582,20 @@ __metadata:
languageName: node
linkType: hard

"@jest/types@npm:^29.2.1":
version: 29.2.1
resolution: "@jest/types@npm:29.2.1"
dependencies:
"@jest/schemas": ^29.0.0
"@types/istanbul-lib-coverage": ^2.0.0
"@types/istanbul-reports": ^3.0.0
"@types/node": "*"
"@types/yargs": ^17.0.8
chalk: ^4.0.0
checksum: a83f20727425179aa05974aa7553c307d207fbb6b7ae5ab1e37fbb6ba9b6655f26655301fc804f2545d33f4c4a6b59d41eed1737c005d2b83fce9e14841b4150
languageName: node
linkType: hard

"@jridgewell/resolve-uri@npm:^3.0.3":
version: 3.0.5
resolution: "@jridgewell/resolve-uri@npm:3.0.5"
Expand Down Expand Up @@ -1655,6 +1683,7 @@ __metadata:
resolution: "jest-light-runner@workspace:."
dependencies:
"@jest/expect": ^29.0.1
"@jest/fake-timers": ^29.2.2
jest-circus: ^29.0.1
jest-each: ^29.0.1
jest-mock: ^29.0.1
Expand Down Expand Up @@ -1696,6 +1725,23 @@ __metadata:
languageName: node
linkType: hard

"jest-message-util@npm:^29.2.1":
version: 29.2.1
resolution: "jest-message-util@npm:29.2.1"
dependencies:
"@babel/code-frame": ^7.12.13
"@jest/types": ^29.2.1
"@types/stack-utils": ^2.0.0
chalk: ^4.0.0
graceful-fs: ^4.2.9
micromatch: ^4.0.4
pretty-format: ^29.2.1
slash: ^3.0.0
stack-utils: ^2.0.3
checksum: 1ec1341dea7f0f04dfa9912647e5c4a092954c122becd9560e43e317407fd401745d99766048be7ee5f0b0b5ff09c84d3c853aa777af57139050efed0ad78376
languageName: node
linkType: hard

"jest-mock@npm:^29.0.1":
version: 29.0.1
resolution: "jest-mock@npm:29.0.1"
Expand All @@ -1706,6 +1752,17 @@ __metadata:
languageName: node
linkType: hard

"jest-mock@npm:^29.2.2":
version: 29.2.2
resolution: "jest-mock@npm:29.2.2"
dependencies:
"@jest/types": ^29.2.1
"@types/node": "*"
jest-util: ^29.2.1
checksum: 9efc3519f50cc911bf759ec65434222c0d80ca033eede5705deb63e0cdad1e25d59e27691aec125d19cb312d46a09509ed205779b4fe60e64cbf54a76903ba58
languageName: node
linkType: hard

"jest-pnp-resolver@npm:^1.2.2":
version: 1.2.2
resolution: "jest-pnp-resolver@npm:1.2.2"
Expand Down Expand Up @@ -1818,6 +1875,20 @@ __metadata:
languageName: node
linkType: hard

"jest-util@npm:^29.2.1":
version: 29.2.1
resolution: "jest-util@npm:29.2.1"
dependencies:
"@jest/types": ^29.2.1
"@types/node": "*"
chalk: ^4.0.0
ci-info: ^3.2.0
graceful-fs: ^4.2.9
picomatch: ^2.2.3
checksum: 781bd14a65599d24b7449877020f4da32e8cb8fbc31c4e849c589ffde58f0eec27de9f690dba182e3ca369fe651c0bb9c307de29a0927d12777677ded56bafb8
languageName: node
linkType: hard

"jest-validate@npm:^29.0.1":
version: 29.0.1
resolution: "jest-validate@npm:29.0.1"
Expand Down Expand Up @@ -2309,6 +2380,17 @@ __metadata:
languageName: node
linkType: hard

"pretty-format@npm:^29.2.1":
version: 29.2.1
resolution: "pretty-format@npm:29.2.1"
dependencies:
"@jest/schemas": ^29.0.0
ansi-styles: ^5.0.0
react-is: ^18.0.0
checksum: d192cbd3dee72e9b60764629d1f098d60fddc3fc9435f44774a01dd1c5794f36a81fa6a7377a527f994317950d8fc6c5bf9c9915387c5d32f107525996e32a1c
languageName: node
linkType: hard

"promise-inflight@npm:^1.0.1":
version: 1.0.1
resolution: "promise-inflight@npm:1.0.1"
Expand Down