Jest 平台
你可以挑选 Jest 的特定功能并将它们用作独立包。以下是可用软件包的列表:
已更改文件
用于识别 git/hg 存储库中已修改文件的工具。导出两个函数:
-
getChangedFilesForRoots返回一个承诺,该承诺解析为具有更改文件和存储库的对象。 -
findRepos返回解析为指定路径中包含的一组存储库的承诺。
例子
const {getChangedFilesForRoots} = require('jest-changed-files');// 打印出当前目录最后修改过的一组文件getChangedFilesForRoots(['./'], {lastCommit: true,}).then(result => console.log(result.changedFiles));
可以在自述文件中阅读更多相关jest-changed-files信息。
差异
可视化数据变化的工具。导出一个函数,该函数比较任何类型的两个值,并返回一个“pretty printed”的字符串,说明两个参数之间的差异。
例子
const diff = require('jest-diff').default;const a = {a: {b: {c: 5}}};const b = {a: {b: {c: 6}}};const result = diff(a, b);// print diffconsole.log(result);
Docblock
用于提取和解析 JavaScript 文件顶部注释的工具。导出各种函数来操作注释块内的数据。
例子
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信息。
获取类型
标识任何 JavaScript 值的原始类型的模块。导出一个函数,该函数返回一个字符串,该字符串具有作为参数传递的值的类型。
例子
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));
验证
用于验证用户提交的配置的工具。 导出具有两个参数的函数:用户配置和包含示例配置和其他选项的对象。返回值是具有两个属性的对象:
-
hasDeprecationWarnings,一个布尔值,指示提交的配置是否有弃用警告, -
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);
可以在自述文件中阅读更多相关jest-validate信息。
worker
用于任务并行化的模块。出口类JestWorker是需要的Node.js模块的路径,并允许你调用模块的导出的方法,好像他们是类方法,返回一个 Promise,消除了在所指定的方法完成它的执行在分叉过程。
例子
// heavy-task.jsmodule.exports = {myHeavyTask: args => {// long running CPU intensive task.},};
// main.jsasync function main() {const worker = new Worker(require.resolve('./heavy-task.js'));// run 2 tasks in parallel with different argumentsconst results = await Promise.all([worker.myHeavyTask({foo: 'bar'}),worker.myHeavyTask({bar: 'foo'}),]);console.log(results);}main();
可以在自述文件中阅读更多相关jest-worker信息。
格式
导出并将任何 JavaScript 值转换为人类可读字符串的函数。开箱即用地支持所有内置 JavaScript 类型,并允许通过用户定义的插件扩展特定于应用程序的类型。
例子
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));
可以在自述文件中阅读更多pretty-format相关信息。