JestでTypeError: Cannot read property 'have' of undefinedが出てしまった時
JSのテストフレームワークのJest
とテストユーティリティEnzyme
で Reactの、とあるコンポーネントが子コンポーネントをもっているかどうかをテストするコードを書いていたら、テスト実行時に以下のエラーに遭遇した。
TypeError: Cannot read property 'have' of undefined
14 |
15 | //
> 16 | expect(wrapper.find(Foo)).to.have.length(1)
| ^
17 | })
18 |
at Object.<anonymous> (src/App.test.js:16:3)
EnzymeのEnzyme - API Referenceをみても、to.have.
という書き方が掲載されていたのに、haveがないと言われてしまうので調べて見た。
原因
結論からいうと、上記のメソッドはChainのアサーションの書き方で、自分はChaiを利用してなかった為に起こったものだった。
を良く見てなかったが、以下のような記述がある。
Enzyme is unopinionated regarding which test runner or assertion library you use, and should be compatible with all major test runners and assertion libraries out there. The documentation and examples for enzyme use mocha and chai, but you should be able to extrapolate to your framework of choice.
このドキュメントは、要するにアサーションライブラリとして mochaとchaiを利用しているため、Chai
のチェーン可能なメソッドで書かれていたのだった。
したがって、自分が確認すべきドキュメントは こちらで以下のように書くべきであった。
expect(wrapper.find(Foo).length).toBe(1)
参考
Author And Source
この問題について(JestでTypeError: Cannot read property 'have' of undefinedが出てしまった時), 我々は、より多くの情報をここで見つけました https://qiita.com/gp333/items/be37b6dfeed23f1e8748著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .