Skip to content

Commit 5a72b1d

Browse files
authored
Merge pull request #1566 from facebook/jsx
Pretty-print React elements
2 parents d45f813 + da75b02 commit 5a72b1d

File tree

5 files changed

+34
-7
lines changed

5 files changed

+34
-7
lines changed

packages/jest-diff/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"chalk": "^1.1.3",
1212
"diff": "^3.0.0",
1313
"jest-matcher-utils": "^15.0.1",
14-
"pretty-format": "^3.6.0"
14+
"pretty-format": "^3.7.0"
1515
},
1616
"scripts": {
1717
"test": "../../packages/jest-cli/bin/jest.js"

packages/jest-diff/src/__tests__/diff-test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,24 @@ test('booleans', () => {
101101
expect(result).toBe(null);
102102
expect(result).toBe(null);
103103
});
104+
105+
test('React elements', () => {
106+
const result = diff({
107+
$$typeof: Symbol.for('react.element'),
108+
type: 'div',
109+
props: {
110+
className: 'fun',
111+
children: 'Hello',
112+
},
113+
}, {
114+
$$typeof: Symbol.for('react.element'),
115+
type: 'div',
116+
className: 'fun',
117+
props: {
118+
children: 'Goodbye',
119+
},
120+
});
121+
expect(stripAnsi(result)).toMatch(/<div[\s\S]+className="fun">/);
122+
expect(stripAnsi(result)).toMatch(/\-\s+Hello/);
123+
expect(stripAnsi(result)).toMatch(/\+\s+Goodbye/);
124+
});

packages/jest-diff/src/index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@
1212

1313
import type {DiffOptions} from './diffStrings';
1414

15+
const ReactElementPlugin = require('pretty-format/plugins/ReactElement');
16+
const ReactTestComponentPlugin = require('pretty-format/plugins/ReactTestComponent');
17+
1518
const chalk = require('chalk');
1619
const diffStrings = require('./diffStrings');
1720
const {getType} = require('jest-matcher-utils');
18-
const jsxLikeExtension = require('pretty-format/plugins/ReactTestComponent');
1921
const prettyFormat = require('pretty-format');
2022

23+
const jsxLikePlugins = [ReactTestComponentPlugin, ReactElementPlugin];
2124
const NO_DIFF_MESSAGE = require('./constants').NO_DIFF_MESSAGE;
2225

2326
// Generate a string that will highlight the difference between two values
@@ -47,8 +50,8 @@ function diff(a: any, b: any, options: ?DiffOptions): ?string {
4750
return null;
4851
default:
4952
return diffStrings(
50-
prettyFormat(a, {plugins: [jsxLikeExtension]}, options),
51-
prettyFormat(b, {plugins: [jsxLikeExtension]}, options),
53+
prettyFormat(a, {plugins: jsxLikePlugins}, options),
54+
prettyFormat(b, {plugins: jsxLikePlugins}, options),
5255
);
5356
}
5457
}

packages/jest-snapshot/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"jest-diff": "^15.0.1",
1212
"jest-file-exists": "^15.0.0",
1313
"jest-util": "^15.0.1",
14-
"pretty-format": "^3.6.0"
14+
"pretty-format": "^3.7.0"
1515
},
1616
"scripts": {
1717
"test": "../jest-cli/bin/jest.js"

packages/jest-snapshot/src/SnapshotFile.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@
99
*/
1010
'use strict';
1111

12+
const ReactElementPlugin = require('pretty-format/plugins/ReactElement');
13+
const ReactTestComponentPlugin = require('pretty-format/plugins/ReactTestComponent');
14+
1215
const createDirectory = require('jest-util').createDirectory;
1316
const fileExists = require('jest-file-exists');
1417
const fs = require('fs');
15-
const jsxLikeExtension = require('pretty-format/plugins/ReactTestComponent');
1618
const path = require('path');
1719
const prettyFormat = require('pretty-format');
1820

21+
const jsxLikePlugins = [ReactElementPlugin, ReactTestComponentPlugin];
1922
const SNAPSHOT_EXTENSION = 'snap';
2023

2124
import type {Path} from 'types/Config';
@@ -90,7 +93,7 @@ class SnapshotFile {
9093

9194
serialize(data: any): string {
9295
return addExtraLineBreaks(prettyFormat(data, {
93-
plugins: [jsxLikeExtension],
96+
plugins: jsxLikePlugins,
9497
}));
9598
}
9699

0 commit comments

Comments
 (0)