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

Fix issue with overly trimmed basenames when formatting test paths #33

Merged
merged 1 commit into from
Nov 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/file_name_plugin/__tests__/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,6 @@ it("selected file doesn't include trimming dots", async () => {

expect(updateConfigAndRun).toHaveBeenCalledWith({
mode: 'watch',
testPathPattern: 'ing\\.js',
testPathPattern: 'ong_name_gonna_need_trimming\\.js',
});
});
8 changes: 8 additions & 0 deletions src/lib/__tests__/__snapshots__/utils.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,11 @@ exports[`formatTestNameByPattern formats when testname="the test name", pattern=
exports[`formatTestNameByPattern formats when testname="the test name", pattern="the", and width="25" 1`] = `"</>the</><dim> test name</>"`;

exports[`formatTestNameByPattern formats when testname="the test name", pattern="the", and width="30" 1`] = `"</>the</><dim> test name</>"`;

exports[`trimAndFormatPath formats when testpath="/project/src/exactly/sep_and_basename.js", pad="6", and columns="29" 1`] = `"<dim>.../</><bold>sep_and_basename.js</>"`;

exports[`trimAndFormatPath formats when testpath="/project/src/gonna/fit/all.js", pad="6", and columns="80" 1`] = `"<dim>src/gonna/fit/</><bold>all.js</>"`;

exports[`trimAndFormatPath formats when testpath="/project/src/long_name_gonna_need_trimming.js", pad="6", and columns="40" 1`] = `"<bold>...ong_name_gonna_need_trimming.js</>"`;

exports[`trimAndFormatPath formats when testpath="/project/src/trimmed_dir/foo.js", pad="6", and columns="20" 1`] = `"<dim>..._dir/</><bold>foo.js</>"`;
22 changes: 15 additions & 7 deletions src/lib/__tests__/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import stripAnsi from 'strip-ansi';
import { trimAndFormatPath, formatTestNameByPattern } from '../utils';

jest.mock('chalk', () => {
const chalk = jest.requireActual('chalk');
return new chalk.constructor({ enabled: true, level: 1 });
});

test('trimAndFormatPath', () => {
expect(
stripAnsi(
trimAndFormatPath(2, { cwd: '/hello/there' }, '/hello/there/to/you', 80),
),
).toEqual('to/you');
describe('trimAndFormatPath', () => {
test.each`
testPath | pad | columns
${'/project/src/gonna/fit/all.js'} | ${6} | ${80}
${'/project/src/trimmed_dir/foo.js'} | ${6} | ${20}
${'/project/src/exactly/sep_and_basename.js'} | ${6} | ${29}
${'/project/src/long_name_gonna_need_trimming.js'} | ${6} | ${40}
`(
'formats when testpath="$testPath", pad="$pad", and columns="$columns"',
({ testPath, pad, columns }) => {
expect(
trimAndFormatPath(pad, { rootDir: '/project' }, testPath, columns),
).toMatchSnapshot();
},
);
});

describe('formatTestNameByPattern', () => {
Expand Down
9 changes: 1 addition & 8 deletions src/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,7 @@ export const trimAndFormatPath = (
);
}
// can't fit dirname, but can fit trimmed basename
return slash(
chalk.bold(
`${TRIMMING_DOTS}${basename.slice(
basename.length - maxLength - 4,
basename.length,
)}`,
),
);
return slash(chalk.bold(`${TRIMMING_DOTS}${basename.slice(-maxLength + 3)}`));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

While working on my previous PR I've noticed this issue. It basically was caused by the fact that maxLength was bigger than basename.length so a negative starting index has been calculated. We don't need to provide both start + end indexes as we always want to slice "till the end" - calculating just a starting index as a negative one simplifies things.

};

export const getTerminalWidth = (
Expand Down