diff --git a/CHANGELOG.md b/CHANGELOG.md index 60010fe140fc..0e88eb8eb88b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ - `[jest-snapshot]` Display change counts in annotation lines ([#8982](https://github.com/facebook/jest/pull/8982)) - `[@jest/test-result]` Create method to create empty `TestResult` ([#8867](https://github.com/facebook/jest/pull/8867)) - `[jest-worker]` [**BREAKING**] Return a promise from `end()`, resolving with the information whether workers exited gracefully ([#8206](https://github.com/facebook/jest/pull/8206)) +- `[jest-reporters]` Transform file paths into hyperlinks ([#8980](https://github.com/facebook/jest/pull/8980)) ### Fixes diff --git a/packages/jest-reporters/package.json b/packages/jest-reporters/package.json index 26e2478139ab..e01aa16e66f3 100644 --- a/packages/jest-reporters/package.json +++ b/packages/jest-reporters/package.json @@ -25,7 +25,8 @@ "jest-worker": "^24.6.0", "slash": "^3.0.0", "source-map": "^0.6.0", - "string-length": "^3.1.0" + "string-length": "^3.1.0", + "terminal-link": "^2.0.0" }, "devDependencies": { "@types/exit": "^0.1.30", diff --git a/packages/jest-reporters/src/__tests__/get_result_header.test.js b/packages/jest-reporters/src/__tests__/get_result_header.test.js new file mode 100644 index 000000000000..3c0c3351ae3e --- /dev/null +++ b/packages/jest-reporters/src/__tests__/get_result_header.test.js @@ -0,0 +1,38 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +import {makeGlobalConfig} from '../../../../TestUtils'; +import getResultHeader from '../get_result_header'; +const terminalLink = require('terminal-link'); + +jest.mock('terminal-link', () => jest.fn(() => 'wannabehyperlink')); + +const testResult = { + testFilePath: '/foo', +}; + +const globalConfig = makeGlobalConfig(); + +beforeEach(() => { + terminalLink.mockClear(); +}); + +test('should call `terminal-link` correctly', () => { + getResultHeader(testResult, globalConfig); + + expect(terminalLink).toBeCalledWith( + expect.stringContaining('foo'), + 'file:///foo', + expect.objectContaining({fallback: expect.any(Function)}), + ); +}); + +test('should render the terminal link', () => { + const result = getResultHeader(testResult, globalConfig); + + expect(result).toContain('wannabehyperlink'); +}); diff --git a/packages/jest-reporters/src/get_result_header.ts b/packages/jest-reporters/src/get_result_header.ts index 4c80809cdce6..81292fa3e518 100644 --- a/packages/jest-reporters/src/get_result_header.ts +++ b/packages/jest-reporters/src/get_result_header.ts @@ -9,6 +9,7 @@ import {Config} from '@jest/types'; import {TestResult} from '@jest/test-result'; import chalk from 'chalk'; import {formatTestPath, printDisplayName} from './utils'; +import terminalLink = require('terminal-link'); const LONG_TEST_COLOR = chalk.reset.bold.bgRed; // Explicitly reset for these messages since they can get written out in the @@ -30,6 +31,13 @@ export default ( projectConfig?: Config.ProjectConfig, ) => { const testPath = result.testFilePath; + const formattedTestPath = formatTestPath( + projectConfig ? projectConfig : globalConfig, + testPath, + ); + const fileLink = terminalLink(formattedTestPath, `file://${testPath}`, { + fallback: () => formattedTestPath, + }); const status = result.numFailingTests > 0 || result.testExecError ? FAIL : PASS; @@ -53,9 +61,7 @@ export default ( : ''; return ( - `${status} ${projectDisplayName}${formatTestPath( - projectConfig ? projectConfig : globalConfig, - testPath, - )}` + (testDetail.length ? ` (${testDetail.join(', ')})` : '') + `${status} ${projectDisplayName}${fileLink}` + + (testDetail.length ? ` (${testDetail.join(', ')})` : '') ); }; diff --git a/yarn.lock b/yarn.lock index db12aa6d64c8..9376627c95bb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -13130,6 +13130,14 @@ supports-color@^7.0.0: dependencies: has-flag "^4.0.0" +supports-hyperlinks@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/supports-hyperlinks/-/supports-hyperlinks-2.0.0.tgz#b1b94a159e9df00b0a554b2d5f0e0a89690334b0" + integrity sha512-bFhn0MQ8qefLyJ3K7PpHiPUTuTVPWw6RXfaMeV6xgJLXtBbszyboz1bvGTVv4R0YpQm2DqlXXn0fFHhxUHVE5w== + dependencies: + has-flag "^4.0.0" + supports-color "^7.0.0" + svgo@^1.0.0, svgo@^1.0.5: version "1.3.0" resolved "https://registry.yarnpkg.com/svgo/-/svgo-1.3.0.tgz#bae51ba95ded9a33a36b7c46ce9c359ae9154313" @@ -13245,6 +13253,14 @@ tempfile@^2.0.0: temp-dir "^1.0.0" uuid "^3.0.1" +terminal-link@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/terminal-link/-/terminal-link-2.0.0.tgz#daa5d9893d57d3a09f981e1a45be37daba3f0ce6" + integrity sha512-rdBAY35jUvVapqCuhehjenLbYY73cVgRQ6podD6u9EDBomBBHjCOtmq2InPgPpTysOIOsQ5PdBzwSC/sKjv6ew== + dependencies: + ansi-escapes "^4.2.1" + supports-hyperlinks "^2.0.0" + terser-webpack-plugin@^1.4.1: version "1.4.1" resolved "https://registry.yarnpkg.com/terser-webpack-plugin/-/terser-webpack-plugin-1.4.1.tgz#61b18e40eaee5be97e771cdbb10ed1280888c2b4"