跳转至主内容
Version: 25.x

Jest 平台工具

你可以自由选择Jest的特定功能,并独立使用这些包。 下面是可用的软件包列表:

jest-changed-files

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

  • getChangedFilesForRoots 返回一个resolved状态的Promise,此Promise包含修改文件和仓库信息。
  • findRepos 返回一个resolved状态的Promise,此Promise包含指定路径的一组仓库数据。

例子

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

// 打印出当前目录最后修改过的一组文件
getChangedFilesForRoots(['./'], {
lastCommit: true,
}).then(result => console.log(result.changedFiles));

获取更多信息,请关注jest-changed-files中的 Readme

jest-diff

数据更改的可视化工具。 输出一个函数,两个任何类型的值输入该函数后,返回一个“较易读”的字符串来展现其区别。

例子

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

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. Exports various functions to manipulate the data inside the comment block.

例子

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);
console.log(parsed);

获取更多关于jest-docblock 的信息,请阅读 readme file.

jest-get-type

Module that identifies the primitive type of any JavaScript value. 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.

例子

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

用于验证用户提交的配置的工具。 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: The return value is an object with two attributes:

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

例子

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);

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.

例子

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();

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. Supports all built-in JavaScript types out of the box and allows extension for application-specific types via user-defined plugins.

例子

const 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));

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