Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: transform file paths into hyperlinks #8980

Merged
merged 7 commits into from Oct 14, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -22,6 +22,7 @@
- `[jest-runner]` Warn if a worker had to be force exited ([#8206](https://github.com/facebook/jest/pull/8206))
- `[@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

Expand Down
3 changes: 2 additions & 1 deletion packages/jest-reporters/package.json
Expand Up @@ -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",
Expand Down
36 changes: 36 additions & 0 deletions packages/jest-reporters/src/__tests__/get_result_header.test.js
@@ -0,0 +1,36 @@
/**
* 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 {formatTestPath} from '../utils';
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();

test('should call `terminal-link` correctly', () => {
terminalLink.mockClear();
lekterable marked this conversation as resolved.
Show resolved Hide resolved

getResultHeader(testResult, globalConfig);
const call = terminalLink.mock.calls[0];

expect(terminalLink).toHaveBeenCalled();
expect(call[0]).toBe(formatTestPath(globalConfig, testResult.testFilePath));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would prefer if we hardcode the expected call argument values here without using formatTestPath/testResult, it kinda reimplements the production code this way and is not easy on the eye.
Also I think toHaveBeenCalledWith simplifies this a lot (4 lines to 1)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tried hardcoding the formatTestPath result by copy-pasting it from the console, but it contains the parts which are coloured by the chalk library and it seems to get lost when copied, any idea how could I keep it if we still want to go hardcode it?
btw. is reimplementing the production code in tests a bad practice? if so, what's exactly wrong with it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could mock chalk like some other tests do so it just becomes something like <green>text</green>. But that's probably too complicated. Maybe just expect.stringContaining('the test path')?
What I mean re reimplementing: The reason we write tests is to run the code with specific example data that reproduces a case simple enough to wrap our heads around. If we could manage the complexity of all cases in all the code at once, we wouldn't need tests but could just look at the production code and see whether it's correct ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense, thanks for the explanation @jeysal ! I've hardcoded the expected values, let me know if that's what you had in mind :)

expect(call[1]).toBe(`file://${testResult.testFilePath}`);
});

test('should render the output correctly', () => {
lekterable marked this conversation as resolved.
Show resolved Hide resolved
const result = getResultHeader(testResult, globalConfig);

expect(result).toMatch(/wannabehyperlink/);
lekterable marked this conversation as resolved.
Show resolved Hide resolved
});
14 changes: 10 additions & 4 deletions packages/jest-reporters/src/get_result_header.ts
Expand Up @@ -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
Expand All @@ -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;

Expand All @@ -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(', ')})` : '')
);
};
16 changes: 16 additions & 0 deletions yarn.lock
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down