diff --git a/integration-tests/__tests__/location_in_results.test.js b/integration-tests/__tests__/location_in_results.test.js index 2e7b78565ab4..015c5b002531 100644 --- a/integration-tests/__tests__/location_in_results.test.js +++ b/integration-tests/__tests__/location_in_results.test.js @@ -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; @@ -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, + }); }); diff --git a/packages/jest-circus/package.json b/packages/jest-circus/package.json index 0d6be54f053e..7bf9bbbde669 100644 --- a/packages/jest-circus/package.json +++ b/packages/jest-circus/package.json @@ -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" diff --git a/packages/jest-circus/src/legacy_code_todo_rewrite/jest_adapter_init.js b/packages/jest-circus/src/legacy_code_todo_rewrite/jest_adapter_init.js index d2f8b6b3c1d6..92574b1503db 100644 --- a/packages/jest-circus/src/legacy_code_todo_rewrite/jest_adapter_init.js +++ b/packages/jest-circus/src/legacy_code_todo_rewrite/jest_adapter_init.js @@ -102,7 +102,7 @@ export const runAndTransformResultsToJestFormat = async ({ globalConfig: GlobalConfig, testPath: string, }): Promise => { - const runResult = await run(); + const runResult = await run(config); let numFailingTests = 0; let numPassingTests = 0; @@ -131,6 +131,7 @@ 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], @@ -138,7 +139,6 @@ export const runAndTransformResultsToJestFormat = async ({ }); let failureMessage = formatResultsErrors( - // $FlowFixMe Types are slightly incompatible and need to be refactored assertionResults, config, globalConfig, diff --git a/packages/jest-circus/src/run.js b/packages/jest-circus/src/run.js index b929757c43de..cf1aa5d2e236 100644 --- a/packages/jest-circus/src/run.js +++ b/packages/jest-circus/src/run.js @@ -7,6 +7,8 @@ * @flow strict-local */ +import type {ProjectConfig} from 'types/Config'; + import type { RunResult, TestEntry, @@ -28,7 +30,7 @@ import { const Promise = getOriginalPromise(); -const run = async (): Promise => { +const run = async (config: ProjectConfig): Promise => { const {rootDescribeBlock} = getState(); dispatch({name: 'run_start'}); await _runTestsForDescribeBlock(rootDescribeBlock); @@ -36,6 +38,7 @@ const run = async (): Promise => { return makeRunResult( getState().rootDescribeBlock, getState().unhandledErrors, + config, ); }; diff --git a/packages/jest-circus/src/utils.js b/packages/jest-circus/src/utils.js index ecc0dae4de2b..a21007e28f8a 100644 --- a/packages/jest-circus/src/utils.js +++ b/packages/jest-circus/src/utils.js @@ -7,6 +7,7 @@ * @flow strict-local */ +import type {ProjectConfig} from 'types/Config'; import type { AsyncFn, BlockMode, @@ -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 @@ -222,14 +225,16 @@ export const getTestDuration = (test: TestEntry): ?number => { export const makeRunResult = ( describeBlock: DescribeBlock, unhandledErrors: Array, + 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 = []; @@ -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; diff --git a/scripts/SkipOnJestCircus.js b/scripts/SkipOnJestCircus.js index b4460d38dfb9..487e171a9e96 100644 --- a/scripts/SkipOnJestCircus.js +++ b/scripts/SkipOnJestCircus.js @@ -10,6 +10,10 @@ /* 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', () => { @@ -17,14 +21,6 @@ const SkipOnJestCircus = { }); } }, - - test() { - if (process.env.JEST_CIRCUS === '1') { - console.warn('[SKIP] Does not work on jest-circus'); - return true; - } - return false; - }, }; module.exports = SkipOnJestCircus; diff --git a/types/Circus.js b/types/Circus.js index f652c88a7251..400bb289084d 100644 --- a/types/Circus.js +++ b/types/Circus.js @@ -137,6 +137,7 @@ export type TestResult = {| duration: ?number, errors: Array, status: TestStatus, + location: ?{|column: number, line: number|}, testPath: Array, |};