Skip to content

Commit 96a3d38

Browse files
mattphillipscaptain-yossarian
authored andcommitted
[jest-each] Add primitive pretty printing (jestjs#7694)
1 parent 4ac0408 commit 96a3d38

File tree

6 files changed

+44
-3
lines changed

6 files changed

+44
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
### Features
44

5+
- `[jest-each]` [**BREAKING**] Add primitive pretty printing for interpolated titles ([#7694](https://github.com/facebook/jest/pull/7694))
56
- `[jest-runtime]` Add `jest.isolateModules` for scoped module initialization ([#6701](https://github.com/facebook/jest/pull/6701))
67
- `[jest-diff]` [**BREAKING**] Support diffing numbers and booleans instead of returning null for different ones ([#7605](https://github.com/facebook/jest/pull/7605))
78
- `[jest-diff]` [**BREAKING**] Replace `diff` with `diff-sequences` package ([#6961](https://github.com/facebook/jest/pull/6961))

e2e/__tests__/__snapshots__/each.test.js.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ PASS __tests__/pretty.test.js
1717
✓ -Infinity == -Infinity
1818
✓ NaN == NaN
1919
template
20-
"hello" == "hello"
20+
✓ hello == hello
2121
✓ 1 == 1
2222
✓ null == null
2323
✓ undefined == undefined

e2e/each/__tests__/failure.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ describe.each`
5353
${'a'} | ${'b'}
5454
${'c'} | ${'d'}
5555
`(
56-
'template table describe fails on all rows expected $left == $right',
56+
'template table describe fails on all rows expected "$left" == "$right"',
5757
({left, right}) => {
5858
it('fails ', () => {
5959
expect(left).toBe(right);

packages/jest-each/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"license": "MIT",
1919
"dependencies": {
2020
"chalk": "^2.0.1",
21+
"jest-get-type": "^22.1.0",
2122
"jest-util": "^23.4.0",
2223
"pretty-format": "^23.6.0"
2324
},

packages/jest-each/src/__tests__/template.test.js

+27-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ describe('jest-each', () => {
220220
const globalMock = get(globalTestMocks, keyPath);
221221
expect(globalMock).toHaveBeenCalledTimes(1);
222222
expect(globalMock).toHaveBeenCalledWith(
223-
'interpolates object keyPath to value: "baz"',
223+
'interpolates object keyPath to value: baz',
224224
expectFunction,
225225
undefined,
226226
);
@@ -282,6 +282,32 @@ describe('jest-each', () => {
282282
10000,
283283
);
284284
});
285+
286+
test('formats primitive values using .toString()', () => {
287+
const globalTestMocks = getGlobalTestMocks();
288+
const number = 1;
289+
const string = 'hello';
290+
const boolean = true;
291+
const symbol = Symbol('world');
292+
const nullValue = null;
293+
const undefinedValue = undefined;
294+
const eachObject = each.withGlobal(globalTestMocks)`
295+
number | string | boolean | symbol | nullValue | undefinedValue
296+
${number} | ${string} | ${boolean} | ${symbol} | ${nullValue} | ${undefinedValue}
297+
`;
298+
299+
const testFunction = get(eachObject, keyPath);
300+
testFunction(
301+
'number: $number | string: $string | boolean: $boolean | symbol: $symbol | null: $nullValue | undefined: $undefinedValue',
302+
noop,
303+
);
304+
const globalMock = get(globalTestMocks, keyPath);
305+
expect(globalMock).toHaveBeenCalledWith(
306+
'number: 1 | string: hello | boolean: true | symbol: Symbol(world) | null: null | undefined: undefined',
307+
expect.any(Function),
308+
undefined,
309+
);
310+
});
285311
});
286312
});
287313

packages/jest-each/src/bind.js

+13
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import util from 'util';
1111
import chalk from 'chalk';
1212
import pretty from 'pretty-format';
13+
import getType from 'jest-get-type';
1314
import {ErrorWithStack} from 'jest-util';
1415

1516
type Table = Array<Array<any>>;
@@ -23,6 +24,13 @@ const RECEIVED_COLOR = chalk.red;
2324
const SUPPORTED_PLACEHOLDERS = /%[sdifjoOp%]/g;
2425
const PRETTY_PLACEHOLDER = '%p';
2526
const INDEX_PLACEHOLDER = '%#';
27+
const PRIMITIVES = new Set([
28+
'string',
29+
'number',
30+
'boolean',
31+
'null',
32+
'undefined',
33+
]);
2634

2735
export default (cb: Function, supportsDone: boolean = true) => (...args: any) =>
2836
function eachBind(title: string, test: Function, timeout: number): void {
@@ -195,6 +203,11 @@ const getMatchingKeyPaths = title => (matches, key) =>
195203
const replaceKeyPathWithValue = data => (title, match) => {
196204
const keyPath = match.replace('$', '').split('.');
197205
const value = getPath(data, keyPath);
206+
const valueType = getType(value);
207+
208+
if (PRIMITIVES.has(valueType)) {
209+
return title.replace(match, value);
210+
}
198211
return title.replace(match, pretty(value, {maxDepth: 1, min: true}));
199212
};
200213

0 commit comments

Comments
 (0)