メインコンテンツへスキップ
Version: 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));

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

jest-diff

データの変更点を可視化するツールです。 任意の型の2つの値を比較し、2つの値の相違点を表す "pretty-print" された文字列を返す関数をエクスポートしています。

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

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

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つの引数を取る関数をエクスポートしています。 返り値は、次の二つの属性を持つオブジェクトです。

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

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

jest-worker

タスクの並列化に使われるモジュール このモジュールは JestWorker クラスをエクスポートしています。 この Worker クラスは、Node.js のモジュールのパスを取り、モジュールがエクスポートしたメソッドをあたかも自身のメソッドであるかのように実行して、指定したメソッドの実行がフォークしたプロセス上で完了した時に解決する Promise を返します。

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

あらゆる 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));

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