a-pompom / Knowledge

各種技術の知識メモ
0 stars 0 forks source link

JestドキュメントのUsing Matchersを読みたい #6

Open a-pompom opened 6 months ago

a-pompom commented 6 months ago

https://jestjs.io/docs/using-matchers

a-pompom commented 5 months ago

いわゆるassertionの表現方法がmatcher。 オブジェクトや配列など、テスト結果の検証方法を紹介。

Common Matchers

https://jestjs.io/docs/using-matchers#common-matchers

expect()の戻り値はExpectationオブジェクト。基本的にはmatcherを呼び出すものと捉えて良さそう。

toBe()Object.is()を内部で呼び出している。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is

===とほぼ等価で、符号付き0とNaNの扱いのみ異なる。 オブジェクトや配列を比較したい場合は、toEqual()を呼び出す。toEqual()はオブジェクトを再帰的に探索してくる。

Truthiness

https://jestjs.io/docs/using-matchers#truthiness

null, undefined, true, falseを区別するためのmatcher。 使い分けは明確だからテストケースに応じてmatcherを切り替えれば良さそう。

Numbers

https://jestjs.io/docs/using-matchers#numbers

いわゆる数値比較のためのmatcher。 条件式の評価結果をtoBe()で検証することでも代用できそう。

Strings

https://jestjs.io/docs/using-matchers#strings

正規表現を比較するためのmatcher。

Arrays and Iterables

https://jestjs.io/docs/using-matchers#arrays-and-iterables

toContain()でIterableが要素を含むか判定。

Exceptions

https://jestjs.io/docs/using-matchers#exceptions

toThrow()は例外が送出されたか検証できる。


ここまで見てきたものは、ほかのテスティングフレームワークと似たような書き方。 必要に応じてドキュメントに戻ってくれば良さそう。