![](https://og-image-lake-nine.vercel.app/**%5Bmsw,%20Jest,%20Node.js%5D%20msw%E3%82%92%E5%88%A9%E7%94%A8%E3%81%97%E3%81%A6HTTP%E5%A2%83%E7%95%8C%E3%81%AE%E3%83%86%E3%82%B9%E3%83%88%E3%82%92%E6%9B%B8%E3%81%8F**.png?md=1&fontSize=100px)
[msw, Jest, Node.js] mswを利用してHTTP境界のテストを書く
CRANK
<p>JestでHTTPリクエストをインターセプトしてモックを返すのに利用した<a href=" Service Worker(msw)</a>の紹介をしたいと思います。環境はNodeです。</p> <h2>テスト対象の実装</h2> <p><code> からデータを取得する、という想定の実装です。</p> <pre><code class="hljs language-ts"><span class="hljs-keyword">import</span> fetch <span class="hljs-keyword">from</span> <span class="hljs-string">'isomorphic-unfetch'</span>; <span class="hljs-keyword">export</span> <span class="hljs-keyword">class</span> <span class="hljs-title hljs-class">GetExampleError</span> <span class="hljs-keyword">extends</span> <span class="hljs-title hljs-class hljs-inherited">Error</span> {} <span class="hljs-keyword">export</span> <span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title hljs-function">getExamples</span>(<span class="hljs-params"></span>): <span class="hljs-title hljs-class">Promise</span><<span class="hljs-built_in">string</span>> { <span class="hljs-…