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

Pretty-print React elements #1566

Merged
merged 3 commits into from
Sep 1, 2016
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
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/);
});
9 changes: 6 additions & 3 deletions packages/jest-diff/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@

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

const ReactElementPlugin = require('pretty-format/plugins/ReactElement');
const ReactTestComponentPlugin = require('pretty-format/plugins/ReactTestComponent');

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 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 +50,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
7 changes: 5 additions & 2 deletions packages/jest-snapshot/src/SnapshotFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
*/
'use strict';

const ReactElementPlugin = require('pretty-format/plugins/ReactElement');
const ReactTestComponentPlugin = require('pretty-format/plugins/ReactTestComponent');

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 jsxLikePlugins = [ReactElementPlugin, ReactTestComponentPlugin];
const SNAPSHOT_EXTENSION = 'snap';

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

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

Expand Down