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

Allow test titles to include array index #6414

Merged
merged 9 commits into from
Aug 8, 2018
Merged
Show file tree
Hide file tree
Changes from 7 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## master

### Features

- `[jest-each]` introduces `%#` option to add index of the test to its title

### Performance

- `[jest-changed-files]` limit git and hg commands to specified roots ([#6732](https://github.com/facebook/jest/pull/6732))
Expand Down
2 changes: 2 additions & 0 deletions docs/GlobalAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ Use `describe.each` if you keep duplicating the same test suites with different
- `%f` - Floating point value.
- `%j` - JSON.
- `%o` - Object.
- `%#` - Index of the test case.
- `%%` - single percent sign ('%'). This does not consume an argument.
- `fn`: `Function` the suite of tests to be ran, this is the function that will receive the parameters in each row as function arguments.

Expand Down Expand Up @@ -488,6 +489,7 @@ Use `test.each` if you keep duplicating the same test with different data. `test
- `%f` - Floating point value.
- `%j` - JSON.
- `%o` - Object.
- `%#` - Index of the test case.
- `%%` - single percent sign ('%'). This does not consume an argument.
- `fn`: `Function` the test to be ran, this is the function that will receive the parameters in each row as function arguments.

Expand Down
3 changes: 3 additions & 0 deletions packages/jest-each/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ jest-each allows you to provide multiple arguments to your `test`/`describe` whi
- `%f` - Floating point value.
- `%j` - JSON.
- `%o` - Object.
- `%#` - Index of the test case.
- `%%` - single percent sign ('%'). This does not consume an argument.
- 🖖 Spock like data tables with [Tagged Template Literals](#tagged-template-literal-of-rows)

Expand Down Expand Up @@ -109,6 +110,7 @@ const each = require('jest-each');
- `%f` - Floating point value.
- `%j` - JSON.
- `%o` - Object.
- `%#` - Index of the test case.
- `%%` - single percent sign ('%'). This does not consume an argument.
- testFn: `Function` the test logic, this is the function that will receive the parameters of each row as function arguments

Expand All @@ -130,6 +132,7 @@ const each = require('jest-each');
- `%f` - Floating point value.
- `%j` - JSON.
- `%o` - Object.
- `%#` - Index of the test case.
- `%%` - single percent sign ('%'). This does not consume an argument.
- suiteFn: `Function` the suite of `test`/`it`s to be ran, this is the function that will receive the parameters in each row as function arguments

Expand Down
6 changes: 3 additions & 3 deletions packages/jest-each/src/__tests__/array.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,20 +102,20 @@ describe('jest-each', () => {
],
]);
const testFunction = get(eachObject, keyPath);
testFunction('expected string: %s %d %s %s %d %j %s %j %d %d', noop);
testFunction('expected string: %s %d %s %s %d %j %s %j %d %d %#', noop);

const globalMock = get(globalTestMocks, keyPath);
expect(globalMock).toHaveBeenCalledTimes(2);
expect(globalMock).toHaveBeenCalledWith(
`expected string: hello 1 null undefined 1.2 ${JSON.stringify({
foo: 'bar',
})} () => {} [] Infinity NaN`,
})} () => {} [] Infinity NaN 0`,
expectFunction,
);
expect(globalMock).toHaveBeenCalledWith(
`expected string: world 1 null undefined 1.2 ${JSON.stringify({
baz: 'qux',
})} () => {} [] Infinity NaN`,
})} () => {} [] Infinity NaN 1`,
expectFunction,
);
});
Expand Down
9 changes: 5 additions & 4 deletions packages/jest-each/src/bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ const EXPECTED_COLOR = chalk.green;
const RECEIVED_COLOR = chalk.red;
const SUPPORTED_PLACEHOLDERS = /%[sdifjoOp%]/g;
const PRETTY_PLACEHOLDER = '%p';
const INDEX_PLACEHOLDER = '%#';

export default (cb: Function) => (...args: any) =>
function eachBind(title: string, test: Function): void {
if (args.length === 1) {
const table: Table = args[0].every(Array.isArray)
? args[0]
: args[0].map(entry => [entry]);
return table.forEach(row =>
cb(arrayFormat(title, ...row), applyRestParams(row, test)),
return table.forEach((row, i) =>
cb(arrayFormat(title, i, ...row), applyRestParams(row, test)),
);
}

Expand Down Expand Up @@ -76,7 +77,7 @@ const getPrettyIndexes = placeholders =>
[],
);

const arrayFormat = (title, ...args) => {
const arrayFormat = (title, rowIndex, ...args) => {
const placeholders = title.match(SUPPORTED_PLACEHOLDERS) || [];
const prettyIndexes = getPrettyIndexes(placeholders);

Expand All @@ -101,7 +102,7 @@ const arrayFormat = (title, ...args) => {
);

return util.format(
prettyTitle,
prettyTitle.replace(INDEX_PLACEHOLDER, rowIndex.toString()),
...remainingArgs.slice(0, placeholders.length - prettyIndexes.length),
);
};
Expand Down