Skip to content

Commit

Permalink
chore: extract FakeTimers into a separate package
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Feb 25, 2019
1 parent 51817fd commit 62559db
Show file tree
Hide file tree
Showing 16 changed files with 75 additions and 40 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

### Performance

Expand Down
1 change: 1 addition & 0 deletions packages/jest-environment-jsdom/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion packages/jest-environment-jsdom/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
1 change: 1 addition & 0 deletions packages/jest-environment-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
3 changes: 2 additions & 1 deletion packages/jest-environment-node/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {|
Expand Down
1 change: 1 addition & 0 deletions packages/jest-environment/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "*",
Expand Down
29 changes: 11 additions & 18 deletions packages/jest-environment/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<unknown>;
testFilePath: Config.Path;
moduleMocker: typeof moduleMocker;
moduleMocker: ModuleMocker;
setup(): Promise<void>;
teardown(): Promise<void>;
}
Expand Down Expand Up @@ -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.
Expand All @@ -124,7 +117,7 @@ export interface Jest {
/**
* Determines if the given function is a mocked function.
*/
isMockFunction(fn: Function): fn is ReturnType<typeof moduleMocker.fn>;
isMockFunction(fn: Function): fn is ReturnType<JestMockFn>;
/**
* Mocks a module with an auto-mocked version when it is being required.
*/
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions packages/jest-environment/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"outDir": "build"
},
"references": [
{"path": "../jest-fake-timers"},
{"path": "../jest-transform"},
{"path": "../jest-types"},
{"path": "../jest-util"}
Expand Down
3 changes: 3 additions & 0 deletions packages/jest-fake-timers/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**/__mocks__/**
**/__tests__/**
src
22 changes: 22 additions & 0 deletions packages/jest-fake-timers/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -22,7 +20,7 @@ const config = {
};

describe('FakeTimers', () => {
let moduleMocker: ModuleMocker;
let moduleMocker: mock.ModuleMocker;

beforeEach(() => {
const global = vm.runInNewContext('this');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<unknown>) => void;

type TimerID = string;

Expand Down Expand Up @@ -50,6 +42,16 @@ type TimerConfig<Ref> = {

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<TimerRef> {
private _cancelledImmediates!: {[key: string]: boolean};
private _cancelledTicks!: {[key: string]: boolean};
Expand Down
11 changes: 11 additions & 0 deletions packages/jest-fake-timers/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "build"
},
"references": [
{"path": "../jest-message-util"},
{"path": "../jest-mock"}
]
}
3 changes: 1 addition & 2 deletions packages/jest-util/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion packages/jest-util/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
5 changes: 2 additions & 3 deletions packages/jest-util/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
"outDir": "build"
},
"references": [
{"path": "../jest-types"},
{"path": "../jest-message-util"},
{"path": "../jest-mock"}
{"path": "../jest-fake-timers"},
{"path": "../jest-types"}
]
}

0 comments on commit 62559db

Please sign in to comment.