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 1 commit
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
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 0`,
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this should be 1 as it’s the second row of test data

Copy link
Author

@mfix22 mfix22 Jun 8, 2018

Choose a reason for hiding this comment

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

🤦‍♂️ of course yeah! 😄

expectFunction,
);
});
Expand Down
10 changes: 5 additions & 5 deletions packages/jest-each/src/bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ 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 {
Expand Down Expand Up @@ -85,16 +86,15 @@ const arrayFormat = (title, ...args) => {
if (prettyIndexes.indexOf(index) !== -1) {
return {
args: acc.args,
title: acc.title.replace(
PRETTY_PLACEHOLDER,
pretty(arg, {maxDepth: 1, min: true}),
),
title: acc.title
.replace(PRETTY_PLACEHOLDER, pretty(arg, {maxDepth: 1, min: true}))
.replace(INDEX_PLACEHOLDER, `${index}`),
Copy link
Contributor

Choose a reason for hiding this comment

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

This index represents the index of each arg in a row of data.

I think you want to use the index of each row in the outer table on line 32.

return table.forEach(row =>
  cb(arrayFormat(title, ...row), applyRestParams(row, test)),
);

};
}

return {
args: acc.args.concat([arg]),
title: acc.title,
title: acc.title.replace(INDEX_PLACEHOLDER, `${index}`),
};
},
{args: [], title},
Expand Down