Open a-pompom opened 6 months ago
いわゆるassertionの表現方法がmatcher。 オブジェクトや配列など、テスト結果の検証方法を紹介。
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()
はオブジェクトを再帰的に探索してくる。
https://jestjs.io/docs/using-matchers#truthiness
null, undefined, true, falseを区別するためのmatcher。 使い分けは明確だからテストケースに応じてmatcherを切り替えれば良さそう。
https://jestjs.io/docs/using-matchers#numbers
いわゆる数値比較のためのmatcher。
条件式の評価結果をtoBe()
で検証することでも代用できそう。
https://jestjs.io/docs/using-matchers#strings
正規表現を比較するためのmatcher。
https://jestjs.io/docs/using-matchers#arrays-and-iterables
toContain()
でIterableが要素を含むか判定。
https://jestjs.io/docs/using-matchers#exceptions
toThrow()
は例外が送出されたか検証できる。
ここまで見てきたものは、ほかのテスティングフレームワークと似たような書き方。 必要に応じてドキュメントに戻ってくれば良さそう。
https://jestjs.io/docs/using-matchers