[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>&#x3C;<span class="hljs-built_in">string</span>> { <span class="hljs-…

kentaro.app
Related Topics: HTTP Node.js
1 comments
  • msw最近良く見るよねぇ。サーバのモッキングがそんなに簡単にできるなら色々考えてみたいな