Skip to content

Commit f70c255

Browse files
committed
chore: extract FakeTimers into a separate package
1 parent 51817fd commit f70c255

File tree

16 files changed

+75
-40
lines changed

16 files changed

+75
-40
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
- `[jest-phabricator]`: Migrate to TypeScript ([#7965](https://github.com/facebook/jest/pull/7965))
6060
- `[jest-runner]`: Migrate to TypeScript ([#7968](https://github.com/facebook/jest/pull/7968))
6161
- `[jest-runtime]`: Migrate to TypeScript ([#7964](https://github.com/facebook/jest/pull/7964))
62+
- `[@jest/fake-timers]`: Extract FakeTimers class from `jest-util` into a new separate package ([#7987](https://github.com/facebook/jest/pull/7987))
6263

6364
### Performance
6465

packages/jest-environment-jsdom/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"license": "MIT",
1010
"main": "build/index.js",
1111
"dependencies": {
12+
"@jest/fake-timers": "^24.1.0",
1213
"jest-mock": "^24.0.0",
1314
"jest-util": "^24.0.0",
1415
"jsdom": "^11.5.1"

packages/jest-environment-jsdom/src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import type {EnvironmentContext} from 'types/Environment';
1212
import type {Global} from 'types/Global';
1313
import type {ModuleMocker} from 'jest-mock';
1414

15-
import {FakeTimers, installCommonGlobals} from 'jest-util';
15+
import FakeTimers from '@jest/fake-timers';
16+
import {installCommonGlobals} from 'jest-util';
1617
import mock from 'jest-mock';
1718
import {JSDOM, VirtualConsole} from 'jsdom';
1819

packages/jest-environment-node/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"license": "MIT",
1010
"main": "build/index.js",
1111
"dependencies": {
12+
"@jest/fake-timers": "^24.1.0",
1213
"jest-mock": "^24.0.0",
1314
"jest-util": "^24.0.0"
1415
},

packages/jest-environment-node/src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import type {Global} from 'types/Global';
1313
import type {ModuleMocker} from 'jest-mock';
1414

1515
import vm from 'vm';
16-
import {FakeTimers, installCommonGlobals} from 'jest-util';
16+
import FakeTimers from '@jest/fake-timers';
17+
import {installCommonGlobals} from 'jest-util';
1718
import mock from 'jest-mock';
1819

1920
type Timer = {|

packages/jest-environment/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"main": "build/index.js",
1111
"types": "build/index.d.ts",
1212
"dependencies": {
13+
"@jest/fake-timers": "^24.1.0",
1314
"@jest/transform": "^24.1.0",
1415
"@jest/types": "^24.1.0",
1516
"@types/node": "*",

packages/jest-environment/src/index.ts

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@
77

88
import {Script} from 'vm';
99
import {Config, Global} from '@jest/types';
10-
import moduleMocker from 'jest-mock';
10+
import jestMock, {ModuleMocker} from 'jest-mock';
1111
import {ScriptTransformer} from '@jest/transform';
12+
import FakeTimers from '@jest/fake-timers';
13+
14+
type JestMockFn = typeof jestMock.fn;
15+
type JestMockSpyOn = typeof jestMock.spyOn;
1216

1317
export type EnvironmentContext = {
1418
console?: Console;
@@ -27,21 +31,10 @@ export interface JestEnvironment {
2731
script: Script,
2832
): {[ScriptTransformer.EVAL_RESULT_VARIABLE]: ModuleWrapper} | null;
2933
global: Global.Global;
30-
// TODO: When `jest-util` is ESM, this can just be `fakeTimers: import('jest-util').FakeTimers`
31-
fakeTimers: {
32-
clearAllTimers(): void;
33-
runAllImmediates(): void;
34-
runAllTicks(): void;
35-
runAllTimers(): void;
36-
advanceTimersByTime(msToRun: number): void;
37-
runOnlyPendingTimers(): void;
38-
runWithRealTimers(callback: () => void): void;
39-
getTimerCount(): number;
40-
useFakeTimers(): void;
41-
useRealTimers(): void;
42-
};
34+
// TODO: This is nullable, and TS doesn't understand we deal with it in `jest-runtime`. Should be fixed
35+
fakeTimers: FakeTimers<unknown>;
4336
testFilePath: Config.Path;
44-
moduleMocker: typeof moduleMocker;
37+
moduleMocker: ModuleMocker;
4538
setup(): Promise<void>;
4639
teardown(): Promise<void>;
4740
}
@@ -112,7 +105,7 @@ export interface Jest {
112105
/**
113106
* Creates a mock function. Optionally takes a mock implementation.
114107
*/
115-
fn: typeof moduleMocker.fn;
108+
fn: JestMockFn;
116109
/**
117110
* Given the name of a module, use the automatic mocking system to generate a
118111
* mocked version of the module for you.
@@ -124,7 +117,7 @@ export interface Jest {
124117
/**
125118
* Determines if the given function is a mocked function.
126119
*/
127-
isMockFunction(fn: Function): fn is ReturnType<typeof moduleMocker.fn>;
120+
isMockFunction(fn: Function): fn is ReturnType<JestMockFn>;
128121
/**
129122
* Mocks a module with an auto-mocked version when it is being required.
130123
*/
@@ -235,7 +228,7 @@ export interface Jest {
235228
* Note: By default, jest.spyOn also calls the spied method. This is
236229
* different behavior from most other test libraries.
237230
*/
238-
spyOn: typeof moduleMocker.spyOn;
231+
spyOn: JestMockSpyOn;
239232
/**
240233
* Indicates that the module system should never return a mocked version of
241234
* the specified module from require() (e.g. that it should always return the

packages/jest-environment/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"outDir": "build"
66
},
77
"references": [
8+
{"path": "../jest-fake-timers"},
89
{"path": "../jest-transform"},
910
{"path": "../jest-types"},
1011
{"path": "../jest-util"}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
**/__mocks__/**
2+
**/__tests__/**
3+
src
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "@jest/fake-timers",
3+
"version": "24.1.0",
4+
"repository": {
5+
"type": "git",
6+
"url": "https://github.com/facebook/jest.git",
7+
"directory": "packages/jest-fake-timers"
8+
},
9+
"license": "MIT",
10+
"main": "build/index.js",
11+
"types": "build/index.d.ts",
12+
"dependencies": {
13+
"@jest/types": "^24.1.0",
14+
"@types/node": "*",
15+
"jest-message-util": "^24.0.0",
16+
"jest-mock": "^24.0.0"
17+
},
18+
"engines": {
19+
"node": ">= 6"
20+
},
21+
"gitHead": "b16789230fd45056a7f2fa199bae06c7a1780deb"
22+
}

0 commit comments

Comments
 (0)