From 3079f296f675fc217709759ca3a1ade9134edbdf Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 4 Nov 2020 19:37:27 +0100 Subject: [PATCH] chore: convert jest-runtime to ESM (#10325) --- CHANGELOG.md | 1 + .../src/legacy-code-todo-rewrite/jestAdapter.ts | 2 +- .../jest-core/src/__tests__/SearchSource.test.ts | 2 +- .../src/__tests__/watchFileChanges.test.ts | 2 +- packages/jest-core/src/cli/index.ts | 6 +++--- packages/jest-core/src/lib/createContext.ts | 4 ++-- packages/jest-jasmine2/src/index.ts | 2 +- packages/jest-repl/src/cli/args.ts | 2 +- packages/jest-repl/src/cli/index.ts | 2 +- .../src/__tests__/dependency_resolver.test.ts | 2 +- packages/jest-runner/src/runTest.ts | 2 +- packages/jest-runner/src/testWorker.ts | 2 +- packages/jest-runner/src/types.ts | 2 +- packages/jest-runtime/src/cli/index.ts | 2 +- packages/jest-runtime/src/index.ts | 16 +++++----------- 15 files changed, 22 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 751f8cc453ca..1e21fe0afb67 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ - `[jest-resolve]` Replace read-pkg-up with escalade package ([#10781](https://github.com/facebook/jest/pull/10781)) - `[jest-runtime]` [**BREAKING**] Do not inject `global` variable into module wrapper ([#10644](https://github.com/facebook/jest/pull/10644)) +- `[jest-runtime]` [**BREAKING**] Convert to ESM ([#10325](https://github.com/facebook/jest/pull/10325)) - `[jest-transform]` Show enhanced `SyntaxError` message for all `SyntaxError`s ([#10749](https://github.com/facebook/jest/pull/10749)) - `[jest-transform]` [**BREAKING**] Refactor API to pass an options bag around rather than multiple boolean options ([#10753](https://github.com/facebook/jest/pull/10753)) 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 f20ef5c0df68..50e5f1abe620 100644 --- a/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapter.ts +++ b/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapter.ts @@ -9,7 +9,7 @@ import type {JestEnvironment} from '@jest/environment'; import type {TestResult} from '@jest/test-result'; import type {Config} from '@jest/types'; import type {TestFileEvent} from 'jest-runner'; -import type {RuntimeType as Runtime} from 'jest-runtime'; +import type Runtime from 'jest-runtime'; import type {SnapshotStateType} from 'jest-snapshot'; import {deepCyclicCopy} from 'jest-util'; diff --git a/packages/jest-core/src/__tests__/SearchSource.test.ts b/packages/jest-core/src/__tests__/SearchSource.test.ts index 2f96cc1b4fe4..86c0d73b32b4 100644 --- a/packages/jest-core/src/__tests__/SearchSource.test.ts +++ b/packages/jest-core/src/__tests__/SearchSource.test.ts @@ -10,7 +10,7 @@ import * as path from 'path'; import type {Config} from '@jest/types'; import {normalize} from 'jest-config'; import {Test} from 'jest-runner'; -import Runtime = require('jest-runtime'); +import Runtime from 'jest-runtime'; import SearchSource, {SearchResult} from '../SearchSource'; jest.setTimeout(15000); diff --git a/packages/jest-core/src/__tests__/watchFileChanges.test.ts b/packages/jest-core/src/__tests__/watchFileChanges.test.ts index 79c4c93f29e5..a2866f6f9b46 100644 --- a/packages/jest-core/src/__tests__/watchFileChanges.test.ts +++ b/packages/jest-core/src/__tests__/watchFileChanges.test.ts @@ -13,7 +13,7 @@ import rimraf = require('rimraf'); import type {AggregatedResult} from '@jest/test-result'; import {normalize} from 'jest-config'; import HasteMap = require('jest-haste-map'); -import Runtime = require('jest-runtime'); +import Runtime from 'jest-runtime'; import {JestHook} from 'jest-watcher'; describe('Watch mode flows with changed files', () => { diff --git a/packages/jest-core/src/cli/index.ts b/packages/jest-core/src/cli/index.ts index c115e865dd09..ca67513a2d4d 100644 --- a/packages/jest-core/src/cli/index.ts +++ b/packages/jest-core/src/cli/index.ts @@ -14,7 +14,7 @@ import type {Config} from '@jest/types'; import type {ChangedFilesPromise} from 'jest-changed-files'; import {readConfigs} from 'jest-config'; import HasteMap = require('jest-haste-map'); -import Runtime = require('jest-runtime'); +import Runtime, {Context} from 'jest-runtime'; import {createDirectory, preRunMessage} from 'jest-util'; import TestWatcher from '../TestWatcher'; import {formatHandleErrors} from '../collectHandles'; @@ -218,7 +218,7 @@ const _run10000 = async ( }; const runWatch = async ( - contexts: Array, + contexts: Array, _configs: Array, hasDeprecationWarnings: boolean, globalConfig: Config.GlobalConfig, @@ -256,7 +256,7 @@ const runWatch = async ( const runWithoutWatch = async ( globalConfig: Config.GlobalConfig, - contexts: Array, + contexts: Array, outputStream: NodeJS.WriteStream, onComplete: OnCompleteCallback, changedFilesPromise?: ChangedFilesPromise, diff --git a/packages/jest-core/src/lib/createContext.ts b/packages/jest-core/src/lib/createContext.ts index 9937a1342ff5..0cd6c5b23677 100644 --- a/packages/jest-core/src/lib/createContext.ts +++ b/packages/jest-core/src/lib/createContext.ts @@ -7,12 +7,12 @@ import type {Config} from '@jest/types'; import type {HasteMapObject} from 'jest-haste-map'; -import Runtime = require('jest-runtime'); +import Runtime, {Context} from 'jest-runtime'; export default ( config: Config.ProjectConfig, {hasteFS, moduleMap}: HasteMapObject, -): Runtime.Context => ({ +): Context => ({ config, hasteFS, moduleMap, diff --git a/packages/jest-jasmine2/src/index.ts b/packages/jest-jasmine2/src/index.ts index b0b3069a971e..1056dcec7330 100644 --- a/packages/jest-jasmine2/src/index.ts +++ b/packages/jest-jasmine2/src/index.ts @@ -10,7 +10,7 @@ import type {JestEnvironment} from '@jest/environment'; import {getCallsite} from '@jest/source-map'; import type {AssertionResult, TestResult} from '@jest/test-result'; import type {Config, Global} from '@jest/types'; -import type {RuntimeType as Runtime} from 'jest-runtime'; +import type Runtime from 'jest-runtime'; import type {SnapshotStateType} from 'jest-snapshot'; import installEach from './each'; import {installErrorOnPrivate} from './errorOnPrivate'; diff --git a/packages/jest-repl/src/cli/args.ts b/packages/jest-repl/src/cli/args.ts index 3e4a194682b6..43dc5793f1f9 100644 --- a/packages/jest-repl/src/cli/args.ts +++ b/packages/jest-repl/src/cli/args.ts @@ -7,7 +7,7 @@ */ import type {Options} from 'yargs'; -import Runtime = require('jest-runtime'); +import Runtime from 'jest-runtime'; export const usage = 'Usage: $0 [--config=]'; diff --git a/packages/jest-repl/src/cli/index.ts b/packages/jest-repl/src/cli/index.ts index e756fc2fef13..58a8f2db1482 100644 --- a/packages/jest-repl/src/cli/index.ts +++ b/packages/jest-repl/src/cli/index.ts @@ -10,7 +10,7 @@ import yargs = require('yargs'); import type {Config} from '@jest/types'; import {deprecationEntries} from 'jest-config'; -import Runtime = require('jest-runtime'); +import Runtime from 'jest-runtime'; import {validateCLIOptions} from 'jest-validate'; import * as args from './args'; const {version: VERSION} = require('../../package.json'); diff --git a/packages/jest-resolve-dependencies/src/__tests__/dependency_resolver.test.ts b/packages/jest-resolve-dependencies/src/__tests__/dependency_resolver.test.ts index eeacf0056acc..6198e25d39f3 100644 --- a/packages/jest-resolve-dependencies/src/__tests__/dependency_resolver.test.ts +++ b/packages/jest-resolve-dependencies/src/__tests__/dependency_resolver.test.ts @@ -26,7 +26,7 @@ const filter = (path: Config.Path) => Object.keys(cases).every(key => cases[key](path)); beforeEach(() => { - Runtime = require('jest-runtime'); + Runtime = require('jest-runtime').default; config = makeProjectConfig({ cacheDirectory: path.resolve(tmpdir(), 'jest-resolve-dependencies-test'), moduleDirectories: ['node_modules'], diff --git a/packages/jest-runner/src/runTest.ts b/packages/jest-runner/src/runTest.ts index 2ccfd7e3cebd..1174753dfb5d 100644 --- a/packages/jest-runner/src/runTest.ts +++ b/packages/jest-runner/src/runTest.ts @@ -25,7 +25,7 @@ import * as docblock from 'jest-docblock'; import LeakDetector from 'jest-leak-detector'; import {formatExecError} from 'jest-message-util'; import type Resolver from 'jest-resolve'; -import RuntimeClass = require('jest-runtime'); +import type RuntimeClass from 'jest-runtime'; import {ErrorWithStack, interopRequireDefault, setGlobal} from 'jest-util'; import type {TestFileEvent, TestFramework, TestRunnerContext} from './types'; diff --git a/packages/jest-runner/src/testWorker.ts b/packages/jest-runner/src/testWorker.ts index 2284a856fcb3..fff6e741e32e 100644 --- a/packages/jest-runner/src/testWorker.ts +++ b/packages/jest-runner/src/testWorker.ts @@ -12,7 +12,7 @@ import type {Config} from '@jest/types'; import HasteMap = require('jest-haste-map'); import {separateMessageFromStack} from 'jest-message-util'; import type Resolver from 'jest-resolve'; -import Runtime = require('jest-runtime'); +import Runtime from 'jest-runtime'; import {messageParent} from 'jest-worker'; import runTest from './runTest'; import type { diff --git a/packages/jest-runner/src/types.ts b/packages/jest-runner/src/types.ts index f90be3e2c201..3d425c045f24 100644 --- a/packages/jest-runner/src/types.ts +++ b/packages/jest-runner/src/types.ts @@ -15,7 +15,7 @@ import type { import type {Config} from '@jest/types'; import type {FS as HasteFS, ModuleMap} from 'jest-haste-map'; import type Resolver from 'jest-resolve'; -import type {RuntimeType} from 'jest-runtime'; +import type RuntimeType from 'jest-runtime'; export type ErrorWithCode = Error & {code?: string}; export type Test = { diff --git a/packages/jest-runtime/src/cli/index.ts b/packages/jest-runtime/src/cli/index.ts index 270a3c82a849..3add89691abc 100644 --- a/packages/jest-runtime/src/cli/index.ts +++ b/packages/jest-runtime/src/cli/index.ts @@ -66,7 +66,7 @@ export async function run( automock: false, }; - const Runtime: typeof import('..') = require('..'); + const {default: Runtime} = await import('..'); try { const hasteMap = await Runtime.createContext(config, { diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 8f4b830af5b1..0b679ee3f47f 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -55,7 +55,9 @@ import { decodePossibleOutsideJestVmPath, findSiblingsWithFileExtension, } from './helpers'; -import type {Context as JestContext} from './types'; +import type {Context} from './types'; + +export type {Context} from './types'; const esmIsAvailable = typeof SourceTextModule === 'function'; @@ -109,12 +111,6 @@ const fromEntries: typeof Object.fromEntries = }, {}); }; -declare namespace Runtime { - export type Context = JestContext; - // ditch this export when moving to esm - for now we need it for to avoid faulty type elision - export type RuntimeType = Runtime; -} - const testTimeoutSymbol = Symbol.for('TEST_TIMEOUT_SYMBOL'); const retryTimesSymbol = Symbol.for('RETRY_TIMES'); @@ -154,7 +150,7 @@ const supportsTopLevelAwait = } })(); -class Runtime { +export default class Runtime { private _cacheFS: StringMap; private _config: Config.ProjectConfig; private _coverageOptions: ShouldInstrumentOptions; @@ -279,7 +275,7 @@ class Runtime { watch?: boolean; watchman: boolean; }, - ): Promise { + ): Promise { createDirectory(config.cacheDirectory); const instance = Runtime.createHasteMap(config, { console: options.console, @@ -1797,5 +1793,3 @@ async function evaluateSyntheticModule(module: SyntheticModule) { return module; } - -export = Runtime;