Skip to content

Commit 20d7577

Browse files
committed
Add testLocationInResults support to jest-circus
Part of jestjs#4362
1 parent 3d127ff commit 20d7577

File tree

7 files changed

+38
-8
lines changed

7 files changed

+38
-8
lines changed

integration-tests/__tests__/location_in_results.test.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import skipOnJestCicrus from '../../scripts/SkipOnJestCircus';
1212
const runJest = require('../runJest');
13+
const SkipOnJestCircus = require('../../scripts/SkipOnJestCircus');
1314

1415
skipOnJestCicrus.suite();
1516

@@ -32,5 +33,11 @@ it('adds correct location info when provided with flag', () => {
3233
expect(result.success).toBe(true);
3334
expect(result.numTotalTests).toBe(2);
3435
expect(assertions[0].location).toEqual({column: 1, line: 10});
35-
expect(assertions[1].location).toEqual({column: 2, line: 15});
36+
37+
// Technically the column should be 3, but callsites is not correct.
38+
// jest-circus uses stack-utils + asyncErrors which resolves this.
39+
expect(assertions[1].location).toEqual({
40+
column: SkipOnJestCircus.isJestCircusRun() ? 3 : 2,
41+
line: 15,
42+
});
3643
});

packages/jest-circus/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"jest-message-util": "^23.0.0",
1616
"jest-snapshot": "^23.0.0",
1717
"jest-util": "^23.0.0",
18-
"pretty-format": "^23.0.0"
18+
"pretty-format": "^23.0.0",
19+
"stack-utils": "^1.0.1"
1920
},
2021
"devDependencies": {
2122
"jest-runtime": "^23.0.0"

packages/jest-circus/src/legacy_code_todo_rewrite/jest_adapter_init.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export const runAndTransformResultsToJestFormat = async ({
102102
globalConfig: GlobalConfig,
103103
testPath: string,
104104
}): Promise<TestResult> => {
105-
const runResult = await run();
105+
const runResult = await run(config);
106106

107107
let numFailingTests = 0;
108108
let numPassingTests = 0;
@@ -131,14 +131,14 @@ export const runAndTransformResultsToJestFormat = async ({
131131
duration: testResult.duration,
132132
failureMessages: testResult.errors,
133133
fullName: ancestorTitles.concat(title).join(' '),
134+
location: testResult.location,
134135
numPassingAsserts: 0,
135136
status,
136137
title: testResult.testPath[testResult.testPath.length - 1],
137138
};
138139
});
139140

140141
let failureMessage = formatResultsErrors(
141-
// $FlowFixMe Types are slightly incompatible and need to be refactored
142142
assertionResults,
143143
config,
144144
globalConfig,

packages/jest-circus/src/run.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
* @flow strict-local
88
*/
99

10+
import type {ProjectConfig} from 'types/Config';
11+
1012
import type {
1113
RunResult,
1214
TestEntry,
@@ -28,14 +30,15 @@ import {
2830

2931
const Promise = getOriginalPromise();
3032

31-
const run = async (): Promise<RunResult> => {
33+
const run = async (config: ProjectConfig): Promise<RunResult> => {
3234
const {rootDescribeBlock} = getState();
3335
dispatch({name: 'run_start'});
3436
await _runTestsForDescribeBlock(rootDescribeBlock);
3537
dispatch({name: 'run_finish'});
3638
return makeRunResult(
3739
getState().rootDescribeBlock,
3840
getState().unhandledErrors,
41+
config,
3942
);
4043
};
4144

packages/jest-circus/src/utils.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* @flow strict-local
88
*/
99

10+
import type {ProjectConfig} from 'types/Config';
1011
import type {
1112
AsyncFn,
1213
BlockMode,
@@ -24,6 +25,8 @@ import type {
2425
} from 'types/Circus';
2526
import {convertDescriptorToString} from 'jest-util';
2627

28+
import StackUtils from 'stack-utils';
29+
2730
import prettyFormat from 'pretty-format';
2831

2932
// Try getting the real promise object from the context, if available. Someone
@@ -222,14 +225,16 @@ export const getTestDuration = (test: TestEntry): ?number => {
222225
export const makeRunResult = (
223226
describeBlock: DescribeBlock,
224227
unhandledErrors: Array<Error>,
228+
config: ProjectConfig,
225229
): RunResult => {
226230
return {
227-
testResults: makeTestResults(describeBlock),
231+
testResults: makeTestResults(describeBlock, config),
228232
unhandledErrors: unhandledErrors.map(_formatError),
229233
};
230234
};
231235

232-
const makeTestResults = (describeBlock: DescribeBlock): TestResults => {
236+
const makeTestResults = (describeBlock: DescribeBlock, config): TestResults => {
237+
const stackUtils = new StackUtils();
233238
let testResults = [];
234239
for (const test of describeBlock.tests) {
235240
const testPath = [];
@@ -243,16 +248,25 @@ const makeTestResults = (describeBlock: DescribeBlock): TestResults => {
243248
if (!status) {
244249
throw new Error('Status should be present after tests are run.');
245250
}
251+
252+
let location = null;
253+
if (config.testLocationInResults) {
254+
const stackLine = test.asyncError.stack.split('\n')[1];
255+
const {line, column} = stackUtils.parseLine(stackLine);
256+
location = {column, line};
257+
}
258+
246259
testResults.push({
247260
duration: test.duration,
248261
errors: test.errors.map(_formatError),
262+
location,
249263
status,
250264
testPath,
251265
});
252266
}
253267

254268
for (const child of describeBlock.children) {
255-
testResults = testResults.concat(makeTestResults(child));
269+
testResults = testResults.concat(makeTestResults(child, config));
256270
}
257271

258272
return testResults;

scripts/SkipOnJestCircus.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
/* eslint-disable jest/no-focused-tests */
1111

1212
const SkipOnJestCircus = {
13+
isJestCircusRun() {
14+
return process.env.JEST_CIRCUS === '1';
15+
},
16+
1317
suite() {
1418
if (process.env.JEST_CIRCUS === '1') {
1519
fit('does not work on jest-circus', () => {

types/Circus.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ export type TestResult = {|
137137
duration: ?number,
138138
errors: Array<FormattedError>,
139139
status: TestStatus,
140+
location: ?{|column: number, line: number|},
140141
testPath: Array<TestName | BlockName>,
141142
|};
142143

0 commit comments

Comments
 (0)