Skip to content

Commit 8a0e965

Browse files
committed
Add testLocationInResults support to jest-circus
Part of jestjs#4362
1 parent 50507ec commit 8a0e965

File tree

7 files changed

+31
-8
lines changed

7 files changed

+31
-8
lines changed

integration-tests/__tests__/location_in_results.test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,8 @@ it('adds correct location info when provided with flag', () => {
2929
expect(result.success).toBe(true);
3030
expect(result.numTotalTests).toBe(2);
3131
expect(assertions[0].location).toEqual({column: 1, line: 10});
32+
33+
// Technically the column should be 3, but callsites is not correct.
34+
// jest-circus uses stack-utils + asyncErrors which resolves this.
3235
expect(assertions[1].location).toEqual({column: 2, line: 15});
3336
});

packages/jest-circus/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"jest-matcher-utils": "^23.0.0",
1515
"jest-message-util": "^23.0.0",
1616
"jest-snapshot": "^23.0.0",
17-
"pretty-format": "^23.0.0"
17+
"pretty-format": "^23.0.0",
18+
"stack-utils": "^1.0.1"
1819
},
1920
"devDependencies": {
2021
"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
@@ -100,7 +100,7 @@ export const runAndTransformResultsToJestFormat = async ({
100100
globalConfig: GlobalConfig,
101101
testPath: string,
102102
}): Promise<TestResult> => {
103-
const result = await run();
103+
const result = await run(config);
104104

105105
let numFailingTests = 0;
106106
let numPassingTests = 0;
@@ -124,12 +124,12 @@ export const runAndTransformResultsToJestFormat = async ({
124124
);
125125
const title = ancestorTitles.pop();
126126

127-
// $FlowFixMe Types are slightly incompatible and need to be refactored
128127
return {
129128
ancestorTitles,
130129
duration: testResult.duration,
131130
failureMessages: testResult.errors,
132131
fullName: ancestorTitles.concat(title).join(' '),
132+
location: testResult.location,
133133
numPassingAsserts: 0,
134134
status,
135135
title: testResult.testPath[testResult.testPath.length - 1],

packages/jest-circus/src/run.js

Lines changed: 4 additions & 2 deletions
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
TestEntry,
1214
TestResults,
@@ -25,12 +27,12 @@ import {
2527
makeTestResults,
2628
} from './utils';
2729

28-
const run = async (): Promise<TestResults> => {
30+
const run = async (config: ProjectConfig): Promise<TestResults> => {
2931
const {rootDescribeBlock} = getState();
3032
dispatch({name: 'run_start'});
3133
await _runTestsForDescribeBlock(rootDescribeBlock);
3234
dispatch({name: 'run_finish'});
33-
return makeTestResults(getState().rootDescribeBlock);
35+
return makeTestResults(rootDescribeBlock, config);
3436
};
3537

3638
const _runTestsForDescribeBlock = async (describeBlock: DescribeBlock) => {

packages/jest-circus/src/utils.js

Lines changed: 18 additions & 2 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,
@@ -22,6 +23,8 @@ import type {
2223
TestResults,
2324
} from 'types/Circus';
2425

26+
import StackUtils from 'stack-utils';
27+
2528
import prettyFormat from 'pretty-format';
2629

2730
export const makeDescribe = (
@@ -213,7 +216,11 @@ export const getTestDuration = (test: TestEntry): ?number => {
213216
return startedAt ? Date.now() - startedAt : null;
214217
};
215218

216-
export const makeTestResults = (describeBlock: DescribeBlock): TestResults => {
219+
export const makeTestResults = (
220+
describeBlock: DescribeBlock,
221+
config: ProjectConfig,
222+
): TestResults => {
223+
const stackUtils = new StackUtils();
217224
let testResults = [];
218225
for (const test of describeBlock.tests) {
219226
const testPath = [];
@@ -227,16 +234,25 @@ export const makeTestResults = (describeBlock: DescribeBlock): TestResults => {
227234
if (!status) {
228235
throw new Error('Status should be present after tests are run.');
229236
}
237+
238+
let location = null;
239+
if (config.testLocationInResults) {
240+
const stackLine = test.asyncError.stack.split('\n')[1];
241+
const {line, column} = stackUtils.parseLine(stackLine);
242+
location = {column, line};
243+
}
244+
230245
testResults.push({
231246
duration: test.duration,
232247
errors: test.errors.map(_formatError),
248+
location,
233249
status,
234250
testPath,
235251
});
236252
}
237253

238254
for (const child of describeBlock.children) {
239-
testResults = testResults.concat(makeTestResults(child));
255+
testResults = testResults.concat(makeTestResults(child, config));
240256
}
241257

242258
return testResults;

packages/jest-runner/src/run_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ async function runTestInternal(
6969
/* $FlowFixMe */
7070
const TestEnvironment = (require(testEnvironment): EnvironmentClass);
7171
/* $FlowFixMe */
72-
const testFramework = (require(config.testRunner): TestFramework);
72+
const testFramework = (require(testFramework): TestFramework);
7373
/* $FlowFixMe */
7474
const Runtime = (require(config.moduleLoader || 'jest-runtime'): Class<
7575
RuntimeClass,

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>,
141142
|};
142143

0 commit comments

Comments
 (0)