Skip to content

Commit

Permalink
Add testLocationInResults support to jest-circus
Browse files Browse the repository at this point in the history
Part of jestjs#4362
  • Loading branch information
captbaritone committed May 26, 2018
1 parent 33fed0b commit eaa6980
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 16 deletions.
9 changes: 8 additions & 1 deletion integration-tests/__tests__/location_in_results.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
'use strict';

const runJest = require('../runJest');
const SkipOnJestCircus = require('../../scripts/SkipOnJestCircus');

it('defaults to null for location', () => {
const result = runJest.json('location-in-results').json;
Expand All @@ -29,5 +30,11 @@ it('adds correct location info when provided with flag', () => {
expect(result.success).toBe(true);
expect(result.numTotalTests).toBe(2);
expect(assertions[0].location).toEqual({column: 1, line: 10});
expect(assertions[1].location).toEqual({column: 2, line: 15});

// Technically the column should be 3, but callsites is not correct.
// jest-circus uses stack-utils + asyncErrors which resolves this.
expect(assertions[1].location).toEqual({
column: SkipOnJestCircus.isJestCircusRun() ? 3 : 2,
line: 15,
});
});
3 changes: 2 additions & 1 deletion packages/jest-circus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"jest-message-util": "^23.0.0",
"jest-snapshot": "^23.0.0",
"jest-util": "^23.0.0",
"pretty-format": "^23.0.0"
"pretty-format": "^23.0.0",
"stack-utils": "^1.0.1"
},
"devDependencies": {
"jest-runtime": "^23.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export const runAndTransformResultsToJestFormat = async ({
globalConfig: GlobalConfig,
testPath: string,
}): Promise<TestResult> => {
const runResult = await run();
const runResult = await run(config);

let numFailingTests = 0;
let numPassingTests = 0;
Expand Down Expand Up @@ -131,14 +131,14 @@ export const runAndTransformResultsToJestFormat = async ({
duration: testResult.duration,
failureMessages: testResult.errors,
fullName: ancestorTitles.concat(title).join(' '),
location: testResult.location,
numPassingAsserts: 0,
status,
title: testResult.testPath[testResult.testPath.length - 1],
};
});

let failureMessage = formatResultsErrors(
// $FlowFixMe Types are slightly incompatible and need to be refactored
assertionResults,
config,
globalConfig,
Expand Down
5 changes: 4 additions & 1 deletion packages/jest-circus/src/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* @flow strict-local
*/

import type {ProjectConfig} from 'types/Config';

import type {
RunResult,
TestEntry,
Expand All @@ -28,14 +30,15 @@ import {

const Promise = getOriginalPromise();

const run = async (): Promise<RunResult> => {
const run = async (config: ProjectConfig): Promise<RunResult> => {
const {rootDescribeBlock} = getState();
dispatch({name: 'run_start'});
await _runTestsForDescribeBlock(rootDescribeBlock);
dispatch({name: 'run_finish'});
return makeRunResult(
getState().rootDescribeBlock,
getState().unhandledErrors,
config,
);
};

Expand Down
20 changes: 17 additions & 3 deletions packages/jest-circus/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* @flow strict-local
*/

import type {ProjectConfig} from 'types/Config';
import type {
AsyncFn,
BlockMode,
Expand All @@ -24,6 +25,8 @@ import type {
} from 'types/Circus';
import {convertDescriptorToString} from 'jest-util';

import StackUtils from 'stack-utils';

import prettyFormat from 'pretty-format';

// Try getting the real promise object from the context, if available. Someone
Expand Down Expand Up @@ -222,14 +225,16 @@ export const getTestDuration = (test: TestEntry): ?number => {
export const makeRunResult = (
describeBlock: DescribeBlock,
unhandledErrors: Array<Error>,
config: ProjectConfig,
): RunResult => {
return {
testResults: makeTestResults(describeBlock),
testResults: makeTestResults(describeBlock, config),
unhandledErrors: unhandledErrors.map(_formatError),
};
};

const makeTestResults = (describeBlock: DescribeBlock): TestResults => {
const makeTestResults = (describeBlock: DescribeBlock, config): TestResults => {
const stackUtils = new StackUtils();
let testResults = [];
for (const test of describeBlock.tests) {
const testPath = [];
Expand All @@ -243,16 +248,25 @@ const makeTestResults = (describeBlock: DescribeBlock): TestResults => {
if (!status) {
throw new Error('Status should be present after tests are run.');
}

let location = null;
if (config.testLocationInResults) {
const stackLine = test.asyncError.stack.split('\n')[1];
const {line, column} = stackUtils.parseLine(stackLine);
location = {column, line};
}

testResults.push({
duration: test.duration,
errors: test.errors.map(_formatError),
location,
status,
testPath,
});
}

for (const child of describeBlock.children) {
testResults = testResults.concat(makeTestResults(child));
testResults = testResults.concat(makeTestResults(child, config));
}

return testResults;
Expand Down
12 changes: 4 additions & 8 deletions scripts/SkipOnJestCircus.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,17 @@
/* eslint-disable jest/no-focused-tests */

const SkipOnJestCircus = {
isJestCircusRun() {
return process.env.JEST_CIRCUS === '1';
},

suite() {
if (process.env.JEST_CIRCUS === '1') {
fit('does not work on jest-circus', () => {
console.warn('[SKIP] Does not work on jest-circus');
});
}
},

test() {
if (process.env.JEST_CIRCUS === '1') {
console.warn('[SKIP] Does not work on jest-circus');
return true;
}
return false;
},
};

module.exports = SkipOnJestCircus;
1 change: 1 addition & 0 deletions types/Circus.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export type TestResult = {|
duration: ?number,
errors: Array<FormattedError>,
status: TestStatus,
location: ?{|column: number, line: number|},
testPath: Array<TestName | BlockName>,
|};

Expand Down

0 comments on commit eaa6980

Please sign in to comment.