Jest 平台工具

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

jest-changed-files

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

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

例子

  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

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

例子

  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. // 打印两个值的差异
  6. console.log(result);

jest-docblock

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

例子

  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. // 打印一个包含两个属性的对象: comments and pragmas.
  12. console.log(parsed);

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

jest-get-type

用于识别任何 JavaScript 值的原始类型的模块, 模块导出了一个可以识别传入参数类型并将类型以字符串作为返回值的函数。

例子

  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

用于验证用户提交的配置的工具。 导出一个要传入两个参数的函数:一个是用户的配置,另一个是包含示例配置和其他选项的对象。 该函数返回一个具有两个属性的对象:

  • hasDeprecationWarnings,布尔值,提示你提交的配置是否有弃用警告
  • 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

用于任务并行化的模块。 导出一个 Worker 类,传入 Node.js 的路径,可以让你调用模块导出的方法,就像是这个类的方法一样,返回一个承诺,当指定的方法在分支进程里执行完成的时候就会解析。

例子

heavy-task.js

  1. module.exports = {
  2. myHeavyTask: args => {
  3. // 长时间运行的CPU密集型任务
  4. },
  5. };

main.js

  1. async function main() {
  2. const worker = new Worker(require.resolve('./heavy-task.js'));
  3. // 传入不同参数并行运行两个任务
  4. const results = await Promise.all([
  5. worker.myHeavyTask({foo: 'bar'}),
  6. worker.myHeavyTask({bar: 'foo'}),
  7. ]);
  8. console.log(results);
  9. }
  10. main();

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

pretty-format

导出一个将任何 JavaScript 值转换为可读的字符串的函数。 天生就支持所有 JavaScript 类型,可以通过用户自定义插件来拓展针对特定应用的类型。

例子

  1. const {format: 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.