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

feat(jest-runner): improve typings by exposing TestRunner abstract classes #12646

Merged
merged 17 commits into from Apr 8, 2022
63 changes: 63 additions & 0 deletions packages/jest-runner/__typetests__/jest-runner.test.ts
@@ -0,0 +1,63 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import type {UnsubscribeFn} from 'emittery';
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 {
OnTestFailure,
OnTestStart,
OnTestSuccess,
TestRunnerContext,
TestRunnerOptions,
TestWatcher,
} from 'jest-runner';

const globalConfig = {} as Config.GlobalConfig;
const runnerContext = {} as TestRunnerContext;

class CallbackRunner extends CallbackTestRunner {
override async runTests(
tests: Array<Test>,
watcher: TestWatcher,
onStart: OnTestStart,
onResult: OnTestSuccess,
onFailure: OnTestFailure,
options: TestRunnerOptions,
): Promise<void> {
return;
}
}

const callbackRunner = new CallbackRunner(globalConfig, runnerContext);

expectType<boolean | undefined>(callbackRunner.isSerial);
expectType<false>(callbackRunner.supportsEventEmitters);

class EmittingRunner extends EmittingTestRunner {
override on<Name extends keyof TestEvents>(
eventName: string,
listener: (eventData: TestEvents[Name]) => void | Promise<void>,
): UnsubscribeFn {
return () => {};
}

override async runTests(
tests: Array<Test>,
watcher: TestWatcher,
options: TestRunnerOptions,
): Promise<void> {
return;
}
}

const emittingRunner = new EmittingRunner(globalConfig, runnerContext);

expectType<boolean | undefined>(emittingRunner.isSerial);
expectType<true>(emittingRunner.supportsEventEmitters);
11 changes: 11 additions & 0 deletions packages/jest-runner/__typetests__/tsconfig.json
@@ -0,0 +1,11 @@
{
"extends": "../../../tsconfig.json",
"compilerOptions": {
"noUnusedLocals": false,
"noUnusedParameters": false,
"skipLibCheck": true,

"types": []
},
"include": ["./**/*"]
}
3 changes: 2 additions & 1 deletion packages/jest-runner/package.json
Expand Up @@ -39,10 +39,11 @@
"throat": "^6.0.1"
},
"devDependencies": {
"@tsd/typescript": "~4.6.2",
"@types/exit": "^0.1.30",
"@types/graceful-fs": "^4.1.2",
"@types/source-map-support": "^0.5.0",
"jest-jasmine2": "^28.0.0-alpha.8"
mrazauskas marked this conversation as resolved.
Show resolved Hide resolved
"tsd-lite": "^0.5.1"
},
"engines": {
"node": "^12.13.0 || ^14.15.0 || ^16.13.0 || >=17.0.0"
Expand Down
3 changes: 1 addition & 2 deletions packages/jest-runner/src/index.ts
Expand Up @@ -26,16 +26,15 @@ interface WorkerInterface extends Worker {
worker: typeof worker;
}

export {CallbackTestRunner, EmittingTestRunner} from './types';
SimenB marked this conversation as resolved.
Show resolved Hide resolved
export type {
OnTestFailure,
OnTestStart,
OnTestSuccess,
TestWatcher,
TestRunnerContext,
TestRunnerOptions,
EmittingTestRunner,
JestTestRunner,
TestRunner,
} from './types';

export default class TestRunner extends EmittingTestRunner {
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-runner/src/types.ts
Expand Up @@ -74,7 +74,7 @@ abstract class BaseTestRunner {
) {}
}

export abstract class TestRunner extends BaseTestRunner {
export abstract class CallbackTestRunner extends BaseTestRunner {
readonly supportsEventEmitters = false;

abstract runTests(
Expand Down Expand Up @@ -102,4 +102,4 @@ export abstract class EmittingTestRunner extends BaseTestRunner {
): Emittery.UnsubscribeFn;
mrazauskas marked this conversation as resolved.
Show resolved Hide resolved
}

export type JestTestRunner = TestRunner | EmittingTestRunner;
export type JestTestRunner = CallbackTestRunner | EmittingTestRunner;
5 changes: 3 additions & 2 deletions yarn.lock
Expand Up @@ -13309,7 +13309,7 @@ __metadata:
languageName: node
linkType: hard

"jest-jasmine2@^28.0.0-alpha.8, jest-jasmine2@workspace:packages/jest-jasmine2":
"jest-jasmine2@workspace:packages/jest-jasmine2":
version: 0.0.0-use.local
resolution: "jest-jasmine2@workspace:packages/jest-jasmine2"
dependencies:
Expand Down Expand Up @@ -13542,6 +13542,7 @@ __metadata:
"@jest/test-result": ^28.0.0-alpha.8
"@jest/transform": ^28.0.0-alpha.8
"@jest/types": ^28.0.0-alpha.8
"@tsd/typescript": ~4.6.2
"@types/exit": ^0.1.30
"@types/graceful-fs": ^4.1.2
"@types/node": "*"
Expand All @@ -13552,7 +13553,6 @@ __metadata:
jest-docblock: ^28.0.0-alpha.6
jest-environment-node: ^28.0.0-alpha.8
jest-haste-map: ^28.0.0-alpha.8
jest-jasmine2: ^28.0.0-alpha.8
jest-leak-detector: ^28.0.0-alpha.8
jest-message-util: ^28.0.0-alpha.8
jest-resolve: ^28.0.0-alpha.8
Expand All @@ -13561,6 +13561,7 @@ __metadata:
jest-worker: ^28.0.0-alpha.8
source-map-support: ^0.5.6
throat: ^6.0.1
tsd-lite: ^0.5.1
languageName: unknown
linkType: soft

Expand Down