Skip to content

Commit

Permalink
Introduce %$ option to add number of the test to its title
Browse files Browse the repository at this point in the history
  • Loading branch information
kemuridama committed Nov 22, 2023
1 parent 2c3d240 commit 06209ca
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
6 changes: 3 additions & 3 deletions packages/jest-each/src/__tests__/array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ describe('jest-each', () => {
]);
const testFunction = get(eachObject, keyPath);
testFunction(
'expected string: %% %%s %s %d %s %s %d %j %s %j %d %d %#',
'expected string: %% %%s %s %d %s %s %d %j %s %j %d %d %# %$',
noop,
);

Expand All @@ -153,14 +153,14 @@ describe('jest-each', () => {
expect(globalMock).toHaveBeenCalledWith(
`expected string: % %s hello 1 null undefined 1.2 ${JSON.stringify({
foo: 'bar',
})} () => {} [] Infinity NaN 0`,
})} () => {} [] Infinity NaN 0 1`,
expectFunction,
undefined,
);
expect(globalMock).toHaveBeenCalledWith(
`expected string: % %s world 1 null undefined 1.2 ${JSON.stringify({
baz: 'qux',
})} () => {} [] Infinity NaN 1`,
})} () => {} [] Infinity NaN 1 2`,
expectFunction,
undefined,
);
Expand Down
12 changes: 9 additions & 3 deletions packages/jest-each/src/table/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {type Templates, interpolateVariables} from './interpolation';
const SUPPORTED_PLACEHOLDERS = /%[sdifjoOp#]/g;
const PRETTY_PLACEHOLDER = '%p';
const INDEX_PLACEHOLDER = '%#';
const NUMBER_PLACEHOLDER = '%$';
const PLACEHOLDER_PREFIX = '%';
const ESCAPED_PLACEHOLDER_PREFIX = /%%/g;
const JEST_EACH_PLACEHOLDER_ESCAPE = '@@__JEST_EACH_PLACEHOLDER_ESCAPE__@@';
Expand Down Expand Up @@ -71,7 +72,10 @@ const formatTitle = (

return util.format(formattedTitle, normalisedValue);
},
interpolateTitleIndex(interpolateEscapedPlaceholders(title), rowIndex),
interpolateTitleIndexAndNumber(
interpolateEscapedPlaceholders(title),
rowIndex,
),
)
.replace(new RegExp(JEST_EACH_PLACEHOLDER_ESCAPE, 'g'), PLACEHOLDER_PREFIX);

Expand All @@ -89,8 +93,10 @@ const getMatchingPlaceholders = (title: string) =>
const interpolateEscapedPlaceholders = (title: string) =>
title.replace(ESCAPED_PLACEHOLDER_PREFIX, JEST_EACH_PLACEHOLDER_ESCAPE);

const interpolateTitleIndex = (title: string, index: number) =>
title.replace(INDEX_PLACEHOLDER, index.toString());
const interpolateTitleIndexAndNumber = (title: string, index: number) =>
title
.replace(INDEX_PLACEHOLDER, index.toString())
.replace(NUMBER_PLACEHOLDER, (index + 1).toString());

const interpolatePrettyPlaceholder = (title: string, value: unknown) =>
title.replace(PRETTY_PLACEHOLDER, pretty(value, {maxDepth: 1, min: true}));

0 comments on commit 06209ca

Please # to comment.