|
| 1 | +--- |
| 2 | +id: jest-platform |
| 3 | +title: Jest Platform |
| 4 | +--- |
| 5 | + |
| 6 | +You can cherry pick specific features of Jest and use them as standalone packages. Here's a list of the available packages: |
| 7 | + |
| 8 | +## jest-changed-files |
| 9 | + |
| 10 | +Tool for identifying modified files in a git/hg repository. Exports two functions: |
| 11 | + |
| 12 | +- `getChangedFilesForRoots` returns a promise that resolves to an object with the changed files and repos. |
| 13 | +- `findRepos` returns a promise that resolves to a set of repositories contained in the specified path. |
| 14 | + |
| 15 | +### Example |
| 16 | + |
| 17 | +``` javascript |
| 18 | +const {getChangedFilesForRoots} = require( 'jest-changed-files'); |
| 19 | + |
| 20 | +// print the set of modified files since last commit in the current repo |
| 21 | +getChangedFilesForRoots(['./'], { |
| 22 | + lastCommit: true, |
| 23 | +}).then(result => console.log(result.changedFiles)); |
| 24 | + |
| 25 | +``` |
| 26 | + |
| 27 | +You can read more about `jest-changed-files` in the [readme file](https://github.com/facebook/jest/blob/master/packages/jest-changed-files/README.md). |
| 28 | + |
| 29 | +## jest-diff |
| 30 | + |
| 31 | +Tool for visualizing changes in data. Exports a function that compares two values of any type and returns a "pretty-printed" string illustrating the difference between the two arguments. |
| 32 | + |
| 33 | +### Example |
| 34 | + |
| 35 | +``` javascript |
| 36 | +const diff = require('jest-diff'); |
| 37 | + |
| 38 | +const a = {a: {b: {c: 5}}}; |
| 39 | +const b = {a: {b: {c: 6}}}; |
| 40 | + |
| 41 | +const result = diff(a, b); |
| 42 | + |
| 43 | +// print diff |
| 44 | +console.log(result); |
| 45 | +``` |
| 46 | + |
| 47 | +## jest-docblock |
| 48 | + |
| 49 | +Tool for extracting and parsing the comments at the top of a JavaScript file. Exports various function to manipulate the data inside the comment block. |
| 50 | + |
| 51 | +### Example |
| 52 | + |
| 53 | +``` javascript |
| 54 | +const { parseWithComments } = require('jest-docblock'); |
| 55 | + |
| 56 | +const code = ` |
| 57 | +/** |
| 58 | + * This is a sample |
| 59 | + * |
| 60 | + * @flow |
| 61 | + */ |
| 62 | + |
| 63 | + console.log('Hello World!'); |
| 64 | +`; |
| 65 | + |
| 66 | +const parsed = parseWithComments(code); |
| 67 | + |
| 68 | +// prints an object with two attributes: comments and pragmas. |
| 69 | +console.log(parsed) |
| 70 | +``` |
| 71 | +You can read more about `jest-docblock` in the [readme file](https://github.com/facebook/jest/blob/master/packages/jest-docblock/README.md). |
| 72 | + |
| 73 | +## jest-get-type |
| 74 | + |
| 75 | +Module that identifies the primitive type of any JavaScript value. Exports a function that returns a string with the type of the value passed as argument. |
| 76 | + |
| 77 | +### Example |
| 78 | + |
| 79 | +``` javascript |
| 80 | +const getType = require('jest-get-type'); |
| 81 | + |
| 82 | +const array = [1, 2, 3]; |
| 83 | +const nullValue = null; |
| 84 | +const undefinedValue = undefined; |
| 85 | + |
| 86 | +// prints 'array' |
| 87 | +console.log(getType(array)); |
| 88 | +// prints 'null' |
| 89 | +console.log(getType(nullValue)); |
| 90 | +// prints 'undefined' |
| 91 | +console.log(getType(undefinedValue)); |
| 92 | +``` |
| 93 | + |
| 94 | +## jest-validate |
| 95 | + |
| 96 | +Tool for validating configurations submitted by users. Exports a function that takes two arguments: the user's configuration and an object containing an example configuration and other options. The return value is an object with two attributes: |
| 97 | + |
| 98 | +- `hasDeprecationWarnings`, a boolean indicating whether the submitted configuration has deprecation warnings, |
| 99 | +- `isValid`, a boolean indicating whether the configuration is correct or not. |
| 100 | + |
| 101 | +### Example |
| 102 | + |
| 103 | +``` javascript |
| 104 | +const { validate } = require('jest-validate'); |
| 105 | + |
| 106 | +const configByUser = { |
| 107 | + transform: '<rootDir>/node_modules/my-custom-transform' |
| 108 | +} |
| 109 | + |
| 110 | +const result = validate(configByUser, { |
| 111 | + comment: ' Documentation: http://custom-docs.com', |
| 112 | + exampleConfig: { transform: '<rootDir>/node_modules/babel-jest' }, |
| 113 | +}); |
| 114 | + |
| 115 | +console.log(result); |
| 116 | +``` |
| 117 | + |
| 118 | +You can read more about `jest-validate` in the [readme file](https://github.com/facebook/jest/blob/master/packages/jest-validate/README.md). |
| 119 | + |
| 120 | +## jest-worker |
| 121 | + |
| 122 | +Module used for parallelization of tasks. Exports a class `Worker` that takes the path of Node.js module and lets you call the module's exported methods as if they where class methods, returning a promise that resolves when the specified method finishes its execution in a forked process. |
| 123 | + |
| 124 | +### Example |
| 125 | + |
| 126 | +``` javascript |
| 127 | +// heavy-task.js |
| 128 | + |
| 129 | +module.exports = { |
| 130 | + myHeavyTask: (args) => { |
| 131 | + // long running CPU intensive task. |
| 132 | + } |
| 133 | +} |
| 134 | +``` |
| 135 | + |
| 136 | +``` javascript |
| 137 | +// main.js |
| 138 | + |
| 139 | +async function main() { |
| 140 | + const worker = new Worker(require.resolve('./heavy-task.js')); |
| 141 | + |
| 142 | + // run 2 tasks in parellel with different arguments |
| 143 | + const results = await Promise.all([ |
| 144 | + worker.myHeavyTask({ foo: 'bar' }), |
| 145 | + worker.myHeavyTask({bar: 'foo' }), |
| 146 | + ]); |
| 147 | + |
| 148 | + console.log(results); |
| 149 | +} |
| 150 | + |
| 151 | +main(); |
| 152 | +``` |
| 153 | + |
| 154 | +You can read more about `jest-worker` in the [readme file](https://github.com/facebook/jest/blob/master/packages/jest-worker/README.md). |
| 155 | + |
| 156 | +## pretty-format |
| 157 | + |
| 158 | +Exports a function that converts any JavaScript value into a human-readable string. Supports all built-in JavaScript types out of the box and allows extension for application-specific types via user-defined plugins. |
| 159 | + |
| 160 | +### Example |
| 161 | + |
| 162 | +``` javascript |
| 163 | +const prettyFormat = require('pretty-format'); |
| 164 | + |
| 165 | +const val = {object: {}}; |
| 166 | +val.circularReference = val; |
| 167 | +val[Symbol('foo')] = 'foo'; |
| 168 | +val.map = new Map([['prop', 'value']]); |
| 169 | +val.array = [-0, Infinity, NaN]; |
| 170 | + |
| 171 | +console.log(prettyFormat(val)); |
| 172 | +``` |
| 173 | + |
| 174 | +You can read more about `pretty-format` in the [readme file](https://github.com/facebook/jest/blob/master/packages/pretty-format/README.md). |
0 commit comments