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

Matcher の使用

Jest は、さまざまな方法で値をテストするために「Matcher」を使用します。このドキュメントでは、一般的に使用される Matcher をいくつか紹介します。完全なリストについては、expect API ドキュメントを参照してください。

一般的な Matcher

値をテストする最も簡単な方法は、完全一致を使用することです。

test('two plus two is four', () => {
expect(2 + 2).toBe(4);
});

このコードでは、expect(2 + 2) は「期待値」オブジェクトを返します。通常、これらの期待値オブジェクトに対して行う操作は、Matcher を呼び出すことだけです。このコードでは、.toBe(4) が Matcher です。Jest が実行されると、すべての失敗した Matcher が追跡され、わかりやすいエラーメッセージが表示されます。

toBeObject.is を使用して完全一致をテストします。オブジェクトの値を確認する場合は、toEqual を使用してください。

test('object assignment', () => {
const data = {one: 1};
data['two'] = 2;
expect(data).toEqual({one: 1, two: 2});
});

toEqual は、オブジェクトまたは配列のすべてのフィールドを再帰的にチェックします。

ヒント

toEqual は、undefined のプロパティを持つオブジェクトキー、undefined の配列項目、配列の疎性、またはオブジェクトの型が一致しないものを無視します。これらを考慮する場合は、代わりに toStrictEqual を使用してください。

not を使用して、Matcher の反対をテストすることもできます。

test('adding positive numbers is not zero', () => {
for (let a = 1; a < 10; a++) {
for (let b = 1; b < 10; b++) {
expect(a + b).not.toBe(0);
}
}
});

真偽値

テストでは、undefinednullfalse を区別する必要がある場合と、区別したくない場合があります。Jest には、自分が何をしたいかを明確にできるヘルパーが含まれています。

  • toBeNullnull のみに一致します。
  • toBeUndefinedundefined のみに一致します。
  • toBeDefinedtoBeUndefined の反対です。
  • toBeTruthy は、if 文で true として扱われるものすべてに一致します。
  • toBeFalsy は、if 文で false として扱われるものすべてに一致します。

例:

test('null', () => {
const n = null;
expect(n).toBeNull();
expect(n).toBeDefined();
expect(n).not.toBeUndefined();
expect(n).not.toBeTruthy();
expect(n).toBeFalsy();
});

test('zero', () => {
const z = 0;
expect(z).not.toBeNull();
expect(z).toBeDefined();
expect(z).not.toBeUndefined();
expect(z).not.toBeTruthy();
expect(z).toBeFalsy();
});

コードに最も正確に対応する Matcher を使用する必要があります。

数値

数値を比較するほとんどの方法には、Matcher の同等物があります。

test('two plus two', () => {
const value = 2 + 2;
expect(value).toBeGreaterThan(3);
expect(value).toBeGreaterThanOrEqual(3.5);
expect(value).toBeLessThan(5);
expect(value).toBeLessThanOrEqual(4.5);

// toBe and toEqual are equivalent for numbers
expect(value).toBe(4);
expect(value).toEqual(4);
});

浮動小数点数の等価性については、小さな丸め誤差にテストを依存させたくないため、toEqual の代わりに toBeCloseTo を使用してください。

test('adding floating point numbers', () => {
const value = 0.1 + 0.2;
//expect(value).toBe(0.3); This won't work because of rounding error
expect(value).toBeCloseTo(0.3); // This works.
});

文字列

toMatch を使用して、文字列を正規表現と照合できます。

test('there is no I in team', () => {
expect('team').not.toMatch(/I/);
});

test('but there is a "stop" in Christoph', () => {
expect('Christoph').toMatch(/stop/);
});

配列と反復可能オブジェクト

toContain を使用して、配列または反復可能オブジェクトに特定の項目が含まれているかどうかを確認できます。

const shoppingList = [
'diapers',
'kleenex',
'trash bags',
'paper towels',
'milk',
];

test('the shopping list has milk on it', () => {
expect(shoppingList).toContain('milk');
expect(new Set(shoppingList)).toContain('milk');
});

例外

特定の関数が呼び出されたときにエラーをスローするかどうかをテストする場合は、toThrow を使用します。

function compileAndroidCode() {
throw new Error('you are using the wrong JDK!');
}

test('compiling android goes as expected', () => {
expect(() => compileAndroidCode()).toThrow();
expect(() => compileAndroidCode()).toThrow(Error);

// You can also use a string that must be contained in the error message or a regexp
expect(() => compileAndroidCode()).toThrow('you are using the wrong JDK');
expect(() => compileAndroidCode()).toThrow(/JDK/);

// Or you can match an exact error message using a regexp like below
expect(() => compileAndroidCode()).toThrow(/^you are using the wrong JDK$/); // Test fails
expect(() => compileAndroidCode()).toThrow(/^you are using the wrong JDK!$/); // Test pass
});
ヒント

例外をスローする関数は、ラッピング関数内で呼び出す必要があります。そうでないと、toThrow アサーションは失敗します。

その他

これはほんの一例です。Matcher の完全なリストについては、リファレンスドキュメントを参照してください。

利用可能な Matcher について学習したら、Jest を使用して非同期コードをテストする方法を確認することをお勧めします。