Skip to main content
Version: 29.7

Jest 平台

你可以挑选 Jest 的特定功能并将它们用作独立包。以下是可用软件包的列表:

¥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

用于识别 git/hg 存储库中已修改文件的工具。导出两个函数:

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

  • getChangedFilesForRoots 返回一个 promise,该 promise 解析为具有更改的文件和存储库的对象。

    ¥getChangedFilesForRoots returns a promise that resolves to an object with the changed files and repos.

  • findRepos 返回一个解析为指定路径中包含的一组存储库的 promise。

    ¥findRepos returns a promise that resolves to a set of repositories contained in the specified path.

示例

¥Example

const {getChangedFilesForRoots} = require('jest-changed-files');

// print the set of modified files since last commit in the current repo
getChangedFilesForRoots(['./'], {
lastCommit: true,
}).then(result => console.log(result.changedFiles));

你可以在 自述文件 中阅读有关 jest-changed-files 的更多信息。

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

jest-diff

用于可视化数据变化的工具。导出一个函数,该函数比较任意类型的两个值并返回 "pretty-printed" 字符串,说明两个参数之间的差异。

¥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.

示例

¥Example

const {diff} = require('jest-diff');

const a = {a: {b: {c: 5}}};
const b = {a: {b: {c: 6}}};

const result = diff(a, b);

// print diff
console.log(result);

jest-docblock

用于提取和解析 JavaScript 文件顶部注释的工具。导出各种函数来操作注释块内的数据。

¥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.

示例

¥Example

const {parseWithComments} = require('jest-docblock');

const code = `
/**

* This is a sample

* * @flow
*/

console.log('Hello World!');
`;

const parsed = parseWithComments(code);

// prints an object with two attributes: comments and pragmas.
console.log(parsed);

你可以在 自述文件 中阅读有关 jest-docblock 的更多信息。

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

jest-get-type

识别任何 JavaScript 值的原始类型的模块。导出一个函数,该函数返回一个字符串,该字符串具有作为参数传递的值的类型。

¥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.

示例

¥Example

const {getType} = require('jest-get-type');

const array = [1, 2, 3];
const nullValue = null;
const undefinedValue = undefined;

// prints 'array'
console.log(getType(array));
// prints 'null'
console.log(getType(nullValue));
// prints 'undefined'
console.log(getType(undefinedValue));

jest-validate

用于验证用户提交的配置的工具。导出一个带有两个参数的函数:用户的配置以及包含示例配置和其他选项的对象。返回值是一个具有两个属性的对象:

¥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:

  • hasDeprecationWarnings,一个布尔值,指示提交的配置是否有弃用警告,

    ¥hasDeprecationWarnings, a boolean indicating whether the submitted configuration has deprecation warnings,

  • isValid,一个布尔值,指示配置是否正确。

    ¥isValid, a boolean indicating whether the configuration is correct or not.

示例

¥Example

const {validate} = require('jest-validate');

const configByUser = {
transform: '<rootDir>/node_modules/my-custom-transform',
};

const result = validate(configByUser, {
comment: ' Documentation: http://custom-docs.com',
exampleConfig: {transform: '<rootDir>/node_modules/babel-jest'},
});

console.log(result);

你可以在 自述文件 中阅读有关 jest-validate 的更多信息。

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

jest-worker

用于任务并行化的模块。导出一个类 JestWorker,该类采用 Node.js 模块的路径,并允许你像调用类方法一样调用模块的导出方法,并返回一个 promise,该 promise 会在指定方法在复刻进程中完成其执行时解析。

¥Module used for parallelization of tasks. Exports a class JestWorker 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.

示例

¥Example

heavy-task.js
module.exports = {
myHeavyTask: args => {
// long running CPU intensive task.
},
};
main.js
async function main() {
const worker = new Worker(require.resolve('./heavy-task.js'));

// run 2 tasks in parallel with different arguments
const results = await Promise.all([
worker.myHeavyTask({foo: 'bar'}),
worker.myHeavyTask({bar: 'foo'}),
]);

console.log(results);
}

main();

你可以在 自述文件 中阅读有关 jest-worker 的更多信息。

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

pretty-format

导出一个函数,将任何 JavaScript 值转换为人类可读的字符串。开箱即用地支持所有内置 JavaScript 类型,并允许通过用户定义的插件扩展特定于应用的类型。

¥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.

示例

¥Example

const {format: prettyFormat} = require('pretty-format');

const val = {object: {}};
val.circularReference = val;
val[Symbol('foo')] = 'foo';
val.map = new Map([['prop', 'value']]);
val.array = [-0, Infinity, NaN];

console.log(prettyFormat(val));

你可以在 自述文件 中阅读有关 pretty-format 的更多信息。

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