Skip to content

Commit c11b070

Browse files
committed
chore: create @jest/test-results package
1 parent ae6d6eb commit c11b070

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+229
-157
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
- `[@jest/core]`: Migrate to TypeScript ([#7998](https://github.com/facebook/jest/pull/7998))
8181
- `[@jest/source-map]`: Extract `getCallsite` function from `jest-util` into a new separate package ([#8029](https://github.com/facebook/jest/pull/8029))
8282
- `[@jest/console]`: Extract custom `console` implementations from `jest-util` into a new separate package ([#8030](https://github.com/facebook/jest/pull/8030))
83+
- `[@jest/test-result]`: Extract TestResult types and helpers into a new separate package ([#8034](https://github.com/facebook/jest/pull/8034))
8384

8485
### Performance
8586

packages/jest-circus/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"dependencies": {
1313
"@babel/traverse": "^7.1.0",
1414
"@jest/environment": "^24.1.0",
15+
"@jest/test-result": "^24.1.0",
1516
"@jest/types": "^24.1.0",
1617
"@types/node": "*",
1718
"chalk": "^2.0.1",

packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapter.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
*/
77

88
import path from 'path';
9-
import {Config, TestResult} from '@jest/types';
9+
import {Config} from '@jest/types';
1010
import {JestEnvironment} from '@jest/environment';
11+
import {TestResult} from '@jest/test-result';
1112
// eslint-disable-next-line import/no-extraneous-dependencies
1213
import Runtime from 'jest-runtime';
1314
import {SnapshotState} from 'jest-snapshot';
@@ -20,7 +21,7 @@ const jestAdapter = async (
2021
environment: JestEnvironment,
2122
runtime: Runtime,
2223
testPath: string,
23-
): Promise<TestResult.TestResult> => {
24+
): Promise<TestResult> => {
2425
const {
2526
initialize,
2627
runAndTransformResultsToJestFormat,
@@ -86,7 +87,7 @@ const jestAdapter = async (
8687
};
8788

8889
const _addSnapshotData = (
89-
results: TestResult.TestResult,
90+
results: TestResult,
9091
// TODO: make just snapshotState: SnapshotState when `jest-snapshot` is ESM
9192
snapshotState: typeof SnapshotState.prototype,
9293
) => {

packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import {Config, TestResult} from '@jest/types';
9-
8+
import {Config} from '@jest/types';
9+
import {AssertionResult, Status, TestResult} from '@jest/test-result';
1010
import {extractExpectedAssertionsErrors, getState, setState} from 'expect';
1111
import {formatExecError, formatResultsErrors} from 'jest-message-util';
1212
import {
@@ -127,51 +127,51 @@ export const runAndTransformResultsToJestFormat = async ({
127127
config: Config.ProjectConfig;
128128
globalConfig: Config.GlobalConfig;
129129
testPath: string;
130-
}): Promise<TestResult.TestResult> => {
130+
}): Promise<TestResult> => {
131131
const runResult: RunResult = await run();
132132

133133
let numFailingTests = 0;
134134
let numPassingTests = 0;
135135
let numPendingTests = 0;
136136
let numTodoTests = 0;
137137

138-
const assertionResults: Array<
139-
TestResult.AssertionResult
140-
> = runResult.testResults.map(testResult => {
141-
let status: TestResult.Status;
142-
if (testResult.status === 'skip') {
143-
status = 'pending';
144-
numPendingTests += 1;
145-
} else if (testResult.status === 'todo') {
146-
status = 'todo';
147-
numTodoTests += 1;
148-
} else if (testResult.errors.length) {
149-
status = 'failed';
150-
numFailingTests += 1;
151-
} else {
152-
status = 'passed';
153-
numPassingTests += 1;
154-
}
138+
const assertionResults: Array<AssertionResult> = runResult.testResults.map(
139+
testResult => {
140+
let status: Status;
141+
if (testResult.status === 'skip') {
142+
status = 'pending';
143+
numPendingTests += 1;
144+
} else if (testResult.status === 'todo') {
145+
status = 'todo';
146+
numTodoTests += 1;
147+
} else if (testResult.errors.length) {
148+
status = 'failed';
149+
numFailingTests += 1;
150+
} else {
151+
status = 'passed';
152+
numPassingTests += 1;
153+
}
155154

156-
const ancestorTitles = testResult.testPath.filter(
157-
name => name !== ROOT_DESCRIBE_BLOCK_NAME,
158-
);
159-
const title = ancestorTitles.pop();
155+
const ancestorTitles = testResult.testPath.filter(
156+
name => name !== ROOT_DESCRIBE_BLOCK_NAME,
157+
);
158+
const title = ancestorTitles.pop();
160159

161-
return {
162-
ancestorTitles,
163-
duration: testResult.duration,
164-
failureMessages: testResult.errors,
165-
fullName: title
166-
? ancestorTitles.concat(title).join(' ')
167-
: ancestorTitles.join(' '),
168-
invocations: testResult.invocations,
169-
location: testResult.location,
170-
numPassingAsserts: 0,
171-
status,
172-
title: testResult.testPath[testResult.testPath.length - 1],
173-
};
174-
});
160+
return {
161+
ancestorTitles,
162+
duration: testResult.duration,
163+
failureMessages: testResult.errors,
164+
fullName: title
165+
? ancestorTitles.concat(title).join(' ')
166+
: ancestorTitles.join(' '),
167+
invocations: testResult.invocations,
168+
location: testResult.location,
169+
numPassingAsserts: 0,
170+
status,
171+
title: testResult.testPath[testResult.testPath.length - 1],
172+
};
173+
},
174+
);
175175

176176
let failureMessage = formatResultsErrors(
177177
assertionResults,

packages/jest-circus/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
{"path": "../jest-message-util"},
1212
{"path": "../jest-runtime"},
1313
{"path": "../jest-snapshot"},
14+
{"path": "../jest-test-result"},
1415
{"path": "../jest-types"},
1516
{"path": "../jest-util"},
1617
{"path": "../pretty-format"}

packages/jest-core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"@jest/console": "^24.1.0",
99
"@jest/types": "^24.1.0",
1010
"@jest/reporters": "^24.1.0",
11+
"@jest/test-result": "^24.1.0",
1112
"@jest/transform": "^24.1.0",
1213
"ansi-escapes": "^3.0.0",
1314
"chalk": "^2.0.1",

packages/jest-core/src/FailedTestsCache.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
*/
77

88
import {Test} from 'jest-runner';
9-
import {Config, TestResult} from '@jest/types';
9+
import {Config} from '@jest/types';
10+
import {TestResult} from '@jest/test-result';
1011

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

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

25-
setTestResults(testResults: Array<TestResult.TestResult>) {
26+
setTestResults(testResults: Array<TestResult>) {
2627
this._enabledTestsMap = (testResults || [])
2728
.filter(testResult => testResult.numFailingTests)
2829
.reduce<TestMap>((suiteMap, testResult) => {

packages/jest-core/src/ReporterDispatcher.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import {TestResult} from '@jest/types';
8+
import {AggregatedResult, TestResult} from '@jest/test-result';
99
import {Test} from 'jest-runner';
1010
import {Context} from 'jest-runtime';
1111
import {Reporter, ReporterOnStartOptions} from './types';
@@ -29,8 +29,8 @@ export default class ReporterDispatcher {
2929

3030
async onTestResult(
3131
test: Test,
32-
testResult: TestResult.TestResult,
33-
results: TestResult.AggregatedResult,
32+
testResult: TestResult,
33+
results: AggregatedResult,
3434
) {
3535
for (const reporter of this._reporters) {
3636
reporter.onTestResult &&
@@ -44,19 +44,13 @@ export default class ReporterDispatcher {
4444
}
4545
}
4646

47-
async onRunStart(
48-
results: TestResult.AggregatedResult,
49-
options: ReporterOnStartOptions,
50-
) {
47+
async onRunStart(results: AggregatedResult, options: ReporterOnStartOptions) {
5148
for (const reporter of this._reporters) {
5249
reporter.onRunStart && (await reporter.onRunStart(results, options));
5350
}
5451
}
5552

56-
async onRunComplete(
57-
contexts: Set<Context>,
58-
results: TestResult.AggregatedResult,
59-
) {
53+
async onRunComplete(contexts: Set<Context>, results: AggregatedResult) {
6054
for (const reporter of this._reporters) {
6155
reporter.onRunComplete &&
6256
(await reporter.onRunComplete(contexts, results));

packages/jest-core/src/SnapshotInteractiveMode.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88

99
import chalk from 'chalk';
1010
import ansiEscapes from 'ansi-escapes';
11-
import {TestResult} from '@jest/types';
11+
import {AggregatedResult, AssertionLocation} from '@jest/test-result';
1212
import {KEYS} from 'jest-watcher';
13-
1413
import {pluralize, specialChars} from 'jest-util';
1514

1615
const {ARROW, CLEAR} = specialChars;
@@ -19,10 +18,10 @@ export default class SnapshotInteractiveMode {
1918
private _pipe: NodeJS.WritableStream;
2019
private _isActive: boolean;
2120
private _updateTestRunnerConfig!: (
22-
assertion: TestResult.AssertionLocation | null,
21+
assertion: AssertionLocation | null,
2322
shouldUpdateSnapshot: boolean,
2423
) => unknown;
25-
private _testAssertions!: Array<TestResult.AssertionLocation>;
24+
private _testAssertions!: Array<AssertionLocation>;
2625
private _countPaths!: number;
2726
private _skippedNum: number;
2827

@@ -205,7 +204,7 @@ export default class SnapshotInteractiveMode {
205204
this._run(false);
206205
}
207206

208-
updateWithResults(results: TestResult.AggregatedResult) {
207+
updateWithResults(results: AggregatedResult) {
209208
const hasSnapshotFailure = !!results.snapshot.failure;
210209
if (hasSnapshotFailure) {
211210
this._drawUIOverlay();
@@ -228,9 +227,9 @@ export default class SnapshotInteractiveMode {
228227
}
229228

230229
run(
231-
failedSnapshotTestAssertions: Array<TestResult.AssertionLocation>,
230+
failedSnapshotTestAssertions: Array<AssertionLocation>,
232231
onConfigChange: (
233-
assertion: TestResult.AssertionLocation | null,
232+
assertion: AssertionLocation | null,
234233
shouldUpdateSnapshot: boolean,
235234
) => unknown,
236235
) {

packages/jest-core/src/TestNamePatternPrompt.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ import {
1212
printPatternCaret,
1313
printRestoredPatternCaret,
1414
} from 'jest-watcher';
15-
import {TestResult} from '@jest/types';
15+
import {TestResult} from '@jest/test-result';
1616

1717
// TODO: Make underscored props `private`
1818
export default class TestNamePatternPrompt extends PatternPrompt {
19-
_cachedTestResults: Array<TestResult.TestResult>;
19+
_cachedTestResults: Array<TestResult>;
2020

2121
constructor(pipe: NodeJS.WritableStream, prompt: Prompt) {
2222
super(pipe, prompt);
@@ -57,7 +57,7 @@ export default class TestNamePatternPrompt extends PatternPrompt {
5757
return matchedTests;
5858
}
5959

60-
updateCachedTestResults(testResults: Array<TestResult.TestResult> = []) {
60+
updateCachedTestResults(testResults: Array<TestResult> = []) {
6161
this._cachedTestResults = testResults;
6262
}
6363
}

0 commit comments

Comments
 (0)