Skip to content

Commit

Permalink
Pretty-print React elements
Browse files Browse the repository at this point in the history
Fixes #1429
Fixes #1509
  • Loading branch information
gaearon committed Sep 1, 2016
1 parent d45f813 commit 0296476
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/jest-diff/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"chalk": "^1.1.3",
"diff": "^3.0.0",
"jest-matcher-utils": "^15.0.1",
"pretty-format": "^3.6.0"
"pretty-format": "^3.7.0"
},
"scripts": {
"test": "../../packages/jest-cli/bin/jest.js"
Expand Down
21 changes: 21 additions & 0 deletions packages/jest-diff/src/__tests__/diff-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,24 @@ test('booleans', () => {
expect(result).toBe(null);
expect(result).toBe(null);
});

test('React elements', () => {
const result = diff({
$$typeof: Symbol.for('react.element'),
type: 'div',
props: {
className: 'fun',
children: 'Hello',
},
}, {
$$typeof: Symbol.for('react.element'),
type: 'div',
className: 'fun',
props: {
children: 'Goodbye',
},
});
expect(stripAnsi(result)).toMatch(/<div[\s\S]+className="fun">/);
expect(stripAnsi(result)).toMatch(/\-\s+Hello/);
expect(stripAnsi(result)).toMatch(/\+\s+Goodbye/);
});
8 changes: 5 additions & 3 deletions packages/jest-diff/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ import type {DiffOptions} from './diffStrings';
const chalk = require('chalk');
const diffStrings = require('./diffStrings');
const {getType} = require('jest-matcher-utils');
const jsxLikeExtension = require('pretty-format/plugins/ReactTestComponent');
const prettyFormat = require('pretty-format');
const ReactTestComponentPlugin = require('pretty-format/plugins/ReactTestComponent');
const ReactElementPlugin = require('pretty-format/plugins/ReactElement');

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

// Generate a string that will highlight the difference between two values
Expand Down Expand Up @@ -47,8 +49,8 @@ function diff(a: any, b: any, options: ?DiffOptions): ?string {
return null;
default:
return diffStrings(
prettyFormat(a, {plugins: [jsxLikeExtension]}, options),
prettyFormat(b, {plugins: [jsxLikeExtension]}, options),
prettyFormat(a, {plugins: jsxLikePlugins}, options),
prettyFormat(b, {plugins: jsxLikePlugins}, options),
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-snapshot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"jest-diff": "^15.0.1",
"jest-file-exists": "^15.0.0",
"jest-util": "^15.0.1",
"pretty-format": "^3.6.0"
"pretty-format": "^3.7.0"
},
"scripts": {
"test": "../jest-cli/bin/jest.js"
Expand Down
6 changes: 4 additions & 2 deletions packages/jest-snapshot/src/SnapshotFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
const createDirectory = require('jest-util').createDirectory;
const fileExists = require('jest-file-exists');
const fs = require('fs');
const jsxLikeExtension = require('pretty-format/plugins/ReactTestComponent');
const path = require('path');
const prettyFormat = require('pretty-format');
const ReactTestComponentPlugin = require('pretty-format/plugins/ReactTestComponent');
const ReactElementPlugin = require('pretty-format/plugins/ReactElement');

const jsxLikePlugins = [ReactTestComponentPlugin, ReactElementPlugin];
const SNAPSHOT_EXTENSION = 'snap';

import type {Path} from 'types/Config';
Expand Down Expand Up @@ -90,7 +92,7 @@ class SnapshotFile {

serialize(data: any): string {
return addExtraLineBreaks(prettyFormat(data, {
plugins: [jsxLikeExtension],
plugins: jsxLikePlugins,
}));
}

Expand Down

0 comments on commit 0296476

Please sign in to comment.