Skip to content

Commit

Permalink
Merge pretty-format into Jest (jestjs#2300)
Browse files Browse the repository at this point in the history
* Import pretty-format into Jest.

* Fix all lint errors in pretty-format.
  • Loading branch information
cpojer authored Dec 12, 2016
1 parent 41fe238 commit 17bfaa9
Show file tree
Hide file tree
Showing 18 changed files with 1,881 additions and 10 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,24 @@
"babel-plugin-transform-flow-strip-types": "^6.18.0",
"chalk": "^1.1.3",
"codecov": "^1.0.1",
"eslint": "^3.11.1",
"eslint-plugin-babel": "^4.0.0",
"eslint-plugin-flow-vars": "^0.5.0",
"eslint-plugin-flowtype": "^2.28.2",
"eslint-plugin-react": "^6.7.1",
"eslint": "^3.11.1",
"flow-bin": "^0.36.0",
"glob": "^7.1.1",
"graceful-fs": "^4.1.11",
"istanbul-api": "^1.1.0-alpha.1",
"istanbul-lib-coverage": "^1.0.0",
"jasmine-reporters": "^2.2.0",
"jsdom": "^9.8.3",
"left-pad": "^1.1.1",
"lerna": "2.0.0-beta.30",
"minimatch": "^3.0.3",
"mkdirp": "^0.5.1",
"progress": "^1.1.8",
"react": "15.3.0",
"rimraf": "^2.5.4",
"strip-ansi": "^3.0.1"
},
Expand Down
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": "^17.0.3",
"pretty-format": "~4.3.1"
"pretty-format": "~17.0.3"
},
"scripts": {
"test": "../../packages/jest-cli/bin/jest.js"
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-diff/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

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

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

const chalk = require('chalk');
const diffStrings = require('./diffStrings');
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-matcher-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
},
"dependencies": {
"chalk": "^1.1.3",
"pretty-format": "~4.3.1"
"pretty-format": "~17.0.3"
}
}
2 changes: 1 addition & 1 deletion packages/jest-snapshot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"jest-matcher-utils": "^17.0.3",
"jest-util": "^17.0.2",
"natural-compare": "^1.4.0",
"pretty-format": "~4.3.1"
"pretty-format": "~17.0.3"
},
"scripts": {
"test": "../jest-cli/bin/jest.js"
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-snapshot/src/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

import type {Path} from 'types/Config';

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

let PLUGINS = [ReactElementPlugin, ReactTestComponentPlugin];

Expand Down
3 changes: 3 additions & 0 deletions packages/pretty-format/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**/__mocks__/**
**/__tests__/**
src
134 changes: 134 additions & 0 deletions packages/pretty-format/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
# pretty-format

> Stringify any JavaScript value.
- Supports [all built-in JavaScript types](#type-support)
- [Blazingly fast](https://gist.github.com/thejameskyle/2b04ffe4941aafa8f970de077843a8fd) (similar performance to v8's `JSON.stringify` and significantly faster than Node's `util.format`)
- Plugin system for extending with custom types (i.e. [`ReactTestComponent`](#reacttestcomponent-plugin))


## Installation

```sh
$ yarn add pretty-format
```

## Usage

```js
const prettyFormat = require('pretty-format');

var obj = { property: {} };
obj.circularReference = obj;
obj[Symbol('foo')] = 'foo';
obj.map = new Map();
obj.map.set('prop', 'value');
obj.array = [1, NaN, Infinity];

console.log(prettyFormat(obj));
```

**Result:**

```js
Object {
"property": Object {},
"circularReference": [Circular],
"map": Map {
"prop" => "value"
},
"array": Array [
1,
NaN,
Infinity
],
Symbol(foo): "foo"
}
```

#### Type Support

`Object`, `Array`, `ArrayBuffer`, `DataView`, `Float32Array`, `Float64Array`, `Int8Array`, `Int16Array`, `Int32Array`, `Uint8Array`, `Uint8ClampedArray`, `Uint16Array`, `Uint32Array`, `arguments`, `Boolean`, `Date`, `Error`, `Function`, `Infinity`, `Map`, `NaN`, `null`, `Number`, `RegExp`, `Set`, `String`, `Symbol`, `undefined`, `WeakMap`, `WeakSet`

### API

```js
console.log(prettyFormat(object));
console.log(prettyFormat(object, options));
```

Options:

* **`callToJSON`**<br>
Type: `boolean`, default: `true`<br>
Call `toJSON()` on passed object.
* **`indent`**<br>
Type: `number`, default: `2`<br>
Number of spaces for indentation.
* **`maxDepth`**<br>
Type: `number`, default: `Infinity`<br>
Print only this number of levels.
* **`min`**<br>
Type: `boolean`, default: `false`<br>
Print without whitespace.
* **`plugins`**<br>
Type: `array`, default: `[]`<br>
Plugins (see the next section).
* **`printFunctionName`**<br>
Type: `boolean`, default: `true`<br>
Print function names or just `[Function]`.
* **`escapeRegex`**<br>
Type: `boolean`, default: `false`<br>
Escape special characters in regular expressions.
* **`highlight`**<br>
Type: `boolean`, default: `false`<br>
Highlight syntax for terminal (works only with `ReactTestComponent` and `ReactElement` plugins.
* **`theme`**<br>
Type: `object`, default: `{tag: 'cyan', content: 'reset'...}`<br>
Syntax highlight theme.<br>
Uses [ansi-styles colors](https://github.com/chalk/ansi-styles#colors) + `reset` for no color.<br>
Available types: `tag`, `content`, `prop` and `value`.

### Plugins

Pretty format also supports adding plugins:

```js
const fooPlugin = {
test(val) {
return val && val.hasOwnProperty('foo');
},
print(val, print, indent) {
return 'Foo: ' + print(val.foo);
}
};

const obj = {foo: {bar: {}}};

prettyFormat(obj, {
plugins: [fooPlugin]
});
// Foo: Object {
// "bar": Object {}
// }
```

#### `ReactTestComponent` and `ReactElement` plugins

```js
const prettyFormat = require('pretty-format');
const reactTestPlugin = require('pretty-format/build/plugins/ReactTestComponent');
const reactElementPlugin = require('pretty-format/build/plugins/ReactElement');

const React = require('react');
const renderer = require('react-test-renderer');

const element = React.createElement('h1', null, 'Hello World');

prettyFormat(renderer.create(element).toJSON(), {
plugins: [reactTestPlugin, reactElementPlugin]
});
// <h1>
// Hello World
// </h1>
```
19 changes: 19 additions & 0 deletions packages/pretty-format/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "pretty-format",
"version": "17.0.3",
"repository": {
"type": "git",
"url": "https://github.com/facebook/jest.git"
},
"license": "BSD-3-Clause",
"description": "Stringify any JavaScript value.",
"main": "build/index.js",
"author": "James Kyle <[email protected]>",
"scripts": {
"test": "../../packages/jest-cli/bin/jest.js",
"perf": "node perf/test.js"
},
"dependencies": {
"ansi-styles": "^2.2.1"
}
}
Loading

0 comments on commit 17bfaa9

Please sign in to comment.