From f70c255fc6e9086ce998b202633c481b89e8a902 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 26 Feb 2019 00:07:20 +0100 Subject: [PATCH] chore: extract FakeTimers into a separate package --- CHANGELOG.md | 1 + packages/jest-environment-jsdom/package.json | 1 + packages/jest-environment-jsdom/src/index.js | 3 +- packages/jest-environment-node/package.json | 1 + packages/jest-environment-node/src/index.js | 3 +- packages/jest-environment/package.json | 1 + packages/jest-environment/src/index.ts | 29 +++++++------------ packages/jest-environment/tsconfig.json | 1 + packages/jest-fake-timers/.npmignore | 3 ++ packages/jest-fake-timers/package.json | 22 ++++++++++++++ .../src/__tests__/index.test.ts} | 6 ++-- .../src/index.ts} | 22 +++++++------- packages/jest-fake-timers/tsconfig.json | 11 +++++++ packages/jest-util/package.json | 3 +- packages/jest-util/src/index.ts | 3 +- packages/jest-util/tsconfig.json | 5 ++-- 16 files changed, 75 insertions(+), 40 deletions(-) create mode 100644 packages/jest-fake-timers/.npmignore create mode 100644 packages/jest-fake-timers/package.json rename packages/{jest-util/src/__tests__/fakeTimers.test.ts => jest-fake-timers/src/__tests__/index.test.ts} (99%) rename packages/{jest-util/src/FakeTimers.ts => jest-fake-timers/src/index.ts} (97%) create mode 100644 packages/jest-fake-timers/tsconfig.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ff03985fa81..08835071522c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -59,6 +59,7 @@ - `[jest-phabricator]`: Migrate to TypeScript ([#7965](https://github.com/facebook/jest/pull/7965)) - `[jest-runner]`: Migrate to TypeScript ([#7968](https://github.com/facebook/jest/pull/7968)) - `[jest-runtime]`: Migrate to TypeScript ([#7964](https://github.com/facebook/jest/pull/7964)) +- `[@jest/fake-timers]`: Extract FakeTimers class from `jest-util` into a new separate package ([#7987](https://github.com/facebook/jest/pull/7987)) ### Performance diff --git a/packages/jest-environment-jsdom/package.json b/packages/jest-environment-jsdom/package.json index 0fb34d1b04f9..65799a5f7909 100644 --- a/packages/jest-environment-jsdom/package.json +++ b/packages/jest-environment-jsdom/package.json @@ -9,6 +9,7 @@ "license": "MIT", "main": "build/index.js", "dependencies": { + "@jest/fake-timers": "^24.1.0", "jest-mock": "^24.0.0", "jest-util": "^24.0.0", "jsdom": "^11.5.1" diff --git a/packages/jest-environment-jsdom/src/index.js b/packages/jest-environment-jsdom/src/index.js index 52eed2ff069c..e5a25208f50a 100644 --- a/packages/jest-environment-jsdom/src/index.js +++ b/packages/jest-environment-jsdom/src/index.js @@ -12,7 +12,8 @@ import type {EnvironmentContext} from 'types/Environment'; import type {Global} from 'types/Global'; import type {ModuleMocker} from 'jest-mock'; -import {FakeTimers, installCommonGlobals} from 'jest-util'; +import FakeTimers from '@jest/fake-timers'; +import {installCommonGlobals} from 'jest-util'; import mock from 'jest-mock'; import {JSDOM, VirtualConsole} from 'jsdom'; diff --git a/packages/jest-environment-node/package.json b/packages/jest-environment-node/package.json index 0914ca74f5e8..374695a24a69 100644 --- a/packages/jest-environment-node/package.json +++ b/packages/jest-environment-node/package.json @@ -9,6 +9,7 @@ "license": "MIT", "main": "build/index.js", "dependencies": { + "@jest/fake-timers": "^24.1.0", "jest-mock": "^24.0.0", "jest-util": "^24.0.0" }, diff --git a/packages/jest-environment-node/src/index.js b/packages/jest-environment-node/src/index.js index f09b3f7c30a1..f70718f9ff29 100644 --- a/packages/jest-environment-node/src/index.js +++ b/packages/jest-environment-node/src/index.js @@ -13,7 +13,8 @@ import type {Global} from 'types/Global'; import type {ModuleMocker} from 'jest-mock'; import vm from 'vm'; -import {FakeTimers, installCommonGlobals} from 'jest-util'; +import FakeTimers from '@jest/fake-timers'; +import {installCommonGlobals} from 'jest-util'; import mock from 'jest-mock'; type Timer = {| diff --git a/packages/jest-environment/package.json b/packages/jest-environment/package.json index d1888425032d..d9595ec1d00b 100644 --- a/packages/jest-environment/package.json +++ b/packages/jest-environment/package.json @@ -10,6 +10,7 @@ "main": "build/index.js", "types": "build/index.d.ts", "dependencies": { + "@jest/fake-timers": "^24.1.0", "@jest/transform": "^24.1.0", "@jest/types": "^24.1.0", "@types/node": "*", diff --git a/packages/jest-environment/src/index.ts b/packages/jest-environment/src/index.ts index 8f9d1e08502e..31a908de1d0a 100644 --- a/packages/jest-environment/src/index.ts +++ b/packages/jest-environment/src/index.ts @@ -7,8 +7,12 @@ import {Script} from 'vm'; import {Config, Global} from '@jest/types'; -import moduleMocker from 'jest-mock'; +import jestMock, {ModuleMocker} from 'jest-mock'; import {ScriptTransformer} from '@jest/transform'; +import FakeTimers from '@jest/fake-timers'; + +type JestMockFn = typeof jestMock.fn; +type JestMockSpyOn = typeof jestMock.spyOn; export type EnvironmentContext = { console?: Console; @@ -27,21 +31,10 @@ export interface JestEnvironment { script: Script, ): {[ScriptTransformer.EVAL_RESULT_VARIABLE]: ModuleWrapper} | null; global: Global.Global; - // TODO: When `jest-util` is ESM, this can just be `fakeTimers: import('jest-util').FakeTimers` - fakeTimers: { - clearAllTimers(): void; - runAllImmediates(): void; - runAllTicks(): void; - runAllTimers(): void; - advanceTimersByTime(msToRun: number): void; - runOnlyPendingTimers(): void; - runWithRealTimers(callback: () => void): void; - getTimerCount(): number; - useFakeTimers(): void; - useRealTimers(): void; - }; + // TODO: This is nullable, and TS doesn't understand we deal with it in `jest-runtime`. Should be fixed + fakeTimers: FakeTimers; testFilePath: Config.Path; - moduleMocker: typeof moduleMocker; + moduleMocker: ModuleMocker; setup(): Promise; teardown(): Promise; } @@ -112,7 +105,7 @@ export interface Jest { /** * Creates a mock function. Optionally takes a mock implementation. */ - fn: typeof moduleMocker.fn; + fn: JestMockFn; /** * Given the name of a module, use the automatic mocking system to generate a * mocked version of the module for you. @@ -124,7 +117,7 @@ export interface Jest { /** * Determines if the given function is a mocked function. */ - isMockFunction(fn: Function): fn is ReturnType; + isMockFunction(fn: Function): fn is ReturnType; /** * Mocks a module with an auto-mocked version when it is being required. */ @@ -235,7 +228,7 @@ export interface Jest { * Note: By default, jest.spyOn also calls the spied method. This is * different behavior from most other test libraries. */ - spyOn: typeof moduleMocker.spyOn; + spyOn: JestMockSpyOn; /** * Indicates that the module system should never return a mocked version of * the specified module from require() (e.g. that it should always return the diff --git a/packages/jest-environment/tsconfig.json b/packages/jest-environment/tsconfig.json index 8824863bd93b..cfce4b39485b 100644 --- a/packages/jest-environment/tsconfig.json +++ b/packages/jest-environment/tsconfig.json @@ -5,6 +5,7 @@ "outDir": "build" }, "references": [ + {"path": "../jest-fake-timers"}, {"path": "../jest-transform"}, {"path": "../jest-types"}, {"path": "../jest-util"} diff --git a/packages/jest-fake-timers/.npmignore b/packages/jest-fake-timers/.npmignore new file mode 100644 index 000000000000..85e48fe7b0a4 --- /dev/null +++ b/packages/jest-fake-timers/.npmignore @@ -0,0 +1,3 @@ +**/__mocks__/** +**/__tests__/** +src diff --git a/packages/jest-fake-timers/package.json b/packages/jest-fake-timers/package.json new file mode 100644 index 000000000000..988be66bb72f --- /dev/null +++ b/packages/jest-fake-timers/package.json @@ -0,0 +1,22 @@ +{ + "name": "@jest/fake-timers", + "version": "24.1.0", + "repository": { + "type": "git", + "url": "https://github.com/facebook/jest.git", + "directory": "packages/jest-fake-timers" + }, + "license": "MIT", + "main": "build/index.js", + "types": "build/index.d.ts", + "dependencies": { + "@jest/types": "^24.1.0", + "@types/node": "*", + "jest-message-util": "^24.0.0", + "jest-mock": "^24.0.0" + }, + "engines": { + "node": ">= 6" + }, + "gitHead": "b16789230fd45056a7f2fa199bae06c7a1780deb" +} diff --git a/packages/jest-util/src/__tests__/fakeTimers.test.ts b/packages/jest-fake-timers/src/__tests__/index.test.ts similarity index 99% rename from packages/jest-util/src/__tests__/fakeTimers.test.ts rename to packages/jest-fake-timers/src/__tests__/index.test.ts index 7d53b0c91e51..1e591f729264 100644 --- a/packages/jest-util/src/__tests__/fakeTimers.test.ts +++ b/packages/jest-fake-timers/src/__tests__/index.test.ts @@ -7,9 +7,7 @@ import vm from 'vm'; import mock from 'jest-mock'; -import FakeTimers from '../FakeTimers'; -// TODO: import this type directly from jest-mock once TS migration is done -type ModuleMocker = typeof mock; +import FakeTimers from '../'; const timerConfig = { idToRef: (id: number) => id, @@ -22,7 +20,7 @@ const config = { }; describe('FakeTimers', () => { - let moduleMocker: ModuleMocker; + let moduleMocker: mock.ModuleMocker; beforeEach(() => { const global = vm.runInNewContext('this'); diff --git a/packages/jest-util/src/FakeTimers.ts b/packages/jest-fake-timers/src/index.ts similarity index 97% rename from packages/jest-util/src/FakeTimers.ts rename to packages/jest-fake-timers/src/index.ts index aea4916981b5..9dca4a0ac1d2 100644 --- a/packages/jest-util/src/FakeTimers.ts +++ b/packages/jest-fake-timers/src/index.ts @@ -5,18 +5,10 @@ * LICENSE file in the root directory of this source tree. */ -import mock from 'jest-mock'; +import {ModuleMocker} from 'jest-mock'; import {formatStackTrace, StackTraceConfig} from 'jest-message-util'; -import setGlobal from './setGlobal'; -type ModuleMocker = typeof mock; - -/** - * We don't know the type of arguments for a callback ahead of time which is why - * we are disabling the flowtype/no-weak-types rule here. - */ - -type Callback = (...args: any) => void; +type Callback = (...args: Array) => void; type TimerID = string; @@ -50,6 +42,16 @@ type TimerConfig = { const MS_IN_A_YEAR = 31536000000; +// TODO: Copied from `jest-util` to avoid cyclic dependency. Import from `jest-util` in the next major +const setGlobal = ( + globalToMutate: NodeJS.Global | Window, + key: string, + value: unknown, +) => { + // @ts-ignore: no index + globalToMutate[key] = value; +}; + export default class FakeTimers { private _cancelledImmediates!: {[key: string]: boolean}; private _cancelledTicks!: {[key: string]: boolean}; diff --git a/packages/jest-fake-timers/tsconfig.json b/packages/jest-fake-timers/tsconfig.json new file mode 100644 index 000000000000..335b1e9c373a --- /dev/null +++ b/packages/jest-fake-timers/tsconfig.json @@ -0,0 +1,11 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "rootDir": "src", + "outDir": "build" + }, + "references": [ + {"path": "../jest-message-util"}, + {"path": "../jest-mock"} + ] +} diff --git a/packages/jest-util/package.json b/packages/jest-util/package.json index 13e9e9f6c706..4fceefa7a5da 100644 --- a/packages/jest-util/package.json +++ b/packages/jest-util/package.json @@ -10,14 +10,13 @@ "main": "build/index.js", "types": "build/index.d.ts", "dependencies": { + "@jest/fake-timers": "^24.1.0", "@jest/types": "^24.1.0", "@types/node": "*", "callsites": "^3.0.0", "chalk": "^2.0.1", "graceful-fs": "^4.1.15", "is-ci": "^2.0.0", - "jest-message-util": "^24.0.0", - "jest-mock": "^24.0.0", "mkdirp": "^0.5.1", "readable-stream": "^3.1.1", "slash": "^2.0.0", diff --git a/packages/jest-util/src/index.ts b/packages/jest-util/src/index.ts index 66c38c2b63e0..1db987e58574 100644 --- a/packages/jest-util/src/index.ts +++ b/packages/jest-util/src/index.ts @@ -5,12 +5,13 @@ * LICENSE file in the root directory of this source tree. */ +// TODO: Remove this export in the next major +import FakeTimers from '@jest/fake-timers'; import BufferedConsole from './BufferedConsole'; import clearLine from './clearLine'; import CustomConsole from './CustomConsole'; import createDirectory from './createDirectory'; import ErrorWithStack from './ErrorWithStack'; -import FakeTimers from './FakeTimers'; import formatTestResults from './formatTestResults'; import getFailedSnapshotTests from './getFailedSnapshotTests'; import getConsoleOutput from './getConsoleOutput'; diff --git a/packages/jest-util/tsconfig.json b/packages/jest-util/tsconfig.json index c8a0f5ac5aa8..876baa6cf5c8 100644 --- a/packages/jest-util/tsconfig.json +++ b/packages/jest-util/tsconfig.json @@ -5,8 +5,7 @@ "outDir": "build" }, "references": [ - {"path": "../jest-types"}, - {"path": "../jest-message-util"}, - {"path": "../jest-mock"} + {"path": "../jest-fake-timers"}, + {"path": "../jest-types"} ] }