Skip to content
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
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### Fixes

* `[pretty-format]` Do not call toJSON recursively
([#5044](https://github.com/facebook/jest/pull/5044))
* `[pretty-format]` Fix errors when identity-obj-proxy mocks CSS Modules
([#4935](https://github.com/facebook/jest/pull/4935))
* `[babel-jest]` Fix support for namespaced babel version 7
Expand Down Expand Up @@ -52,7 +54,7 @@
([#4730](https://github.com/facebook/jest/pull/4730))
* `[jest-runtime]` Use realpath to match transformers
([#5000](https://github.com/facebook/jest/pull/5000))
* `[expect]` [**BREAKING**] Replace identity equality with Object.is in toBe
* `[expect]` [**BREAKING**] Replace identity equality with Object.is in toBe
matcher ([#4917](https://github.com/facebook/jest/pull/4917))

### Features
Expand Down Expand Up @@ -127,7 +129,7 @@
environments ([#4958](https://github.com/facebook/jest/pull/4958))
* `[jest-snapshot]` Promises support for `toThrowErrorMatchingSnapshot`
([#4946](https://github.com/facebook/jest/pull/4946))
* `[jest-cli]` Explain which snapshots are obsolete
* `[jest-cli]` Explain which snapshots are obsolete
([#5005](https://github.com/facebook/jest/pull/5005))

### Chore & Maintenance
Expand Down
4 changes: 2 additions & 2 deletions packages/pretty-format/src/__tests__/pretty_format.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -687,13 +687,13 @@ describe('prettyFormat()', () => {
).toEqual('Object {\n "toJSON": false,\n "value": true,\n}');
});

it('calls toJSON recursively', () => {
it('does not call toJSON recursively', () => {
expect(
prettyFormat({
toJSON: () => ({toJSON: () => ({value: true})}),
value: false,
}),
).toEqual('Object {\n "value": true,\n}');
).toEqual('Object {\n "toJSON": [Function toJSON],\n}');
});

it('calls toJSON on Sets', () => {
Expand Down
16 changes: 13 additions & 3 deletions packages/pretty-format/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ function printComplexValue(
indentation: string,
depth: number,
refs: Refs,
hasCalledToJSON?: boolean,
): string {
if (refs.indexOf(val) !== -1) {
return '[Circular]';
Expand All @@ -189,9 +190,10 @@ function printComplexValue(
config.callToJSON &&
!hitMaxDepth &&
val.toJSON &&
typeof val.toJSON === 'function'
typeof val.toJSON === 'function' &&
!hasCalledToJSON
) {
return printer(val.toJSON(), config, indentation, depth, refs);
return printer(val.toJSON(), config, indentation, depth, refs, true);
}

const toStringed = toString.call(val);
Expand Down Expand Up @@ -312,6 +314,7 @@ function printer(
indentation: string,
depth: number,
refs: Refs,
hasCalledToJSON?: boolean,
): string {
const plugin = findPlugin(config.plugins, val);
if (plugin !== null) {
Expand All @@ -327,7 +330,14 @@ function printer(
return basicResult;
}

return printComplexValue(val, config, indentation, depth, refs);
return printComplexValue(
val,
config,
indentation,
depth,
refs,
hasCalledToJSON,
);
}

const DEFAULT_THEME: Theme = {
Expand Down
1 change: 1 addition & 0 deletions types/PrettyFormat.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export type Printer = (
indentation: string,
depth: number,
refs: Refs,
hasCalledToJSON?: boolean,
) => string;

export type Test = any => boolean;
Expand Down