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

[WIP][Proposal] Report progress of individual test cases #9001

Closed
wants to merge 13 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const jestAdapter = async (
environment: JestEnvironment,
runtime: Runtime,
testPath: string,
sendMessageToJest?: Function,
): Promise<TestResult> => {
const {
initialize,
Expand All @@ -44,6 +45,7 @@ const jestAdapter = async (
globalConfig,
localRequire: runtime.requireModule.bind(runtime),
parentProcess: process,
sendMessageToJest,
testPath,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
} from '../state';
import {getTestID} from '../utils';
import run from '../run';
import testCaseReportHandler from '../testCaseReportHandler';
import globals from '..';

type Process = NodeJS.Process;
Expand All @@ -43,6 +44,7 @@ export const initialize = ({
localRequire,
parentProcess,
testPath,
sendMessageToJest,
}: {
config: Config.ProjectConfig;
environment: JestEnvironment;
Expand All @@ -52,6 +54,7 @@ export const initialize = ({
localRequire: (path: Config.Path) => any;
testPath: Config.Path;
parentProcess: Process;
sendMessageToJest?: Function;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type it properly?

}) => {
if (globalConfig.testTimeout) {
getRunnerState().testTimeout = globalConfig.testTimeout;
Expand Down Expand Up @@ -138,6 +141,9 @@ export const initialize = ({
setState({snapshotState, testPath});

addEventHandler(handleSnapshotStateAfterRetry(snapshotState));
if (sendMessageToJest) {
addEventHandler(testCaseReportHandler(testPath, sendMessageToJest));
}

// Return it back to the outer scope (test runner outside the VM).
return {globals, snapshotState};
Expand Down
36 changes: 36 additions & 0 deletions packages/jest-circus/src/testCaseReportHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* 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 {Circus} from '@jest/types';
import {TestCase, TestCaseResult} from '@jest/test-result';
import {makeSingleTestResult, parseSingleTestResult} from './utils';

const testCaseReportHandler = (
testPath: string,
sendMessageToJest: Function,
) => (event: Circus.Event) => {
switch (event.name) {
case 'test_done': {
const testResult = makeSingleTestResult(event.test);
const testCaseResult: TestCaseResult = parseSingleTestResult(testResult);
const testCase: TestCase = {
ancestorTitles: testCaseResult.ancestorTitles,
fullName: testCaseResult.fullName,
location: testCaseResult.location,
title: testCaseResult.title,
};
sendMessageToJest('test-case-result', [
testPath,
testCase,
testCaseResult,
]);
break;
}
}
};

export default testCaseReportHandler;
148 changes: 110 additions & 38 deletions packages/jest-circus/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import isGeneratorFn from 'is-generator-fn';
import co from 'co';
import StackUtils = require('stack-utils');
import prettyFormat = require('pretty-format');
import {getState} from './state';
import {AssertionResult, Status} from '@jest/test-result';
import {ROOT_DESCRIBE_BLOCK_NAME, getState} from './state';

const stackUtils = new StackUtils({cwd: 'A path that does not exist'});

Expand Down Expand Up @@ -243,50 +244,56 @@ export const makeRunResult = (
unhandledErrors: unhandledErrors.map(_formatError),
});

const makeTestResults = (
describeBlock: Circus.DescribeBlock,
): Circus.TestResults => {
export const makeSingleTestResult = (
test: Circus.TestEntry,
): Circus.TestResult => {
const {includeTestLocationInResult} = getState();
let testResults: Circus.TestResults = [];
for (const test of describeBlock.tests) {
const testPath = [];
let parent: Circus.TestEntry | Circus.DescribeBlock | undefined = test;
do {
testPath.unshift(parent.name);
} while ((parent = parent.parent));

const {status} = test;
const testPath = [];
let parent: Circus.TestEntry | Circus.DescribeBlock | undefined = test;
do {
testPath.unshift(parent.name);
} while ((parent = parent.parent));

if (!status) {
throw new Error('Status should be present after tests are run.');
}
const {status} = test;

let location = null;
if (includeTestLocationInResult) {
const stackLine = test.asyncError.stack.split('\n')[1];
const parsedLine = stackUtils.parseLine(stackLine);
if (
parsedLine &&
typeof parsedLine.column === 'number' &&
typeof parsedLine.line === 'number'
) {
location = {
column: parsedLine.column,
line: parsedLine.line,
};
}
}
if (!status) {
throw new Error('Status should be present after tests are run.');
}

testResults.push({
duration: test.duration,
errors: test.errors.map(_formatError),
invocations: test.invocations,
location,
status,
testPath,
});
let location = null;
if (includeTestLocationInResult) {
const stackLine = test.asyncError.stack.split('\n')[1];
const parsedLine = stackUtils.parseLine(stackLine);
if (
parsedLine &&
typeof parsedLine.column === 'number' &&
typeof parsedLine.line === 'number'
) {
location = {
column: parsedLine.column,
line: parsedLine.line,
};
}
}

return {
duration: test.duration,
errors: test.errors.map(_formatError),
invocations: test.invocations,
location,
status,
testPath,
};
};

const makeTestResults = (
describeBlock: Circus.DescribeBlock,
): Circus.TestResults => {
let testResults: Circus.TestResults = describeBlock.tests.map(
makeSingleTestResult,
);

for (const child of describeBlock.children) {
testResults = testResults.concat(makeTestResults(child));
}
Expand Down Expand Up @@ -354,3 +361,68 @@ export const invariant = (condition: unknown, message?: string) => {
throw new Error(message);
}
};

export const parseSingleTestResult = (
testResult: Circus.TestResult,
): AssertionResult => {
let status: Status;
if (testResult.status === 'skip') {
status = 'pending';
} else if (testResult.status === 'todo') {
status = 'todo';
} else if (testResult.errors.length) {
status = 'failed';
} else {
status = 'passed';
}

const ancestorTitles = testResult.testPath.filter(
name => name !== ROOT_DESCRIBE_BLOCK_NAME,
);
const title = ancestorTitles.pop();

return {
ancestorTitles,
duration: testResult.duration,
failureMessages: testResult.errors,
fullName: title
? ancestorTitles.concat(title).join(' ')
: ancestorTitles.join(' '),
invocations: testResult.invocations,
location: testResult.location,
numPassingAsserts: 0,
status,
title: testResult.testPath[testResult.testPath.length - 1],
};
};

export const parseTestResults = (testResults: Array<Circus.TestResult>) => {
let numFailingTests = 0;
let numPassingTests = 0;
let numPendingTests = 0;
let numTodoTests = 0;

const assertionResults: Array<AssertionResult> = testResults.map(
testResult => {
if (testResult.status === 'skip') {
numPendingTests += 1;
} else if (testResult.status === 'todo') {
numTodoTests += 1;
} else if (testResult.errors.length) {
numFailingTests += 1;
} else {
numPassingTests += 1;
}

return parseSingleTestResult(testResult);
},
);

return {
assertionResults,
numFailingTests,
numPassingTests,
numPendingTests,
numTodoTests,
};
};
35 changes: 29 additions & 6 deletions packages/jest-core/src/ReporterDispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
* LICENSE file in the root directory of this source tree.
*/

import {AggregatedResult, TestResult} from '@jest/test-result';
import {
AggregatedResult,
TestCase,
TestCaseResult,
TestResult,
} from '@jest/test-result';
import {Test} from 'jest-runner';
import {Context} from 'jest-runtime';
import {Reporter, ReporterOnStartOptions} from '@jest/reporters';
Expand All @@ -27,14 +32,17 @@ export default class ReporterDispatcher {
);
}

async onTestResult(
async onTestFileResult(
test: Test,
testResult: TestResult,
results: AggregatedResult,
) {
for (const reporter of this._reporters) {
reporter.onTestResult &&
(await reporter.onTestResult(test, testResult, results));
if (reporter.onTestFileResult) {
await reporter.onTestFileResult(test, testResult, results);
} else if (reporter.onTestResult) {
await reporter.onTestResult(test, testResult, results);
}
}

// Release memory if unused later.
Expand All @@ -43,9 +51,13 @@ export default class ReporterDispatcher {
testResult.console = undefined;
}

async onTestStart(test: Test) {
async onTestFileStart(test: Test) {
for (const reporter of this._reporters) {
reporter.onTestStart && (await reporter.onTestStart(test));
if (reporter.onTestFileStart) {
await reporter.onTestFileStart(test);
} else if (reporter.onTestStart) {
await reporter.onTestStart(test);
}
}
}

Expand All @@ -55,6 +67,17 @@ export default class ReporterDispatcher {
}
}

async onTestCaseResult(
test: Test,
testCase: TestCase,
testCaseResult: TestCaseResult,
) {
for (const reporter of this._reporters) {
reporter.onTestCaseResult &&
(await reporter.onTestCaseResult(test, testCase, testCaseResult));
}
}

async onRunComplete(contexts: Set<Context>, results: AggregatedResult) {
for (const reporter of this._reporters) {
reporter.onRunComplete &&
Expand Down
Loading