tag:crieit.net,2005:https://crieit.net/tags/console.error/feed 「console.error」の記事 - Crieit Crieitでタグ「console.error」に投稿された最近の記事 2022-04-23T01:06:29+09:00 https://crieit.net/tags/console.error/feed tag:crieit.net,2005:PublicArticle/18173 2022-04-23T01:06:29+09:00 2022-04-23T01:06:29+09:00 https://crieit.net/posts/jest-operate-dom-and-ignore-console-error-20220425 Jest で DOM 操作の試験、 console.error を無視 <p>Jest によるテストコードを書く上でいくつか嵌まったポイントをピックアップ。</p> <h2 id="DOM 操作の試験"><a href="#DOM+%E6%93%8D%E4%BD%9C%E3%81%AE%E8%A9%A6%E9%A8%93">DOM 操作の試験</a></h2> <p>まず引っかかったのは DOM 操作。今回はバリバリ DOM を操作したりある要素が存在しているかのチェックをする、といった完全に DOM ありきのコードを対象にしていました。</p> <p>機能のユニットテストであれば直感的に分かるのですが、 DOM 操作って……。</p> <h3 id="sample/assert.js"><a href="#sample%2Fassert.js">sample/assert.js</a></h3> <pre><code class="js">const dom = ` <div id="hoge"></div> `; module.exports = dom; </code></pre> <h3 id="sample/exception.js"><a href="#sample%2Fexception.js">sample/exception.js</a></h3> <pre><code class="js">const dom = ` <!--<div id="hoge"></div>--> `; module.exports = dom; </code></pre> <h3><strong>tests</strong>/hoge.test.js</h3> <pre><code class="js">const hoge = require('../src/hoge'); const domAssert = require('../sample/assert'); const domException = require('../sample/exception'); test('method:hoge test:assert', () => { document.body.innerHTML = domAssert; const hogeDOM = document.querySelector('#hoge'); const hogeInstance = new hoge(); expect(hogeInstance.judge(hogeDOM)).toBe(true); }); test('method:hoge test:exception', () => { document.body.innerHTML = domException; const hogeDOM = document.querySelector('#hoge'); const hogeInstance = new hoge(); expect(hogeInstance.judge(hogeDOM)).toBe(false); }); </code></pre> <p>例えば <code>hoge</code> という <code>id</code>属性 が付与された DOM が存在するか否かのチェックをする <code>hoge</code>クラス に対して <code>judge()</code>メソッド をテストしたい場合。</p> <ol> <li>DOM となるHTMLタグのコードを JS の文字列としてセット</li> <li>それを <code>module.exports</code></li> <li>テストコードで <code>require()</code> し、コード内の <code>document.body.innerHTML</code> へ代入する</li> <li>後は通常の JS のように <code>document.querySelector</code> なり何なりで操作やチェックが可能。属性であれば <code>getAttribute()</code> してあげれば良い</li> </ol> <p>……わりと素直にできることが分かりました。 Jest すごい。</p> <h3 id="The error below may be caused by using the wrong test environment"><a href="#The+error+below+may+be+caused+by+using+the+wrong+test+environment">The error below may be caused by using the wrong test environment</a></h3> <p>さて、上述でわりと直感的に DOM 操作した上でテストコードを記述できる、と記載しましたがいざテストをしようとすると以下のエラーが発生しました (Jest が 27系 以上)。</p> <blockquote> <p>The error below may be caused by using the wrong test environment, see https://jestjs.io/docs/configuration#testenvironment-string.<br /> Consider using the "jsdom" test environment.</p> </blockquote> <p>これは Jest に DOM 操作用の設定が必要なためのようです。</p> <p>解決策としては2つ。</p> <h4 id="テストコード内に設定を記述"><a href="#%E3%83%86%E3%82%B9%E3%83%88%E3%82%B3%E3%83%BC%E3%83%89%E5%86%85%E3%81%AB%E8%A8%AD%E5%AE%9A%E3%82%92%E8%A8%98%E8%BF%B0">テストコード内に設定を記述</a></h4> <p>まずはテストコード内に設定を記述する方法。これはテストコードごとに設定を変更できる柔軟性がある一方、必要な場合は全てのテストコードで同じ記述をする必要があるため数が多いと面倒になる、というパターン。</p> <pre><code class="js">/** * @jest-environment jsdom */ </code></pre> <h4 id="jest.config.js"><a href="#jest.config.js">jest.config.js</a></h4> <p>もう1つは <code>jest.config.js</code> に記述する方法。こちらは一括指定ができるものの、テストコードごとの設定はできないので先程とは逆ですね。</p> <pre><code class="js">module.exports = { testEnvironment: 'jsdom', // 追加 coverageDirectory: 'coverage' }; </code></pre> <h2 id="console.error() を無視"><a href="#console.error%28%29+%E3%82%92%E7%84%A1%E8%A6%96">console.error() を無視</a></h2> <p>テスト対象のコードで例外等の際に <code>console.error()</code> を使用していると、そのコードに入ったときに Jest が止まってしまいます。</p> <p>そこで、 <code>console.error()</code> に遭遇しても Jest が止まらないようにする方法を。</p> <pre><code class="js">const hoge = require('../src/hoge'); const domException = require('../sample/exception'); test('method:fuga test:exception', () => { jest.spyOn(console, 'error').mockImplementation((mes) => { console.log(mes); }); document.body.innerHTML = domException; const hogeDOM = document.querySelector('#hoge'); const hogeInstance = new hoge(); expect(hogeInstance.fuga(hogeDOM)).toBe(false); }); </code></pre> <p>例えばこんな感じ。 <code>jest.spyOn(console, 'error') ...</code> を実際のコードを走らせる前に記述しておくことで <code>console.error()</code> で止まらなくなります。</p> <h2 id="参考"><a href="#%E5%8F%82%E8%80%83">参考</a></h2> <h3 id="DOM操作"><a href="#DOM%E6%93%8D%E4%BD%9C">DOM操作</a></h3> <ul> <li><a target="_blank" rel="nofollow noopener" href="https://crudzoo.com/blog/jest">JestでjQuery主体のJavaScriptをテストする① | Crudzoo</a></li> <li><a target="_blank" rel="nofollow noopener" href="https://jestjs.io/ja/docs/tutorial-jquery">DOM 操作 · Jest</a></li> <li><a target="_blank" rel="nofollow noopener" href="https://tech.excite.co.jp/entry/2021/12/10/100000">DOM操作をするjQueryコードをJestでテストする方法を勉強した話 - エキサイト TechBlog.</a></li> </ul> <h3 id="The error below may be caused by using the wrong test environment"><a href="#The+error+below+may+be+caused+by+using+the+wrong+test+environment">The error below may be caused by using the wrong test environment</a></h3> <ul> <li><a target="_blank" rel="nofollow noopener" href="https://qiita.com/mame_daifuku/items/79b6a5a1514a3f067e1a">Jestの「 The error below may be caused by using the wrong test environment」の解決方法 - Qiita</a></li> </ul> <h3 id="undefined 判定"><a href="#undefined+%E5%88%A4%E5%AE%9A">undefined 判定</a></h3> <ul> <li><a target="_blank" rel="nofollow noopener" href="https://jestjs.io/ja/docs/expect#tobeundefined">Expect · Jest</a></li> </ul> <h3 id="null 判定"><a href="#null+%E5%88%A4%E5%AE%9A">null 判定</a></h3> <ul> <li><a target="_blank" rel="nofollow noopener" href="https://jestjs.io/ja/docs/expect#tobenull">Expect · Jest</a></li> </ul> <h3 id="(未使用) 非同期"><a href="#%28%E6%9C%AA%E4%BD%BF%E7%94%A8%29+%E9%9D%9E%E5%90%8C%E6%9C%9F">(未使用) 非同期</a></h3> <ul> <li><a target="_blank" rel="nofollow noopener" href="https://jestjs.io/ja/docs/asynchronous">非同期コードのテスト · Jest</a></li> </ul> <h3 id="console.errorで止まらないように"><a href="#console.error%E3%81%A7%E6%AD%A2%E3%81%BE%E3%82%89%E3%81%AA%E3%81%84%E3%82%88%E3%81%86%E3%81%AB">console.errorで止まらないように</a></h3> <ul> <li><a target="_blank" rel="nofollow noopener" href="https://zenn.dev/nus3/scraps/f1ea3cb4982593">Jest 逆引き テストケース</a></li> <li><a target="_blank" rel="nofollow noopener" href="https://blog.kozakana.net/2021/02/dont-stop-with-console-error-when-running-tests-in-jest/">jestでテスト実行時console.errorで止まらないようにする | Simple is Beautiful.</a></li> <li><a target="_blank" rel="nofollow noopener" href="https://qiita.com/manten120/items/790de8e1612fc8a15355">【Jest】console.log()をmock化する方法2通り - Qiita</a></li> </ul> arm-band