Skip to content

Commit

Permalink
Fix issue with overly trimmed basenames when formatting test paths (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist authored and SimenB committed Nov 10, 2019
1 parent aa70d5d commit d1e8cfc
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
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)}`));
};

export const getTerminalWidth = (
Expand Down

0 comments on commit d1e8cfc

Please sign in to comment.