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

Support index in it.each with template strings #10763

Merged
merged 4 commits into from Apr 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion docs/GlobalAPI.md
Expand Up @@ -273,7 +273,7 @@ describe.each([
- `table`: `Tagged Template Literal`
- First row of variable name column headings separated with `|`
- One or more subsequent rows of data supplied as template literal expressions using `${value}` syntax.
- `name`: `String` the title of the test suite, use `$variable` to inject test data into the suite title from the tagged template expressions.
- `name`: `String` the title of the test suite, use `$variable` to inject test data into the suite title from the tagged template expressions, and `$#` for the index of the row.
- To inject nested object values use you can supply a keyPath i.e. `$variable.path.to.value`
- `fn`: `Function` the suite of tests to be ran, this is the function that will receive the test data object.
- Optionally, you can provide a `timeout` (in milliseconds) for specifying how long to wait for each row before aborting. _Note: The default timeout is 5 seconds._
Expand Down
1 change: 1 addition & 0 deletions packages/jest-each/README.md
Expand Up @@ -338,6 +338,7 @@ each`

- name: `String` the title of the `test`, use `$variable` in the name string to inject test values into the test title from the tagged template expressions
- To inject nested object values use you can supply a keyPath i.e. `$variable.path.to.value`
- You can use `$#` to inject the index of the table row.
- testFn: `Function` the test logic, this is the function that will receive the parameters of each row as function arguments

#### `each[tagged template].describe(name, suiteFn)`
Expand Down
24 changes: 15 additions & 9 deletions packages/jest-each/src/__tests__/template.test.ts
Expand Up @@ -171,17 +171,20 @@ describe('jest-each', () => {
${1} | ${1} | ${2}
`;
const testFunction = get(eachObject, keyPath);
testFunction('expected string: a=$a, b=$b, expected=$expected', noop);
testFunction(
'expected string: a=$a, b=$b, expected=$expected index=$#',
noop,
);

const globalMock = get(globalTestMocks, keyPath);
expect(globalMock).toHaveBeenCalledTimes(2);
expect(globalMock).toHaveBeenCalledWith(
'expected string: a=0, b=1, expected=1',
'expected string: a=0, b=1, expected=1 index=0',
expectFunction,
undefined,
);
expect(globalMock).toHaveBeenCalledWith(
'expected string: a=1, b=1, expected=2',
'expected string: a=1, b=1, expected=2 index=1',
expectFunction,
undefined,
);
Expand All @@ -196,19 +199,19 @@ describe('jest-each', () => {
`;
const testFunction = get(eachObject, keyPath);
testFunction(
'add($a, $b) expected string: a=$a, b=$b, expected=$expected',
'add($a, $b) expected string: a=$a, b=$b, expected=$expected index=$#',
noop,
);

const globalMock = get(globalTestMocks, keyPath);
expect(globalMock).toHaveBeenCalledTimes(2);
expect(globalMock).toHaveBeenCalledWith(
'add(0, 1) expected string: a=0, b=1, expected=1',
'add(0, 1) expected string: a=0, b=1, expected=1 index=0',
expectFunction,
undefined,
);
expect(globalMock).toHaveBeenCalledWith(
'add(1, 1) expected string: a=1, b=1, expected=2',
'add(1, 1) expected string: a=1, b=1, expected=2 index=1',
expectFunction,
undefined,
);
Expand Down Expand Up @@ -426,17 +429,20 @@ describe('jest-each', () => {
${1} | ${1} | ${2}
`;
const testFunction = get(eachObject, keyPath);
testFunction('expected string: a=$a, b=$b, expected=$expected', noop);
testFunction(
'expected string: a=$a, b=$b, expected=$expected index=$#',
noop,
);

const globalMock = get(globalTestMocks, keyPath);
expect(globalMock).toHaveBeenCalledTimes(2);
expect(globalMock).toHaveBeenCalledWith(
'expected string: a=0, b=1, expected=1',
'expected string: a=0, b=1, expected=1 index=0',
expectFunction,
undefined,
);
expect(globalMock).toHaveBeenCalledWith(
'expected string: a=1, b=1, expected=2',
'expected string: a=1, b=1, expected=2 index=1',
expectFunction,
undefined,
);
Expand Down
9 changes: 5 additions & 4 deletions packages/jest-each/src/table/template.ts
Expand Up @@ -22,9 +22,9 @@ export default (
): EachTests => {
const table = convertRowToTable(row, headings);
const templates = convertTableToTemplates(table, headings);
return templates.map(template => ({
return templates.map((template, index) => ({
arguments: [template],
title: interpolate(title, template),
title: interpolate(title, template, index),
}));
};

Expand All @@ -47,10 +47,11 @@ const convertTableToTemplates = (
),
);

const interpolate = (title: string, template: Template) =>
const interpolate = (title: string, template: Template, index: number) =>
Object.keys(template)
.reduce(getMatchingKeyPaths(title), []) // aka flatMap
.reduce(replaceKeyPathWithValue(template), title);
.reduce(replaceKeyPathWithValue(template), title)
.replace('$#', '' + index);

const getMatchingKeyPaths = (title: string) => (
matches: Headings,
Expand Down