Skip to content

Commit

Permalink
chore: create @jest/test-results package
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Mar 3, 2019
1 parent d185d22 commit 51fe9a5
Show file tree
Hide file tree
Showing 62 changed files with 314 additions and 237 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -81,6 +81,7 @@
- `[@jest/core]`: Migrate to TypeScript ([#7998](https://github.com/facebook/jest/pull/7998))
- `[@jest/source-map]`: Extract `getCallsite` function from `jest-util` into a new separate package ([#8029](https://github.com/facebook/jest/pull/8029))
- `[@jest/console]`: Extract custom `console` implementations from `jest-util` into a new separate package ([#8030](https://github.com/facebook/jest/pull/8030))
- `[@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
Expand Up @@ -6,8 +6,9 @@
*/

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';
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,7 +87,7 @@ const jestAdapter = async (
};

const _addSnapshotData = (
results: TestResult.TestResult,
results: TestResult,
// TODO: make just snapshotState: SnapshotState when `jest-snapshot` is ESM
snapshotState: typeof SnapshotState.prototype,
) => {
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-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 './types';
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 @@ -91,7 +94,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 @@ -127,10 +130,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 @@ -346,7 +346,7 @@ export default class TestScheduler {

private _bailIfNeeded(
contexts: Set<Context>,
aggregatedResults: TestResult.AggregatedResult,
aggregatedResults: AggregatedResult,
watcher: TestWatcher,
): Promise<void> {
if (
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-core/src/TestSequencer.ts
Expand Up @@ -6,7 +6,7 @@
*/

import fs from 'fs';
import {TestResult} from '@jest/types';
import {AggregatedResult} from '@jest/test-result';
import {getCacheFilePath} from 'jest-haste-map';
import {Context} from 'jest-runtime';
import {Test} from 'jest-runner';
Expand Down Expand Up @@ -90,7 +90,7 @@ export default class TestSequencer {
});
}

cacheResults(tests: Array<Test>, results: TestResult.AggregatedResult) {
cacheResults(tests: Array<Test>, results: AggregatedResult) {
const map = Object.create(null);
tests.forEach(test => (map[test.path] = test));
results.testResults.forEach(testResult => {
Expand Down
9 changes: 5 additions & 4 deletions packages/jest-core/src/cli/index.ts
Expand Up @@ -5,7 +5,8 @@
* LICENSE file in the root directory of this source tree.
*/

import {Config, TestResult} from '@jest/types';
import {Config} from '@jest/types';
import {AggregatedResult} from '@jest/test-result';
import {CustomConsole} from '@jest/console';
import {createDirectory, preRunMessage} from 'jest-util';
import {readConfigs} from 'jest-config';
Expand All @@ -27,20 +28,20 @@ import logDebugMessages from '../lib/log_debug_messages';

const {print: preRunMessagePrint} = preRunMessage;

type OnCompleteCallback = (results: TestResult.AggregatedResult) => void;
type OnCompleteCallback = (results: AggregatedResult) => void;

export const runCLI = async (
argv: Config.Argv,
projects: Array<Config.Path>,
): Promise<{
results: TestResult.AggregatedResult;
results: AggregatedResult;
globalConfig: Config.GlobalConfig;
}> => {
const realFs = require('fs');
const fs = require('graceful-fs');
fs.gracefulify(realFs);

let results: TestResult.AggregatedResult | undefined;
let results: AggregatedResult | undefined;

// If we output a JSON object, we can't write anything to stdout, since
// it'll break the JSON structure and it won't be valid.
Expand Down

0 comments on commit 51fe9a5

Please sign in to comment.