メインコンテンツへスキップ
バージョン: 29.7

Jest プラットフォーム

Jest の特定の機能を取り出して、スタンドアロンパッケージとして使用することができます。利用可能なパッケージを次に示します。

jest-changed-files

git/hg リポジトリ内の変更されたファイルを特定するためのツールです。2 つの関数をエクスポートします。

  • getChangedFilesForRoots は、変更されたファイルとリポジトリを含むオブジェクトで解決される Promise を返します。
  • findRepos は、指定されたパスに含まれるリポジトリのセットで解決される Promise を返します。

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 の詳細については、README ファイルをご覧ください。

jest-diff

データの変更を視覚化するツールです。任意のタイプの 2 つの値を比較し、2 つの引数の違いを説明する「見栄えの良い」文字列を返す関数をエクスポートします。

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 ファイルの先頭のコメントを抽出して解析するためのツールです。コメントブロック内のデータを操作するためのさまざまな関数をエクスポートします。

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 の詳細については、README ファイルをご覧ください。

jest-get-type

任意の 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));

jest-validate

ユーザーが送信した設定を検証するためのツールです。2 つの引数を受け取る関数をエクスポートします。ユーザーの設定と、サンプル設定およびその他のオプションを含むオブジェクトです。戻り値には、2 つの属性を持つオブジェクトがあります

  • 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 の詳細を読むことができます。

jest-worker

タスクの並列化に使用されるモジュールです。Node.js モジュールのパスを取得する JestWorker クラスをエクスポートし、モジュールのエクスポートされたメソッドをクラスメソッドであるかのように呼び出し、分岐処理の指定したメソッドの実行が完了すると解決されるプロミスを返します。

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 の詳細を読むことができます。

pretty-format

任意の JavaScript 値を人間が読める文字列に変換する関数をエクスポートします。標準の JavaScript 型をすべてサポートし、ユーザー定義のプラグインを介してアプリケーション固有の型の拡張ができます。

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 の詳細を読むことができます。