Skip to content

Commit

Permalink
chore: convert jest-runtime to ESM (#10325)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB authored Nov 4, 2020
1 parent d17f467 commit 3079f29
Show file tree
Hide file tree
Showing 15 changed files with 22 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

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 @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-core/src/__tests__/watchFileChanges.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
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 @@ -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';
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/createContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
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 {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';
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 @@ -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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-runner/src/runTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

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 @@ -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 {
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 {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 = {
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-runtime/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
16 changes: 5 additions & 11 deletions packages/jest-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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');

Expand Down Expand Up @@ -154,7 +150,7 @@ const supportsTopLevelAwait =
}
})();

class Runtime {
export default class Runtime {
private _cacheFS: StringMap;
private _config: Config.ProjectConfig;
private _coverageOptions: ShouldInstrumentOptions;
Expand Down Expand Up @@ -279,7 +275,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 @@ -1797,5 +1793,3 @@ async function evaluateSyntheticModule(module: SyntheticModule) {

return module;
}

export = Runtime;

0 comments on commit 3079f29

Please sign in to comment.