You can cherry pick specific features of Jest and use them as standalone packages. Here's a list of the available packages:

jest-changed-files

Tool for identifying modified files in a git/hg repository. Exports two functions:

  • getChangedFilesForRoots returns a promise that resolves to an object with the changed files and repos.
  • findRepos returns a promise that resolves to a set of repositories contained in the specified path.

例子

  1. const {getChangedFilesForRoots} = require('jest-changed-files');
  2. // 打印出当前目录最后修改过的一组文件
  3. getChangedFilesForRoots(['./'], {
  4. lastCommit: true,
  5. }).then(result => console.log(result.changedFiles));

You can read more about jest-changed-files in the readme file.

jest-diff

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.

例子

  1. const diff = require('jest-diff');
  2. const a = {a: {b: {c: 5}}};
  3. const b = {a: {b: {c: 6}}};
  4. const result = diff(a, b);
  5. // print diff
  6. console.log(result);

jest-docblock

Tool for extracting and parsing the comments at the top of a JavaScript file. Exports various functions to manipulate the data inside the comment block.

例子

  1. const {parseWithComments} = require('jest-docblock');
  2. const code = `
  3. /**
  4. * This is a sample
  5. *
  6. * @flow
  7. */
  8. console.log('Hello World!');
  9. `;
  10. const parsed = parseWithComments(code);
  11. // prints an object with two attributes: comments and pragmas.
  12. console.log(parsed);

You can read more about jest-docblock in the readme file.

jest-get-type

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.

例子

  1. const getType = require('jest-get-type');
  2. const array = [1, 2, 3];
  3. const nullValue = null;
  4. const undefinedValue = undefined;
  5. // prints 'array'
  6. console.log(getType(array));
  7. // prints 'null'
  8. console.log(getType(nullValue));
  9. // prints 'undefined'
  10. console.log(getType(undefinedValue));

jest-validate

用于验证用户提交的配置的工具。 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:

  • hasDeprecationWarnings, a boolean indicating whether the submitted configuration has deprecation warnings,
  • isValid, 一个布尔值, 指示配置是否正确。

例子

  1. const {validate} = require('jest-validate');
  2. const configByUser = {
  3. transform: '<rootDir>/node_modules/my-custom-transform',
  4. };
  5. const result = validate(configByUser, {
  6. comment: ' Documentation: http://custom-docs.com',
  7. exampleConfig: {transform: '<rootDir>/node_modules/babel-jest'},
  8. });
  9. console.log(result);

You can read more about jest-validate in the readme file.

jest-worker

用于任务并行化的模块。 Exports a class Worker that takes the path of Node.js module and lets you call the module's exported methods as if they were class methods, returning a promise that resolves when the specified method finishes its execution in a forked process.

例子

  1. // heavy-task.js
  2. module.exports = {
  3. myHeavyTask: args => {
  4. // long running CPU intensive task.
  5. },
  6. };
  1. // main.js
  2. async function main() {
  3. const worker = new Worker(require.resolve('./heavy-task.js'));
  4. // run 2 tasks in parallel with different arguments
  5. const results = await Promise.all([
  6. worker.myHeavyTask({foo: 'bar'}),
  7. worker.myHeavyTask({bar: 'foo'}),
  8. ]);
  9. console.log(results);
  10. }
  11. main();

You can read more about jest-worker in the readme file.

pretty-format

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.

例子

  1. const prettyFormat = require('pretty-format');
  2. const val = {object: {}};
  3. val.circularReference = val;
  4. val[Symbol('foo')] = 'foo';
  5. val.map = new Map([['prop', 'value']]);
  6. val.array = [-0, Infinity, NaN];
  7. console.log(prettyFormat(val));

You can read more about pretty-format in the readme file.