Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: convert jest-runner to ESM #10900

Merged
merged 2 commits into from Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -43,6 +43,7 @@
- `[jest-resolve]` [**BREAKING**] Migrate to ESM ([#10688](https://github.com/facebook/jest/pull/10688))
- `[jest-resolve-dependencies]` [**BREAKING**] Migrate to ESM ([#10876](https://github.com/facebook/jest/pull/10876))
- `[jest-mock]` [**BREAKING**] Migrate to ESM ([#10887](https://github.com/facebook/jest/pull/10887))
- `[jest-runner]` [**BREAKING**] Migrate to ESM ([#10900](https://github.com/facebook/jest/pull/10900))
- `[jest-util]` No longer checking `enumerable` when adding `process.domain` ([#10862](https://github.com/facebook/jest/pull/10862))

### Performance
Expand Down
1 change: 1 addition & 0 deletions e2e/transform/transform-runner/runner.ts
Expand Up @@ -49,6 +49,7 @@ export default class BaseTestRunner {
{
ancestorTitles: [],
duration: 2,
failureDetails: [],
failureMessages: [],
fullName: 'sample test',
location: null,
Expand Down
17 changes: 7 additions & 10 deletions packages/jest-core/src/TestScheduler.ts
Expand Up @@ -28,7 +28,7 @@ import {
import {ScriptTransformer} from '@jest/transform';
import type {Config} from '@jest/types';
import {formatExecError} from 'jest-message-util';
import TestRunner = require('jest-runner');
import TestRunner, {Test} from 'jest-runner';
import type {Context} from 'jest-runtime';
import snapshot = require('jest-snapshot');
import {interopRequireDefault} from 'jest-util';
Expand Down Expand Up @@ -76,7 +76,7 @@ export default class TestScheduler {
}

async scheduleTests(
tests: Array<TestRunner.Test>,
tests: Array<Test>,
watcher: TestWatcher,
): Promise<AggregatedResult> {
const onTestFileStart = this._dispatcher.onTestFileStart.bind(
Expand All @@ -98,7 +98,7 @@ export default class TestScheduler {

const runInBand = shouldRunInBand(tests, timings, this._globalConfig);

const onResult = async (test: TestRunner.Test, testResult: TestResult) => {
const onResult = async (test: Test, testResult: TestResult) => {
if (watcher.isInterrupted()) {
return Promise.resolve();
}
Expand Down Expand Up @@ -138,10 +138,7 @@ export default class TestScheduler {
return this._bailIfNeeded(contexts, aggregatedResults, watcher);
};

const onFailure = async (
test: TestRunner.Test,
error: SerializableError,
) => {
const onFailure = async (test: Test, error: SerializableError) => {
if (watcher.isInterrupted()) {
return;
}
Expand Down Expand Up @@ -242,7 +239,7 @@ export default class TestScheduler {
testRunner.on(
'test-case-result',
([testPath, testCaseResult]) => {
const test: TestRunner.Test = {context, path: testPath};
const test: Test = {context, path: testPath};
this._dispatcher.onTestCaseResult(test, testCaseResult);
},
),
Expand Down Expand Up @@ -297,8 +294,8 @@ export default class TestScheduler {

private _partitionTests(
testRunners: Record<string, TestRunner>,
tests: Array<TestRunner.Test>,
): Record<string, Array<TestRunner.Test>> | null {
tests: Array<Test>,
): Record<string, Array<Test>> | null {
if (Object.keys(testRunners).length > 1) {
return tests.reduce((testRuns, test) => {
const runner = test.context.config.runner;
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-core/src/__tests__/SearchSource.test.ts
Expand Up @@ -9,7 +9,7 @@
import * as path from 'path';
import type {Config} from '@jest/types';
import {normalize} from 'jest-config';
import {Test} from 'jest-runner';
import type {Test} from 'jest-runner';
import Runtime from 'jest-runtime';
import SearchSource, {SearchResult} from '../SearchSource';

Expand Down
89 changes: 42 additions & 47 deletions packages/jest-runner/src/index.ts
Expand Up @@ -16,15 +16,15 @@ import Worker, {PromiseWithCustomMessage} from 'jest-worker';
import runTest from './runTest';
import type {SerializableResolver, worker} from './testWorker';
import type {
OnTestFailure as JestOnTestFailure,
OnTestStart as JestOnTestStart,
OnTestSuccess as JestOnTestSuccess,
Test as JestTest,
TestEvents as JestTestEvents,
TestFileEvent as JestTestFileEvent,
TestRunnerContext as JestTestRunnerContext,
TestRunnerOptions as JestTestRunnerOptions,
TestWatcher as JestTestWatcher,
OnTestFailure,
OnTestStart,
OnTestSuccess,
Test,
TestEvents,
TestFileEvent,
TestRunnerContext,
TestRunnerOptions,
TestWatcher,
WatcherState,
} from './types';

Expand All @@ -34,40 +34,37 @@ interface WorkerInterface extends Worker {
worker: typeof worker;
}

declare namespace TestRunner {
export type Test = JestTest;
export type OnTestFailure = JestOnTestFailure;
export type OnTestStart = JestOnTestStart;
export type OnTestSuccess = JestOnTestSuccess;
export type TestWatcher = JestTestWatcher;
export type TestRunnerContext = JestTestRunnerContext;
export type TestRunnerOptions = JestTestRunnerOptions;
export type TestFileEvent = JestTestFileEvent;
}
export type {
Test,
OnTestFailure,
OnTestStart,
OnTestSuccess,
TestWatcher,
TestRunnerContext,
TestRunnerOptions,
TestFileEvent,
} from './types';

class TestRunner {
export default class TestRunner {
private readonly _globalConfig: Config.GlobalConfig;
private readonly _context: JestTestRunnerContext;
private readonly eventEmitter = new Emittery.Typed<JestTestEvents>();
private readonly _context: TestRunnerContext;
private readonly eventEmitter = new Emittery.Typed<TestEvents>();
readonly __PRIVATE_UNSTABLE_API_supportsEventEmitters__: boolean = true;

readonly isSerial?: boolean;

constructor(
globalConfig: Config.GlobalConfig,
context?: JestTestRunnerContext,
) {
constructor(globalConfig: Config.GlobalConfig, context?: TestRunnerContext) {
this._globalConfig = globalConfig;
this._context = context || {};
}

async runTests(
tests: Array<JestTest>,
watcher: JestTestWatcher,
onStart: JestOnTestStart | undefined,
onResult: JestOnTestSuccess | undefined,
onFailure: JestOnTestFailure | undefined,
options: JestTestRunnerOptions,
tests: Array<Test>,
watcher: TestWatcher,
onStart: OnTestStart | undefined,
onResult: OnTestSuccess | undefined,
onFailure: OnTestFailure | undefined,
options: TestRunnerOptions,
): Promise<void> {
return await (options.serial
? this._createInBandTestRun(tests, watcher, onStart, onResult, onFailure)
Expand All @@ -81,11 +78,11 @@ class TestRunner {
}

private async _createInBandTestRun(
tests: Array<JestTest>,
watcher: JestTestWatcher,
onStart?: JestOnTestStart,
onResult?: JestOnTestSuccess,
onFailure?: JestOnTestFailure,
tests: Array<Test>,
watcher: TestWatcher,
onStart?: OnTestStart,
onResult?: OnTestSuccess,
onFailure?: OnTestFailure,
) {
process.env.JEST_WORKER_ID = '1';
const mutex = throat(1);
Expand All @@ -97,7 +94,7 @@ class TestRunner {
if (watcher.isInterrupted()) {
throw new CancelRun();
}
let sendMessageToJest: JestTestFileEvent;
let sendMessageToJest: TestFileEvent;

// Remove `if(onStart)` in Jest 27
if (onStart) {
Expand Down Expand Up @@ -152,11 +149,11 @@ class TestRunner {
}

private async _createParallelTestRun(
tests: Array<JestTest>,
watcher: JestTestWatcher,
onStart?: JestOnTestStart,
onResult?: JestOnTestSuccess,
onFailure?: JestOnTestFailure,
tests: Array<Test>,
watcher: TestWatcher,
onStart?: OnTestStart,
onResult?: OnTestSuccess,
onFailure?: OnTestFailure,
) {
const resolvers: Map<string, SerializableResolver> = new Map();
for (const test of tests) {
Expand Down Expand Up @@ -187,7 +184,7 @@ class TestRunner {

// Send test suites to workers continuously instead of all at once to track
// the start time of individual tests.
const runTestInWorker = (test: JestTest) =>
const runTestInWorker = (test: Test) =>
mutex(async () => {
if (watcher.isInterrupted()) {
return Promise.reject();
Expand Down Expand Up @@ -225,7 +222,7 @@ class TestRunner {
return promise;
});

const onError = async (err: SerializableError, test: JestTest) => {
const onError = async (err: SerializableError, test: Test) => {
// Remove `if(onFailure)` in Jest 27
if (onFailure) {
await onFailure(test, err);
Expand Down Expand Up @@ -290,5 +287,3 @@ class CancelRun extends Error {
this.name = 'CancelRun';
}
}

export = TestRunner;