Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### Features

- `[jest-each]` [**BREAKING**] Add primitive pretty printing for interpolated titles ([#7694](https://github.com/facebook/jest/pull/7694))
- `[jest-runtime]` Add `jest.isolateModules` for scoped module initialization ([#6701](https://github.com/facebook/jest/pull/6701))
- `[jest-diff]` [**BREAKING**] Support diffing numbers and booleans instead of returning null for different ones ([#7605](https://github.com/facebook/jest/pull/7605))
- `[jest-diff]` [**BREAKING**] Replace `diff` with `diff-sequences` package ([#6961](https://github.com/facebook/jest/pull/6961))
Expand Down
10 changes: 5 additions & 5 deletions e2e/__tests__/__snapshots__/each.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ PASS __tests__/pretty.test.js
✓ -Infinity == -Infinity
✓ NaN == NaN
template
"hello" == "hello"
✓ hello == hello
✓ 1 == 1
✓ null == null
✓ undefined == undefined
Expand Down Expand Up @@ -127,9 +127,9 @@ FAIL __tests__/failure.test.js
✕ The word red contains the letter 'z'
✕ The word green contains the letter 'z'
✕ The word bean contains the letter 'z'
template table describe fails on all rows expected "a" == "b"
template table describe fails on all rows expected a == b
✕ fails
template table describe fails on all rows expected "c" == "d"
template table describe fails on all rows expected c == d
✕ fails
array table describe fails on all rows expected a == b
✕ fails
Expand Down Expand Up @@ -289,7 +289,7 @@ FAIL __tests__/failure.test.js

at toBe (__tests__/failure.test.js:47:28)

● template table describe fails on all rows expected "a" == "b" › fails
● template table describe fails on all rows expected a == b › fails

expect(received).toBe(expected) // Object.is equality

Expand All @@ -306,7 +306,7 @@ FAIL __tests__/failure.test.js

at Object.toBe (__tests__/failure.test.js:59:20)

● template table describe fails on all rows expected "c" == "d" › fails
● template table describe fails on all rows expected c == d › fails

expect(received).toBe(expected) // Object.is equality

Expand Down
1 change: 1 addition & 0 deletions packages/jest-each/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"license": "MIT",
"dependencies": {
"chalk": "^2.0.1",
"jest-get-type": "^22.4.3",
"jest-util": "^23.4.0",
"pretty-format": "^23.6.0"
},
Expand Down
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.includes(valueType)) {
return title.replace(match, value);
}
return title.replace(match, pretty(value, {maxDepth: 1, min: true}));
};

Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7177,6 +7177,11 @@ jest-docblock@^21.0.0:
resolved "https://registry.yarnpkg.com/jest-docblock/-/jest-docblock-21.2.0.tgz#51529c3b30d5fd159da60c27ceedc195faf8d414"
integrity sha512-5IZ7sY9dBAYSV+YjQ0Ovb540Ku7AO9Z5o2Cg789xj167iQuZ2cG+z0f3Uct6WeYLbU6aQiM2pCs7sZ+4dotydw==

jest-get-type@^22.4.3:
version "22.4.3"
resolved "https://registry.yarnpkg.com/jest-get-type/-/jest-get-type-22.4.3.tgz#e3a8504d8479342dd4420236b322869f18900ce4"
integrity sha512-/jsz0Y+V29w1chdXVygEKSz2nBoHoYqNShPe+QgxSNjAuP1i8+k4LbQNrfoliKej0P45sivkSCh7yiD6ubHS3w==

[email protected]:
version "23.5.0"
resolved "https://registry.yarnpkg.com/jest-haste-map/-/jest-haste-map-23.5.0.tgz#d4ca618188bd38caa6cb20349ce6610e194a8065"
Expand Down