diff --git a/CHANGELOG.md b/CHANGELOG.md index 913b7572a4d3..d5b4d2c280c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,7 +49,7 @@ - `[jest-resolve]` Expose `JestResolver`, `AsyncResolver`, `SyncResolver`, `PackageFilter`, `PathFilter` and `PackageJSON` types ([#12707](https://github.com/facebook/jest/pull/12707), ([#12712](https://github.com/facebook/jest/pull/12712)) - `[jest-runner]` Allow `setupFiles` module to export an async function ([#12042](https://github.com/facebook/jest/pull/12042)) - `[jest-runner]` Allow passing `testEnvironmentOptions` via docblocks ([#12470](https://github.com/facebook/jest/pull/12470)) -- `[jest-runner]` Exposing `CallbackTestRunner`, `EmittingTestRunner` abstract classes to help typing third party runners ([#12646](https://github.com/facebook/jest/pull/12646)) +- `[jest-runner]` Expose `CallbackTestRunner`, `EmittingTestRunner` abstract classes and `CallbackTestRunnerInterface`, `EmittingTestRunnerInterface` to help typing third party runners ([#12646](https://github.com/facebook/jest/pull/12646), [#12715](https://github.com/facebook/jest/pull/12715)) - `[jest-runtime]` [**BREAKING**] `Runtime.createHasteMap` now returns a promise ([#12008](https://github.com/facebook/jest/pull/12008)) - `[jest-runtime]` Calling `jest.resetModules` function will clear FS and transform cache ([#12531](https://github.com/facebook/jest/pull/12531)) - `[jest-runtime]` [**BREAKING**] Remove `Context` type export, it must be imported from `@jest/test-result` ([#12685](https://github.com/facebook/jest/pull/12685)) diff --git a/packages/jest-runner/__typetests__/jest-runner.test.ts b/packages/jest-runner/__typetests__/jest-runner.test.ts index 04c00ab32d06..97ad722655ab 100644 --- a/packages/jest-runner/__typetests__/jest-runner.test.ts +++ b/packages/jest-runner/__typetests__/jest-runner.test.ts @@ -6,18 +6,21 @@ */ import {expectType} from 'tsd-lite'; -import type {Test, TestEvents} from '@jest/test-result'; -import type {Config} from '@jest/types'; import {CallbackTestRunner, EmittingTestRunner} from 'jest-runner'; import type { + CallbackTestRunnerInterface, + Config, + EmittingTestRunnerInterface, OnTestFailure, OnTestStart, OnTestSuccess, + Test, + TestEvents, TestRunnerContext, TestRunnerOptions, + TestWatcher, UnsubscribeFn, } from 'jest-runner'; -import type {TestWatcher} from 'jest-watcher'; const globalConfig = {} as Config.GlobalConfig; const runnerContext = {} as TestRunnerContext; @@ -45,6 +48,32 @@ const callbackRunner = new CallbackRunner(globalConfig, runnerContext); expectType(callbackRunner.isSerial); expectType(callbackRunner.supportsEventEmitters); +// CallbackTestRunnerInterface + +class CustomCallbackRunner implements CallbackTestRunnerInterface { + readonly #maxConcurrency: number; + readonly #globalConfig: Config.GlobalConfig; + + constructor(globalConfig: Config.GlobalConfig) { + this.#globalConfig = globalConfig; + this.#maxConcurrency = globalConfig.maxWorkers; + } + + async runTests( + tests: Array, + watcher: TestWatcher, + onStart: OnTestStart, + onResult: OnTestSuccess, + onFailure: OnTestFailure, + options: TestRunnerOptions, + ): Promise { + expectType(this.#globalConfig); + expectType(this.#maxConcurrency); + + return; + } +} + // EmittingRunner class EmittingRunner extends EmittingTestRunner { @@ -71,3 +100,34 @@ const emittingRunner = new EmittingRunner(globalConfig, runnerContext); expectType(emittingRunner.isSerial); expectType(emittingRunner.supportsEventEmitters); + +// EmittingTestRunnerInterface + +class CustomEmittingRunner implements EmittingTestRunnerInterface { + readonly #maxConcurrency: number; + readonly #globalConfig: Config.GlobalConfig; + readonly supportsEventEmitters = true; + + constructor(globalConfig: Config.GlobalConfig) { + this.#globalConfig = globalConfig; + this.#maxConcurrency = globalConfig.maxWorkers; + } + + async runTests( + tests: Array, + watcher: TestWatcher, + options: TestRunnerOptions, + ): Promise { + expectType(this.#globalConfig); + expectType(this.#maxConcurrency); + + return; + } + + on( + eventName: string, + listener: (eventData: TestEvents[Name]) => void | Promise, + ): UnsubscribeFn { + return () => {}; + } +} diff --git a/packages/jest-runner/src/index.ts b/packages/jest-runner/src/index.ts index 627b446798f2..4f1bb6bf074e 100644 --- a/packages/jest-runner/src/index.ts +++ b/packages/jest-runner/src/index.ts @@ -27,8 +27,13 @@ interface WorkerInterface extends Worker { worker: typeof worker; } +export type {Test, TestEvents} from '@jest/test-result'; +export type {Config} from '@jest/types'; +export type {TestWatcher} from 'jest-watcher'; export {CallbackTestRunner, EmittingTestRunner} from './types'; export type { + CallbackTestRunnerInterface, + EmittingTestRunnerInterface, OnTestFailure, OnTestStart, OnTestSuccess, diff --git a/packages/jest-runner/src/types.ts b/packages/jest-runner/src/types.ts index 4a80e06e8a1b..18a0ffe4e139 100644 --- a/packages/jest-runner/src/types.ts +++ b/packages/jest-runner/src/types.ts @@ -57,6 +57,36 @@ export type TestRunnerSerializedContext = { export type UnsubscribeFn = () => void; +export interface CallbackTestRunnerInterface { + readonly isSerial?: boolean; + readonly supportsEventEmitters?: boolean; + + runTests( + tests: Array, + watcher: TestWatcher, + onStart: OnTestStart, + onResult: OnTestSuccess, + onFailure: OnTestFailure, + options: TestRunnerOptions, + ): Promise; +} + +export interface EmittingTestRunnerInterface { + readonly isSerial?: boolean; + readonly supportsEventEmitters: true; + + runTests( + tests: Array, + watcher: TestWatcher, + options: TestRunnerOptions, + ): Promise; + + on( + eventName: Name, + listener: (eventData: TestEvents[Name]) => void | Promise, + ): UnsubscribeFn; +} + abstract class BaseTestRunner { readonly isSerial?: boolean; abstract readonly supportsEventEmitters: boolean; @@ -67,7 +97,10 @@ abstract class BaseTestRunner { ) {} } -export abstract class CallbackTestRunner extends BaseTestRunner { +export abstract class CallbackTestRunner + extends BaseTestRunner + implements CallbackTestRunnerInterface +{ readonly supportsEventEmitters = false; abstract runTests( @@ -80,7 +113,10 @@ export abstract class CallbackTestRunner extends BaseTestRunner { ): Promise; } -export abstract class EmittingTestRunner extends BaseTestRunner { +export abstract class EmittingTestRunner + extends BaseTestRunner + implements EmittingTestRunnerInterface +{ readonly supportsEventEmitters = true; abstract runTests(