Skip to content

Commit

Permalink
Add primitive check to pretty printing
Browse files Browse the repository at this point in the history
  • Loading branch information
mattphillips committed Jan 24, 2019
1 parent 22a520d commit a75ec9c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
28 changes: 27 additions & 1 deletion packages/jest-each/src/__tests__/template.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ describe('jest-each', () => {
const globalMock = get(globalTestMocks, keyPath);
expect(globalMock).toHaveBeenCalledTimes(1);
expect(globalMock).toHaveBeenCalledWith(
'interpolates object keyPath to value: "baz"',
'interpolates object keyPath to value: baz',
expectFunction,
undefined,
);
Expand Down Expand Up @@ -282,6 +282,32 @@ describe('jest-each', () => {
10000,
);
});

test('formats primitive values using .toString()', () => {
const globalTestMocks = getGlobalTestMocks();
const number = 1;
const string = 'hello';
const boolean = true;
const symbol = Symbol('world');
const nullValue = null;
const undefinedValue = undefined;
const eachObject = each.withGlobal(globalTestMocks)`
number | string | boolean | symbol | nullValue | undefinedValue
${number} | ${string} | ${boolean} | ${symbol} | ${nullValue} | ${undefinedValue}
`;

const testFunction = get(eachObject, keyPath);
testFunction(
'number: $number | string: $string | boolean: $boolean | symbol: $symbol | null: $nullValue | undefined: $undefinedValue',
noop,
);
const globalMock = get(globalTestMocks, keyPath);
expect(globalMock).toHaveBeenCalledWith(
'number: 1 | string: hello | boolean: true | symbol: Symbol(world) | null: null | undefined: undefined',
expect.any(Function),
undefined,
);
});
});
});

Expand Down
7 changes: 7 additions & 0 deletions packages/jest-each/src/bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import util from 'util';
import chalk from 'chalk';
import pretty from 'pretty-format';
import getType from 'jest-get-type';
import {ErrorWithStack} from 'jest-util';

type Table = Array<Array<any>>;
Expand All @@ -23,6 +24,7 @@ const RECEIVED_COLOR = chalk.red;
const SUPPORTED_PLACEHOLDERS = /%[sdifjoOp%]/g;
const PRETTY_PLACEHOLDER = '%p';
const INDEX_PLACEHOLDER = '%#';
const PRIMITIVES = ['string', 'number', 'boolean', 'null', 'undefined'];

export default (cb: Function, supportsDone: boolean = true) => (...args: any) =>
function eachBind(title: string, test: Function, timeout: number): void {
Expand Down Expand Up @@ -195,6 +197,11 @@ const getMatchingKeyPaths = title => (matches, key) =>
const replaceKeyPathWithValue = data => (title, match) => {
const keyPath = match.replace('$', '').split('.');
const value = getPath(data, keyPath);
const valueType = getType(value);

if (PRIMITIVES.indexOf(valueType) > -1) {
return title.replace(match, value);
}
return title.replace(match, pretty(value, {maxDepth: 1, min: true}));
};

Expand Down

0 comments on commit a75ec9c

Please sign in to comment.