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

[jest-each] Add primitive pretty printing #7694

Merged
merged 8 commits into from
Jan 25, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion 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
2 changes: 1 addition & 1 deletion e2e/each/__tests__/failure.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe.each`
${'a'} | ${'b'}
${'c'} | ${'d'}
`(
'template table describe fails on all rows expected $left == $right',
'template table describe fails on all rows expected "$left" == "$right"',
({left, right}) => {
it('fails ', () => {
expect(left).toBe(right);
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.1.0",
"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
13 changes: 13 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,13 @@ const RECEIVED_COLOR = chalk.red;
const SUPPORTED_PLACEHOLDERS = /%[sdifjoOp%]/g;
const PRETTY_PLACEHOLDER = '%p';
const INDEX_PLACEHOLDER = '%#';
const PRIMITIVES = new Set([
Copy link
Contributor

Choose a reason for hiding this comment

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

Not blocking at all, but we might consider moving this to jest-get-types (e.g.: isPrimitiveType(getType(value))).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rubennorte yup good shout, I'll send another PR for this change so we can merge this PR in the meantime :)

'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 +203,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.has(valueType)) {
return title.replace(match, value);
}
return title.replace(match, pretty(value, {maxDepth: 1, min: true}));
};

Expand Down