Skip to content

Commit afe2241

Browse files
authored
chore: create @jest/test-results package (#8034)
1 parent f526a91 commit afe2241

69 files changed

Lines changed: 343 additions & 260 deletions

Some content is hidden

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

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
- `[jest-cli]`: Migrate to TypeScript ([#8024](https://github.com/facebook/jest/pull/8024))
9191
- `[jest]`: Migrate to TypeScript ([#8024](https://github.com/facebook/jest/pull/8024))
9292
- `[docs]` Update automock configuration, add note related to manual mocks ([#8051](https://github.com/facebook/jest/pull/8051))
93+
- `[@jest/test-result]`: Extract TestResult types and helpers into a new separate package ([#8034](https://github.com/facebook/jest/pull/8034))
9394

9495
### Performance
9596

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: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
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';
13-
import {SnapshotState} from 'jest-snapshot';
14+
import {SnapshotStateType} from 'jest-snapshot';
1415

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

@@ -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,9 +87,8 @@ const jestAdapter = async (
8687
};
8788

8889
const _addSnapshotData = (
89-
results: TestResult.TestResult,
90-
// TODO: make just snapshotState: SnapshotState when `jest-snapshot` is ESM
91-
snapshotState: typeof SnapshotState.prototype,
90+
results: TestResult,
91+
snapshotState: SnapshotStateType,
9292
) => {
9393
results.testResults.forEach(({fullName, status}) => {
9494
if (status === 'pending' || status === 'failed') {

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-cli/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"types": "build/index.d.ts",
77
"dependencies": {
88
"@jest/core": "^24.1.0",
9+
"@jest/test-result": "^24.1.0",
910
"@jest/types": "^24.1.0",
1011
"chalk": "^2.0.1",
1112
"exit": "^0.1.2",

packages/jest-cli/src/cli/index.ts

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

88
import path from 'path';
9-
import {Config, TestResult} from '@jest/types';
9+
import {Config} from '@jest/types';
10+
import {AggregatedResult} from '@jest/test-result';
1011
import {clearLine} from 'jest-util';
1112
import {validateCLIOptions} from 'jest-validate';
1213
import {deprecationEntries} from 'jest-config';
@@ -104,7 +105,7 @@ const getProjectListFromCLIArgs = (
104105
};
105106

106107
const readResultsAndExit = (
107-
result: TestResult.AggregatedResult | null,
108+
result: AggregatedResult | null,
108109
globalConfig: Config.GlobalConfig,
109110
) => {
110111
const code = !result || result.success ? 0 : globalConfig.testFailureExitCode;

packages/jest-cli/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"references": [
88
{"path": "../jest-core"},
99
{"path": "../jest-config"},
10+
{"path": "../jest-test-result"},
1011
{"path": "../jest-types"},
1112
{"path": "../jest-util"},
1213
{"path": "../jest-validate"}

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) => {

0 commit comments

Comments
 (0)