From 16fe18d62c34713a6758c2fb216c31834115807a Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 26 Feb 2019 11:05:17 +0100 Subject: [PATCH] chore: cleanup JestEnvironment types (#7988) --- CHANGELOG.md | 2 +- .../src/legacy-code-todo-rewrite/jestAdapter.ts | 6 ++++-- packages/jest-environment/src/index.ts | 16 ++++++---------- packages/jest-runner/src/runTest.ts | 2 +- packages/jest-runtime/src/cli/index.ts | 2 +- packages/jest-runtime/src/index.ts | 16 +++++++++------- 6 files changed, 22 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c28550f79bee..e6e9e2d3890b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -59,7 +59,7 @@ - `[jest-circus]`: Migrate to TypeScript ([#7916](https://github.com/facebook/jest/pull/7916)) - `[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-runtime]`: Migrate to TypeScript ([#7964](https://github.com/facebook/jest/pull/7964), [#7988](https://github.com/facebook/jest/pull/7988)) - `[@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-circus/src/legacy-code-todo-rewrite/jestAdapter.ts b/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapter.ts index 7064ad335553..7ad289942726 100644 --- a/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapter.ts +++ b/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapter.ts @@ -47,7 +47,8 @@ const jestAdapter = async ( }); if (config.timers === 'fake') { - environment.fakeTimers.useFakeTimers(); + // during setup, this cannot be null (and it's fine to explode if it is) + environment.fakeTimers!.useFakeTimers(); } globals.beforeEach(() => { @@ -63,7 +64,8 @@ const jestAdapter = async ( runtime.resetAllMocks(); if (config.timers === 'fake') { - environment.fakeTimers.useFakeTimers(); + // during setup, this cannot be null (and it's fine to explode if it is) + environment.fakeTimers!.useFakeTimers(); } } diff --git a/packages/jest-environment/src/index.ts b/packages/jest-environment/src/index.ts index d8a6aed05f4e..a29270083abb 100644 --- a/packages/jest-environment/src/index.ts +++ b/packages/jest-environment/src/index.ts @@ -22,19 +22,15 @@ export type EnvironmentContext = { // TODO: type this better: https://nodejs.org/api/modules.html#modules_the_module_wrapper type ModuleWrapper = (...args: Array) => unknown; -export interface JestEnvironment { - new ( - config: Config.ProjectConfig, - context?: EnvironmentContext, - ): JestEnvironment; +export declare class JestEnvironment { + constructor(config: Config.ProjectConfig); + constructor(config: Config.ProjectConfig, context: EnvironmentContext); + global: Global.Global; + fakeTimers: FakeTimers | null; + moduleMocker: ModuleMocker | null; runScript( script: Script, ): {[ScriptTransformer.EVAL_RESULT_VARIABLE]: ModuleWrapper} | null; - global: Global.Global; - // 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: ModuleMocker; setup(): Promise; teardown(): Promise; } diff --git a/packages/jest-runner/src/runTest.ts b/packages/jest-runner/src/runTest.ts index 94c759a5188f..22e7cd57465b 100644 --- a/packages/jest-runner/src/runTest.ts +++ b/packages/jest-runner/src/runTest.ts @@ -96,7 +96,7 @@ async function runTestInternal( }); } - const TestEnvironment: JestEnvironment = require(testEnvironment); + const TestEnvironment: typeof JestEnvironment = require(testEnvironment); const testFramework: TestFramework = process.env.JEST_CIRCUS === '1' ? require('jest-circus/runner') // eslint-disable-line import/no-extraneous-dependencies diff --git a/packages/jest-runtime/src/cli/index.ts b/packages/jest-runtime/src/cli/index.ts index cb4527e312ec..5a6284388da5 100644 --- a/packages/jest-runtime/src/cli/index.ts +++ b/packages/jest-runtime/src/cli/index.ts @@ -80,7 +80,7 @@ export function run(cliArgv?: Config.Argv, cliInfo?: Array) { watchman: globalConfig.watchman, }) as Promise) .then(hasteMap => { - const Environment: JestEnvironment = require(config.testEnvironment); + const Environment: typeof JestEnvironment = require(config.testEnvironment); const environment = new Environment(config); setGlobal( environment.global, diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 6c8849a792ee..694738227f3a 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -125,7 +125,8 @@ class Runtime { this._isCurrentlyExecutingManualMock = null; this._mockFactories = Object.create(null); this._mockRegistry = Object.create(null); - this._moduleMocker = this._environment.moduleMocker; + // during setup, this cannot be null (and it's fine to explode if it is) + this._moduleMocker = this._environment.moduleMocker!; this._isolatedModuleRegistry = null; this._isolatedMockRegistry = null; this._moduleRegistry = Object.create(null); @@ -170,7 +171,7 @@ class Runtime { } } - // TODO: Can this be `static shouldInstrument = shouldInstrument;`? + // TODO: Make this `static shouldInstrument = shouldInstrument;` after https://github.com/facebook/jest/issues/7846 static shouldInstrument( filename: Config.Path, options: ShouldInstrumentOptions, @@ -934,11 +935,11 @@ class Runtime { return jestObject; }; const useFakeTimers = () => { - this._environment.fakeTimers.useFakeTimers(); + _getFakeTimers().useFakeTimers(); return jestObject; }; const useRealTimers = () => { - this._environment.fakeTimers.useRealTimers(); + _getFakeTimers().useRealTimers(); return jestObject; }; const resetModules = () => { @@ -968,7 +969,7 @@ class Runtime { return jestObject; }; - const _getFakeTimers = () => { + const _getFakeTimers = (): NonNullable => { if (!this._environment.fakeTimers) { this._logFormattedReferenceError( 'You are trying to access a property or method of the Jest environment after it has been torn down.', @@ -976,7 +977,8 @@ class Runtime { process.exitCode = 1; } - return this._environment.fakeTimers; + // We've logged a user message above, so it doesn't matter if we return `null` here + return this._environment.fakeTimers!; }; const jestObject: Jest = { @@ -1024,7 +1026,7 @@ class Runtime { return jestObject; } - _logFormattedReferenceError(errorMessage: string) { + private _logFormattedReferenceError(errorMessage: string) { const originalStack = new ReferenceError(errorMessage) .stack!.split('\n') // Remove this file from the stack (jest-message-utils will keep one line)