Skip to content

Commit

Permalink
feat: add message of total tests to run
Browse files Browse the repository at this point in the history
  • Loading branch information
WillianAgostini committed Jan 18, 2024
1 parent f6a5d5d commit 3ce02b0
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 5 deletions.
Expand Up @@ -104,6 +104,7 @@ const jestAdapter = async (
const results = await runAndTransformResultsToJestFormat({
config,
globalConfig,
sendMessageToJest,
setupAfterEnvPerfStats,
testPath,
});
Expand Down
Expand Up @@ -30,6 +30,7 @@ import {
addEventHandler,
dispatch,
getState as getRunnerState,
getState,
} from '../state';
import testCaseReportHandler from '../testCaseReportHandler';
import {unhandledRejectionHandler} from '../unhandledRejectionHandler';
Expand Down Expand Up @@ -143,17 +144,44 @@ export const initialize = async ({
return {globals: globalsObject, snapshotState};
};

const countNumberOfTests = (describeBlock: Circus.DescribeBlock) => {
let numberOfTests = 0;

for (const child of describeBlock.children) {
switch (child.type) {
case 'describeBlock':
numberOfTests += countNumberOfTests(child);
break;

case 'test':
numberOfTests++;
break;
}
}

return numberOfTests;
};

export const runAndTransformResultsToJestFormat = async ({
config,
globalConfig,
sendMessageToJest,
setupAfterEnvPerfStats,
testPath,
}: {
config: Config.ProjectConfig;
globalConfig: Config.GlobalConfig;
sendMessageToJest?: TestFileEvent;
testPath: string;
setupAfterEnvPerfStats: Config.SetupAfterEnvPerfStats;
}): Promise<TestResult> => {
const {rootDescribeBlock} = getState();
if (sendMessageToJest)
sendMessageToJest('test-file-num-of-tests', [
testPath,
countNumberOfTests(rootDescribeBlock),
]);

const runResult: Circus.RunResult = await run();

let numFailingTests = 0;
Expand Down
11 changes: 11 additions & 0 deletions packages/jest-core/src/ReporterDispatcher.ts
Expand Up @@ -92,6 +92,17 @@ export default class ReporterDispatcher {
}
}

async onNumberOfTests(
testPath: string,
numberOfTests: number,
): Promise<void> {
for (const reporter of this._reporters) {
if (reporter.onNumberOfTests) {
await reporter.onNumberOfTests(testPath, numberOfTests);
}
}
}

async onRunComplete(
testContexts: Set<TestContext>,
results: AggregatedResult,
Expand Down
6 changes: 6 additions & 0 deletions packages/jest-core/src/TestScheduler.ts
Expand Up @@ -272,6 +272,12 @@ class TestScheduler {
this._dispatcher.onTestCaseResult(test, testCaseResult);
},
),
testRunner.on(
'test-file-num-of-tests',
([testPath, numberOfTests]) => {
this._dispatcher.onNumberOfTests(testPath, numberOfTests);
},
),
];

await testRunner.runTests(tests, watcher, testRunnerOptions);
Expand Down
10 changes: 8 additions & 2 deletions packages/jest-jasmine2/src/index.ts
Expand Up @@ -8,7 +8,7 @@
import * as path from 'path';
import type {JestEnvironment} from '@jest/environment';
import {getCallsite} from '@jest/source-map';
import type {TestResult} from '@jest/test-result';
import type {TestFileEvent, TestResult} from '@jest/test-result';
import type {Config, Global} from '@jest/types';
import type Runtime from 'jest-runtime';
import type {SnapshotState} from 'jest-snapshot';
Expand All @@ -31,8 +31,14 @@ export default async function jasmine2(
environment: JestEnvironment,
runtime: Runtime,
testPath: string,
sendMessageToJest?: TestFileEvent,
): Promise<TestResult> {
const reporter = new JasmineReporter(globalConfig, config, testPath);
const reporter = new JasmineReporter(
globalConfig,
config,
testPath,
sendMessageToJest,
);
const jasmineFactory =
runtime.requireInternalModule<typeof import('./jasmine/jasmineLight')>(
JASMINE,
Expand Down
13 changes: 11 additions & 2 deletions packages/jest-jasmine2/src/reporter.ts
Expand Up @@ -7,6 +7,7 @@

import {
type AssertionResult,
type TestFileEvent,
type TestResult,
createEmptyTestResult,
} from '@jest/test-result';
Expand All @@ -27,11 +28,13 @@ export default class Jasmine2Reporter implements Reporter {
private readonly _resultsPromise: Promise<TestResult>;
private readonly _startTimes: Map<string, Microseconds>;
private readonly _testPath: string;
private readonly _sendMessageToJest?: TestFileEvent;

constructor(
globalConfig: Config.GlobalConfig,
config: Config.ProjectConfig,
testPath: string,
sendMessageToJest?: TestFileEvent,
) {
this._globalConfig = globalConfig;
this._config = config;
Expand All @@ -41,10 +44,16 @@ export default class Jasmine2Reporter implements Reporter {
this._resolve = null;
this._resultsPromise = new Promise(resolve => (this._resolve = resolve));
this._startTimes = new Map();
this._sendMessageToJest = sendMessageToJest;
}

// eslint-disable-next-line @typescript-eslint/no-empty-function
jasmineStarted(_runDetails: RunDetails): void {}
jasmineStarted(_runDetails: RunDetails): void {
if (this._sendMessageToJest && _runDetails.totalSpecsDefined !== undefined)
this._sendMessageToJest('test-file-num-of-tests', [
this._testPath,
_runDetails.totalSpecsDefined,
]);
}

specStarted(spec: SpecResult): void {
this._startTimes.set(spec.id, Date.now());
Expand Down
2 changes: 2 additions & 0 deletions packages/jest-reporters/src/BaseReporter.ts
Expand Up @@ -34,6 +34,8 @@ export default class BaseReporter implements Reporter {
/* eslint-disable @typescript-eslint/no-empty-function */
onTestCaseResult(_test: Test, _testCaseResult: TestCaseResult): void {}

onNumberOfTests(_testPath: string, _numberOfTests: number): void {}

onTestResult(
_test?: Test,
_testResult?: TestResult,
Expand Down
4 changes: 4 additions & 0 deletions packages/jest-reporters/src/DefaultReporter.ts
Expand Up @@ -147,6 +147,10 @@ export default class DefaultReporter extends BaseReporter {
this._status.addTestCaseResult(test, testCaseResult);
}

override onNumberOfTests(testPath: string, numberOfTests: number): void {
this._status.addNumberOfTests(testPath, numberOfTests);
}

override onRunComplete(): void {
this.forceFlushBufferedOutput();
this._status.runFinished();
Expand Down
14 changes: 13 additions & 1 deletion packages/jest-reporters/src/Status.ts
Expand Up @@ -91,6 +91,7 @@ export default class Status {
private _interval?: NodeJS.Timeout;
private _aggregatedResults?: AggregatedResult;
private _showStatus: boolean;
private _numberOfTests: Map<string, number>;

constructor(private readonly _globalConfig: Config.GlobalConfig) {
this._cache = null;
Expand All @@ -100,6 +101,7 @@ export default class Status {
this._emitScheduled = false;
this._estimatedTime = 0;
this._showStatus = false;
this._numberOfTests = new Map<string, number>();
}

onChange(callback: () => void): void {
Expand Down Expand Up @@ -134,6 +136,14 @@ export default class Status {
this._currentTests.addPassedTest(test.path);
}

addNumberOfTests(testPath: string, numberOfTests: number): void {
this._numberOfTests.set(testPath, numberOfTests);
}

getNumberOfTests(testPath: string): number | undefined {
return this._numberOfTests.get(testPath);
}

testStarted(testPath: string, config: Config.ProjectConfig): void {
this._currentTests.add(testPath, config);
if (this._showStatus) {
Expand Down Expand Up @@ -180,8 +190,10 @@ export default class Status {
: '';
const prefix = RUNNING + projectDisplayName;

const numberOfTests = this.getNumberOfTests(testPath);
const txtNumberOfTests = numberOfTests ? `of ${numberOfTests}` : '';
const currentTestPassed = `${chalk.bold.green(
`${passedTests} passed`,
`${passedTests} passed ${txtNumberOfTests}`,
)}`;
content += `${wrapAnsiString(
prefix +
Expand Down
4 changes: 4 additions & 0 deletions packages/jest-reporters/src/types.ts
Expand Up @@ -42,6 +42,10 @@ export interface Reporter {
test: Test,
testCaseResult: TestCaseResult,
) => Promise<void> | void;
readonly onNumberOfTests?: (
testPath: string,
numberOfTests: number,
) => Promise<void> | void;
readonly onRunStart?: (
results: AggregatedResult,
options: ReporterOnStartOptions,
Expand Down
1 change: 1 addition & 0 deletions packages/jest-test-result/src/types.ts
Expand Up @@ -205,6 +205,7 @@ export type TestContext = {

// Typings for `sendMessageToJest` events
export type TestEvents = {
'test-file-num-of-tests': [string, number];
'test-file-start': [Test];
'test-file-success': [Test, TestResult];
'test-file-failure': [Test, SerializableError];
Expand Down

0 comments on commit 3ce02b0

Please sign in to comment.