React で作ったアプリを Jest + enzyme でテストしている時にTypeError: Cannot read property 'equal' of undefined
というエラーになったのでメモ
enzymeのAPIリファレンスを見ながらこんなテストを書いていました。
describe("App", ()=> { const app = shallow(<App />); // 中略 it('count is 0', () => { expect( app.find('.count').text() ).to.equal('0'); }); });
これをテストすると…
TypeError: Cannot read property 'equal' of undefined 27 | it('count is 0', () => { > 28 | expect(app.find('.count').text()).to.equal("0"); | ^ 29 | });
テスト以前の問題として equal
が無いよというエラーになってしまいました。
to.equal
は chai の構文。Jest なら .toEqual
を使う
That's not an Enzyme problem.
expect(...).to~
isundefined
because you have installed expect.js and you are using chai syntax.
ref. javascript - TypeError: Cannot read property 'equal' of undefined - Stack Overflow
という事のようです。
enzymeのAPIリファレンス にあるサンプルコードは expect
の部分が基本的に .to.〜
の構文で書かれていますが、この部分は enzyme ではないので Jest なら Jest の構文に置き換えて読んでくれという事のようです。
なので、先のテストは Jest を使ったテストの場合は .toEqual()
に置き換えればOK
describe("App", ()=> { const app = shallow(<App />); // 中略 it('count is 0', () => { expect( app.find('.count').text() ).toEqual('0'); }); });
👇
PASS src/__tests__/App.test.js
( 'ω' و( و"♪ イェーイ
モダンフロントエンド色んなライブラリを合わせて使うから、該当部分がどのライブラリのものなのか把握しておかないと結構ハマる…
node学園で聴いた npm / yarn zero install の仕組み導入されるの楽しみ
[参考]
- Jest · 🃏 Delightful JavaScript Testing
- Introduction · Enzyme
- javascript - TypeError: Cannot read property 'equal' of undefined - Stack Overflow
- Chai

- 作者: 掌田津耶乃
- 出版社/メーカー: 秀和システム
- 発売日: 2019/03/08
- メディア: 単行本
- この商品を含むブログを見る

- 出版社/メーカー: 東洋ベバレッジ
- メディア: 食品&飲料
- 購入: 1人 クリック: 17回
- この商品を含むブログを見る
これは放課後チャイタイム