forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pretty-format into Jest (jestjs#2300)
* Import pretty-format into Jest. * Fix all lint errors in pretty-format.
- Loading branch information
Showing
18 changed files
with
1,881 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,6 @@ | |
}, | ||
"dependencies": { | ||
"chalk": "^1.1.3", | ||
"pretty-format": "~4.3.1" | ||
"pretty-format": "~17.0.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
**/__mocks__/** | ||
**/__tests__/** | ||
src |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
Oops, something went wrong.