Skip to content
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
Next Next commit
Add support for %p pretty format placeholder
  • Loading branch information
mattphillips committed May 30, 2018
commit a9a8d4365914f65a61f2ed0655426488dad1622f
19 changes: 19 additions & 0 deletions e2e/__tests__/__snapshots__/each.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`formats args with pretty format when given %p 1`] = `
"PASS __tests__/pretty.test.js
✓ \\"hello\\" == \\"hello\\"
✓ 1 == 1
✓ null == null
✓ undefined == undefined
✓ 1.2 == 1.2
✓ {\\"foo\\": \\"bar\\"} == {\\"foo\\": \\"bar\\"}
✓ {\\"foo\\": [Object]} == {\\"foo\\": [Object]}
✓ [Function noop] == [Function noop]
✓ [] == []
✓ [[Object]] == [[Object]]
✓ Infinity == Infinity
✓ -Infinity == -Infinity
✓ NaN == NaN

"
`;

exports[`runs only the describe.only.each tests 1`] = `
"PASS __tests__/describe-only.test.js
passes all rows expected true == true
Expand Down
7 changes: 7 additions & 0 deletions e2e/__tests__/each.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,10 @@ test('runs only the describe.only.each tests', () => {
expect(rest).toMatchSnapshot();
expect(result.status).toBe(0);
});

test('formats args with pretty format when given %p', () => {
const result = runJest(dir, ['pretty.test.js']);
const {rest} = extractSummary(result.stderr);
expect(rest).toMatchSnapshot();
expect(result.status).toBe(0);
});
26 changes: 26 additions & 0 deletions e2e/each/__tests__/pretty.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright (c) 2018-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

const noop = () => {};

it.each([
['hello', 'hello'],
[1, 1],
[null, null],
[undefined, undefined],
[1.2, 1.2],
[{foo: 'bar'}, {foo: 'bar'}],
[{foo: {bar: 'baz'}}, {foo: {bar: 'baz'}}],
[noop, noop],
[[], []],
[[{foo: {bar: 'baz'}}], [{foo: {bar: 'baz'}}]],
[Infinity, Infinity],
[-Infinity, -Infinity],
[NaN, NaN],
])('%p == %p', (left, right) => {
expect(left).toEqual(right);
});
47 changes: 47 additions & 0 deletions packages/jest-each/src/__tests__/array.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*
*/

import pretty from 'pretty-format';
import each from '../';

const noop = () => {};
Expand Down Expand Up @@ -140,6 +141,52 @@ describe('jest-each', () => {
);
});

test('calls global test title with %p placeholder injected at the correct positions', () => {
const globalTestMocks = getGlobalTestMocks();
const eachObject = each.withGlobal(globalTestMocks)([
['string1', 'pretty1', 'string2', 'pretty2'],
['string1', 'pretty1', 'string2', 'pretty2'],
]);
const testFunction = get(eachObject, keyPath);
testFunction('expected string: %s %p %s %p', noop);

const globalMock = get(globalTestMocks, keyPath);
expect(globalMock).toHaveBeenCalledTimes(2);
expect(globalMock).toHaveBeenCalledWith(
`expected string: string1 ${pretty('pretty1')} string2 ${pretty(
'pretty2',
)}`,
expectFunction,
);
expect(globalMock).toHaveBeenCalledWith(
`expected string: string1 ${pretty('pretty1')} string2 ${pretty(
'pretty2',
)}`,
expectFunction,
);
});

test('does not calls global test title with %p placeholder when no data is supplied at given position', () => {
const globalTestMocks = getGlobalTestMocks();
const eachObject = each.withGlobal(globalTestMocks)([
['string1', 'pretty1', 'string2'],
['string1', 'pretty1', 'string2'],
]);
const testFunction = get(eachObject, keyPath);
testFunction('expected string: %s %p %s %p', noop);

const globalMock = get(globalTestMocks, keyPath);
expect(globalMock).toHaveBeenCalledTimes(2);
expect(globalMock).toHaveBeenCalledWith(
`expected string: string1 ${pretty('pretty1')} string2 %p`,
expectFunction,
);
expect(globalMock).toHaveBeenCalledWith(
`expected string: string1 ${pretty('pretty1')} string2 %p`,
expectFunction,
);
});

test('calls global with cb function containing all parameters of each test case when given 1d array', () => {
const globalTestMocks = getGlobalTestMocks();
const testCallBack = jest.fn();
Expand Down
45 changes: 41 additions & 4 deletions packages/jest-each/src/bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ import chalk from 'chalk';
import pretty from 'pretty-format';

type Table = Array<Array<any>>;
type PrettyArgs = {
args: Array<mixed>,
title: string,
};

const EXPECTED_COLOR = chalk.green;
const RECEIVED_COLOR = chalk.red;
const SUPPORTED_PLACEHOLDERS = /%[sdifjoO%]/g;
const SUPPORTED_PLACEHOLDERS = /%[sdifjoOp%]/g;
const PRETTY_PLACEHOLDER = '%p';

export default (cb: Function) => (...args: any) =>
function eachBind(title: string, test: Function): void {
Expand Down Expand Up @@ -59,9 +64,41 @@ export default (cb: Function) => (...args: any) =>
);
};

const arrayFormat = (str, ...args) => {
const matches = (str.match(SUPPORTED_PLACEHOLDERS) || []).length;
return util.format(str, ...args.slice(0, matches));
const getPrettyIndexes = placeholders =>
placeholders.reduce(
(indexes, placeholder, index) =>
placeholder === PRETTY_PLACEHOLDER ? indexes.concat(index) : indexes,
[],
);

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

const {title: prettyTitle, args: remainingArgs} = args.reduce(
(acc: PrettyArgs, arg, index) => {
if (prettyIndexes.indexOf(index) !== -1) {
return {
args: acc.args,
title: acc.title.replace(
PRETTY_PLACEHOLDER,
pretty(arg, {maxDepth: 1, min: true}),
),
};
}

return {
args: acc.args.concat([arg]),
title: acc.title,
};
},
{args: [], title},
);

return util.format(
prettyTitle,
...remainingArgs.slice(0, placeholders.length - prettyIndexes.length),
);
};

const applyRestParams = (params: Array<any>, test: Function) => {
Expand Down