Skip to content

Commit

Permalink
chore: convert jest-runtime to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Aug 23, 2020
1 parent 7e5e27a commit 3d4dae1
Show file tree
Hide file tree
Showing 17 changed files with 36 additions and 42 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

### Chore & Maintenance

- `[jest-runtime]` [**BREAKING**] Convert to ESM ([#10325](https://github.com/facebook/jest/pull/10325))

### Performance

## 26.4.2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type {Config} from '@jest/types';
import type {JestEnvironment} from '@jest/environment';
import type {TestResult} from '@jest/test-result';
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';

Expand Down
2 changes: 1 addition & 1 deletion packages/jest-core/src/__tests__/SearchSource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import * as path from 'path';
import Runtime = require('jest-runtime');
import Runtime from 'jest-runtime';
import {normalize} from 'jest-config';
import {Test} from 'jest-runner';
import type {Config} from '@jest/types';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import * as path from 'path';
import {tmpdir} from 'os';
import * as fs from 'graceful-fs';
import {JestHook} from 'jest-watcher';
import Runtime = require('jest-runtime');
import Runtime from 'jest-runtime';
import {normalize} from 'jest-config';
import HasteMap = require('jest-haste-map');
import rimraf = require('rimraf');
Expand Down
6 changes: 3 additions & 3 deletions packages/jest-core/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type {AggregatedResult} from '@jest/test-result';
import {CustomConsole} from '@jest/console';
import {createDirectory, preRunMessage} from 'jest-util';
import {readConfigs} from 'jest-config';
import Runtime = require('jest-runtime');
import Runtime, {Context} from 'jest-runtime';
import type {ChangedFilesPromise} from 'jest-changed-files';
import HasteMap = require('jest-haste-map');
import chalk = require('chalk');
Expand Down Expand Up @@ -218,7 +218,7 @@ const _run10000 = async (
};

const runWatch = async (
contexts: Array<Runtime.Context>,
contexts: Array<Context>,
_configs: Array<Config.ProjectConfig>,
hasDeprecationWarnings: boolean,
globalConfig: Config.GlobalConfig,
Expand Down Expand Up @@ -256,7 +256,7 @@ const runWatch = async (

const runWithoutWatch = async (
globalConfig: Config.GlobalConfig,
contexts: Array<Runtime.Context>,
contexts: Array<Context>,
outputStream: NodeJS.WriteStream,
onComplete: OnCompleteCallback,
changedFilesPromise?: ChangedFilesPromise,
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-core/src/lib/create_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
*/

import type {Config} from '@jest/types';
import Runtime = require('jest-runtime');
import Runtime, {Context} from 'jest-runtime';
import type {HasteMapObject} from 'jest-haste-map';

export default (
config: Config.ProjectConfig,
{hasteFS, moduleMap}: HasteMapObject,
): Runtime.Context => ({
): Context => ({
config,
hasteFS,
moduleMap,
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-jasmine2/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type {Config, Global} from '@jest/types';
import type {AssertionResult, TestResult} from '@jest/test-result';
import type {JestEnvironment} from '@jest/environment';
import type {SnapshotStateType} from 'jest-snapshot';
import type {RuntimeType as Runtime} from 'jest-runtime';
import type Runtime from 'jest-runtime';

import {getCallsite} from '@jest/source-map';
import installEach from './each';
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-repl/src/cli/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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=<pathToConfigFile>]';

Expand Down
2 changes: 1 addition & 1 deletion packages/jest-repl/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
*/

import Runtime = require('jest-runtime');
import Runtime from 'jest-runtime';
import yargs = require('yargs');
import {validateCLIOptions} from 'jest-validate';
import {deprecationEntries} from 'jest-config';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,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'],
Expand Down
17 changes: 9 additions & 8 deletions packages/jest-runner/src/runTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import {
getConsoleOutput,
} from '@jest/console';
import type {JestEnvironment} from '@jest/environment';
import RuntimeClass = require('jest-runtime');
import type RuntimeClass from 'jest-runtime';
import * as fs from 'graceful-fs';
import {ErrorWithStack, interopRequireDefault, setGlobal} from 'jest-util';
import {ErrorWithStack, setGlobal} from 'jest-util';
import LeakDetector from 'jest-leak-detector';
import type {ResolverType} from 'jest-resolve';
import {getTestEnvironment} from 'jest-config';
Expand Down Expand Up @@ -103,16 +103,17 @@ async function runTestInternal(
});
}

const TestEnvironment: typeof JestEnvironment = interopRequireDefault(
require(testEnvironment),
const TestEnvironment: typeof JestEnvironment = (
await import(testEnvironment)
).default;
const testFramework: TestFramework =
process.env.JEST_CIRCUS === '1'
? require('jest-circus/runner') // eslint-disable-line import/no-extraneous-dependencies
: require(config.testRunner);
? // @ts-expect-error: JS file
(await import('jest-circus/runner')).default
: (await import(config.testRunner)).default;
const Runtime: typeof RuntimeClass = config.moduleLoader
? require(config.moduleLoader)
: require('jest-runtime');
? (await import(config.moduleLoader)).default
: (await import('jest-runtime')).default;

const consoleOut = globalConfig.useStderr ? process.stderr : process.stdout;
const consoleFormatter = (type: LogType, message: LogMessage) =>
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-runner/src/testWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type {SerializableError, TestResult} from '@jest/test-result';
import HasteMap = require('jest-haste-map');
import exit = require('exit');
import {separateMessageFromStack} from 'jest-message-util';
import Runtime = require('jest-runtime');
import Runtime from 'jest-runtime';
import type {ResolverType} from 'jest-resolve';
import {messageParent} from 'jest-worker';
import type {
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-runner/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type {
import type {JestEnvironment} from '@jest/environment';
import type {FS as HasteFS, ModuleMap} from 'jest-haste-map';
import type {ResolverType} from 'jest-resolve';
import type {RuntimeType} from 'jest-runtime';
import type RuntimeType from 'jest-runtime';

export type ErrorWithCode = Error & {code?: string};
export type Test = {
Expand Down
6 changes: 3 additions & 3 deletions packages/jest-runtime/src/__mocks__/createRuntime.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import path from 'path';

module.exports = async function createRuntime(filename, config) {
const NodeEnvironment = require('jest-environment-node');
const Runtime = require('../');
const {default: NodeEnvironment} = await import('jest-environment-node');
const {default: Runtime} = await import('../');

const {normalize} = require('jest-config');
const {normalize} = await import('jest-config');

config = normalize(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import HasteMap from 'jest-haste-map';
const Runtime = require('../');
import Runtime from '../';

jest.mock('jest-haste-map');

Expand Down
6 changes: 2 additions & 4 deletions packages/jest-runtime/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {setGlobal, tryRealpath} from 'jest-util';
import {validateCLIOptions} from 'jest-validate';
import {deprecationEntries, readConfig} from 'jest-config';
import {VERSION} from '../version';
import type {Context} from '../types';
import * as args from './args';

export async function run(
Expand Down Expand Up @@ -67,11 +66,10 @@ export async function run(
automock: false,
};

// Break circular dependency
const Runtime: any = require('..');
const {default: Runtime} = await import('..');

try {
const hasteMap: Context = await Runtime.createContext(config, {
const hasteMap = await Runtime.createContext(config, {
maxWorkers: Math.max(cpus().length - 1, 1),
watchman: globalConfig.watchman,
});
Expand Down
17 changes: 5 additions & 12 deletions packages/jest-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import HasteMap = require('jest-haste-map');
import Resolver = require('jest-resolve');
import Snapshot = require('jest-snapshot');
import stripBOM = require('strip-bom');
import type {Context as JestContext} from './types';
import type {Context} from './types';
import {
createOutsideJestVmPath,
decodePossibleOutsideJestVmPath,
Expand All @@ -57,6 +57,8 @@ import {
import {options as cliOptions} from './cli/args';
import {run as cliRun} from './cli';

export type {Context} from './types';

interface JestGlobalsValues extends Global.TestFrameworkGlobals {
jest: typeof JestGlobals.jest;
expect: typeof JestGlobals.expect;
Expand Down Expand Up @@ -105,12 +107,6 @@ const fromEntries: typeof Object.fromEntries =
}, {});
};

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');

Expand All @@ -137,8 +133,7 @@ type RunScriptEvalResult = {[EVAL_RESULT_VARIABLE]: ModuleWrapper};

const runtimeSupportsVmModules = typeof SyntheticModule === 'function';

/* eslint-disable-next-line no-redeclare */
class Runtime {
export default class Runtime {
private _cacheFS: StringMap;
private _config: Config.ProjectConfig;
private _coverageOptions: ShouldInstrumentOptions;
Expand Down Expand Up @@ -256,7 +251,7 @@ class Runtime {
watch?: boolean;
watchman: boolean;
},
): Promise<JestContext> {
): Promise<Context> {
createDirectory(config.cacheDirectory);
const instance = Runtime.createHasteMap(config, {
console: options.console,
Expand Down Expand Up @@ -1718,5 +1713,3 @@ function invariant(condition: unknown, message?: string): asserts condition {
throw new Error(message);
}
}

export = Runtime;

0 comments on commit 3d4dae1

Please sign in to comment.