Skip to content

Commit

Permalink
chore: create @jest/test-results package (#8034)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Mar 5, 2019
1 parent f526a91 commit afe2241
Show file tree
Hide file tree
Showing 69 changed files with 343 additions and 260 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -90,6 +90,7 @@
- `[jest-cli]`: Migrate to TypeScript ([#8024](https://github.com/facebook/jest/pull/8024))
- `[jest]`: Migrate to TypeScript ([#8024](https://github.com/facebook/jest/pull/8024))
- `[docs]` Update automock configuration, add note related to manual mocks ([#8051](https://github.com/facebook/jest/pull/8051))
- `[@jest/test-result]`: Extract TestResult types and helpers into a new separate package ([#8034](https://github.com/facebook/jest/pull/8034))

### Performance

Expand Down
1 change: 1 addition & 0 deletions packages/jest-circus/package.json
Expand Up @@ -12,6 +12,7 @@
"dependencies": {
"@babel/traverse": "^7.1.0",
"@jest/environment": "^24.1.0",
"@jest/test-result": "^24.1.0",
"@jest/types": "^24.1.0",
"@types/node": "*",
"chalk": "^2.0.1",
Expand Down
12 changes: 6 additions & 6 deletions packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapter.ts
Expand Up @@ -6,11 +6,12 @@
*/

import path from 'path';
import {Config, TestResult} from '@jest/types';
import {Config} from '@jest/types';
import {JestEnvironment} from '@jest/environment';
import {TestResult} from '@jest/test-result';
// eslint-disable-next-line import/no-extraneous-dependencies
import Runtime from 'jest-runtime';
import {SnapshotState} from 'jest-snapshot';
import {SnapshotStateType} from 'jest-snapshot';

const FRAMEWORK_INITIALIZER = require.resolve('./jestAdapterInit');

Expand All @@ -20,7 +21,7 @@ const jestAdapter = async (
environment: JestEnvironment,
runtime: Runtime,
testPath: string,
): Promise<TestResult.TestResult> => {
): Promise<TestResult> => {
const {
initialize,
runAndTransformResultsToJestFormat,
Expand Down Expand Up @@ -86,9 +87,8 @@ const jestAdapter = async (
};

const _addSnapshotData = (
results: TestResult.TestResult,
// TODO: make just snapshotState: SnapshotState when `jest-snapshot` is ESM
snapshotState: typeof SnapshotState.prototype,
results: TestResult,
snapshotState: SnapshotStateType,
) => {
results.testResults.forEach(({fullName, status}) => {
if (status === 'pending' || status === 'failed') {
Expand Down
Expand Up @@ -5,8 +5,8 @@
* LICENSE file in the root directory of this source tree.
*/

import {Config, TestResult} from '@jest/types';

import {Config} from '@jest/types';
import {AssertionResult, Status, TestResult} from '@jest/test-result';
import {extractExpectedAssertionsErrors, getState, setState} from 'expect';
import {formatExecError, formatResultsErrors} from 'jest-message-util';
import {
Expand Down Expand Up @@ -127,51 +127,51 @@ export const runAndTransformResultsToJestFormat = async ({
config: Config.ProjectConfig;
globalConfig: Config.GlobalConfig;
testPath: string;
}): Promise<TestResult.TestResult> => {
}): Promise<TestResult> => {
const runResult: RunResult = await run();

let numFailingTests = 0;
let numPassingTests = 0;
let numPendingTests = 0;
let numTodoTests = 0;

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

const ancestorTitles = testResult.testPath.filter(
name => name !== ROOT_DESCRIBE_BLOCK_NAME,
);
const title = ancestorTitles.pop();
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],
};
});
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],
};
},
);

let failureMessage = formatResultsErrors(
assertionResults,
Expand Down
1 change: 1 addition & 0 deletions packages/jest-circus/tsconfig.json
Expand Up @@ -11,6 +11,7 @@
{"path": "../jest-message-util"},
{"path": "../jest-runtime"},
{"path": "../jest-snapshot"},
{"path": "../jest-test-result"},
{"path": "../jest-types"},
{"path": "../jest-util"},
{"path": "../pretty-format"}
Expand Down
1 change: 1 addition & 0 deletions packages/jest-cli/package.json
Expand Up @@ -6,6 +6,7 @@
"types": "build/index.d.ts",
"dependencies": {
"@jest/core": "^24.1.0",
"@jest/test-result": "^24.1.0",
"@jest/types": "^24.1.0",
"chalk": "^2.0.1",
"exit": "^0.1.2",
Expand Down
5 changes: 3 additions & 2 deletions packages/jest-cli/src/cli/index.ts
Expand Up @@ -6,7 +6,8 @@
*/

import path from 'path';
import {Config, TestResult} from '@jest/types';
import {Config} from '@jest/types';
import {AggregatedResult} from '@jest/test-result';
import {clearLine} from 'jest-util';
import {validateCLIOptions} from 'jest-validate';
import {deprecationEntries} from 'jest-config';
Expand Down Expand Up @@ -104,7 +105,7 @@ const getProjectListFromCLIArgs = (
};

const readResultsAndExit = (
result: TestResult.AggregatedResult | null,
result: AggregatedResult | null,
globalConfig: Config.GlobalConfig,
) => {
const code = !result || result.success ? 0 : globalConfig.testFailureExitCode;
Expand Down
1 change: 1 addition & 0 deletions packages/jest-cli/tsconfig.json
Expand Up @@ -7,6 +7,7 @@
"references": [
{"path": "../jest-core"},
{"path": "../jest-config"},
{"path": "../jest-test-result"},
{"path": "../jest-types"},
{"path": "../jest-util"},
{"path": "../jest-validate"}
Expand Down
1 change: 1 addition & 0 deletions packages/jest-core/package.json
Expand Up @@ -8,6 +8,7 @@
"@jest/console": "^24.1.0",
"@jest/types": "^24.1.0",
"@jest/reporters": "^24.1.0",
"@jest/test-result": "^24.1.0",
"@jest/transform": "^24.1.0",
"ansi-escapes": "^3.0.0",
"chalk": "^2.0.1",
Expand Down
5 changes: 3 additions & 2 deletions packages/jest-core/src/FailedTestsCache.ts
Expand Up @@ -6,7 +6,8 @@
*/

import {Test} from 'jest-runner';
import {Config, TestResult} from '@jest/types';
import {Config} from '@jest/types';
import {TestResult} from '@jest/test-result';

type TestMap = {[key: string]: {[key: string]: boolean}};

Expand All @@ -22,7 +23,7 @@ export default class FailedTestsCache {
return tests.filter(testResult => enabledTestsMap[testResult.path]);
}

setTestResults(testResults: Array<TestResult.TestResult>) {
setTestResults(testResults: Array<TestResult>) {
this._enabledTestsMap = (testResults || [])
.filter(testResult => testResult.numFailingTests)
.reduce<TestMap>((suiteMap, testResult) => {
Expand Down
16 changes: 5 additions & 11 deletions packages/jest-core/src/ReporterDispatcher.ts
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import {TestResult} from '@jest/types';
import {AggregatedResult, TestResult} from '@jest/test-result';
import {Test} from 'jest-runner';
import {Context} from 'jest-runtime';
import {Reporter, ReporterOnStartOptions} from '@jest/reporters';
Expand All @@ -29,8 +29,8 @@ export default class ReporterDispatcher {

async onTestResult(
test: Test,
testResult: TestResult.TestResult,
results: TestResult.AggregatedResult,
testResult: TestResult,
results: AggregatedResult,
) {
for (const reporter of this._reporters) {
reporter.onTestResult &&
Expand All @@ -44,19 +44,13 @@ export default class ReporterDispatcher {
}
}

async onRunStart(
results: TestResult.AggregatedResult,
options: ReporterOnStartOptions,
) {
async onRunStart(results: AggregatedResult, options: ReporterOnStartOptions) {
for (const reporter of this._reporters) {
reporter.onRunStart && (await reporter.onRunStart(results, options));
}
}

async onRunComplete(
contexts: Set<Context>,
results: TestResult.AggregatedResult,
) {
async onRunComplete(contexts: Set<Context>, results: AggregatedResult) {
for (const reporter of this._reporters) {
reporter.onRunComplete &&
(await reporter.onRunComplete(contexts, results));
Expand Down
13 changes: 6 additions & 7 deletions packages/jest-core/src/SnapshotInteractiveMode.ts
Expand Up @@ -8,9 +8,8 @@

import chalk from 'chalk';
import ansiEscapes from 'ansi-escapes';
import {TestResult} from '@jest/types';
import {AggregatedResult, AssertionLocation} from '@jest/test-result';
import {KEYS} from 'jest-watcher';

import {pluralize, specialChars} from 'jest-util';

const {ARROW, CLEAR} = specialChars;
Expand All @@ -19,10 +18,10 @@ export default class SnapshotInteractiveMode {
private _pipe: NodeJS.WritableStream;
private _isActive: boolean;
private _updateTestRunnerConfig!: (
assertion: TestResult.AssertionLocation | null,
assertion: AssertionLocation | null,
shouldUpdateSnapshot: boolean,
) => unknown;
private _testAssertions!: Array<TestResult.AssertionLocation>;
private _testAssertions!: Array<AssertionLocation>;
private _countPaths!: number;
private _skippedNum: number;

Expand Down Expand Up @@ -205,7 +204,7 @@ export default class SnapshotInteractiveMode {
this._run(false);
}

updateWithResults(results: TestResult.AggregatedResult) {
updateWithResults(results: AggregatedResult) {
const hasSnapshotFailure = !!results.snapshot.failure;
if (hasSnapshotFailure) {
this._drawUIOverlay();
Expand All @@ -228,9 +227,9 @@ export default class SnapshotInteractiveMode {
}

run(
failedSnapshotTestAssertions: Array<TestResult.AssertionLocation>,
failedSnapshotTestAssertions: Array<AssertionLocation>,
onConfigChange: (
assertion: TestResult.AssertionLocation | null,
assertion: AssertionLocation | null,
shouldUpdateSnapshot: boolean,
) => unknown,
) {
Expand Down
6 changes: 3 additions & 3 deletions packages/jest-core/src/TestNamePatternPrompt.ts
Expand Up @@ -12,11 +12,11 @@ import {
printPatternCaret,
printRestoredPatternCaret,
} from 'jest-watcher';
import {TestResult} from '@jest/types';
import {TestResult} from '@jest/test-result';

// TODO: Make underscored props `private`
export default class TestNamePatternPrompt extends PatternPrompt {
_cachedTestResults: Array<TestResult.TestResult>;
_cachedTestResults: Array<TestResult>;

constructor(pipe: NodeJS.WritableStream, prompt: Prompt) {
super(pipe, prompt);
Expand Down Expand Up @@ -57,7 +57,7 @@ export default class TestNamePatternPrompt extends PatternPrompt {
return matchedTests;
}

updateCachedTestResults(testResults: Array<TestResult.TestResult> = []) {
updateCachedTestResults(testResults: Array<TestResult> = []) {
this._cachedTestResults = testResults;
}
}
16 changes: 8 additions & 8 deletions packages/jest-core/src/TestScheduler.ts
Expand Up @@ -7,7 +7,7 @@

import chalk from 'chalk';
import {formatExecError} from 'jest-message-util';
import {Config, TestResult} from '@jest/types';
import {Config} from '@jest/types';
import snapshot from 'jest-snapshot';
import TestRunner, {Test} from 'jest-runner';
import {Context} from 'jest-runtime';
Expand All @@ -22,9 +22,12 @@ import {
import exit from 'exit';
import {
addResult,
AggregatedResult,
buildFailureTestResult,
makeEmptyAggregatedTestResult,
} from './testResultHelpers';
SerializableError,
TestResult,
} from '@jest/test-result';
import ReporterDispatcher from './ReporterDispatcher';
import TestWatcher from './TestWatcher';
import {shouldRunInBand} from './testSchedulerHelper';
Expand Down Expand Up @@ -90,7 +93,7 @@ export default class TestScheduler {
timings,
);

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

const onFailure = async (
test: Test,
error: TestResult.SerializableError,
) => {
const onFailure = async (test: Test, error: SerializableError) => {
if (watcher.isInterrupted()) {
return;
}
Expand Down Expand Up @@ -345,7 +345,7 @@ export default class TestScheduler {

private _bailIfNeeded(
contexts: Set<Context>,
aggregatedResults: TestResult.AggregatedResult,
aggregatedResults: AggregatedResult,
watcher: TestWatcher,
): Promise<void> {
if (
Expand Down

0 comments on commit afe2241

Please sign in to comment.