<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Arivu on Medium]]></title>
        <description><![CDATA[Stories by Arivu on Medium]]></description>
        <link>https://medium.com/@arivu_a?source=rss-ddadbe82f2c4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*oeIAAD4-042R74KA.jpeg</url>
            <title>Stories by Arivu on Medium</title>
            <link>https://medium.com/@arivu_a?source=rss-ddadbe82f2c4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 14:19:34 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@arivu_a/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to mock functions globally in jest]]></title>
            <link>https://medium.com/@arivu_a/how-to-mock-functions-globally-in-jest-f267fedf7683?source=rss-ddadbe82f2c4------2</link>
            <guid isPermaLink="false">https://medium.com/p/f267fedf7683</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[jest]]></category>
            <category><![CDATA[mocking]]></category>
            <dc:creator><![CDATA[Arivu]]></dc:creator>
            <pubDate>Mon, 13 May 2019 17:07:22 GMT</pubDate>
            <atom:updated>2019-05-15T00:41:50.205Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1022/1*4kaNb-WLyEGIEe5IffS_4A.png" /></figure><p>There are times where we need to mock functions like <a href="https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API">fetch</a>, etc., globally instead of mocking individually in each file. NPM modules like <a href="https://www.npmjs.com/package/jest-fetch-mock">jest-fetch-mock</a>, etc., are there for rescue to mock these globally. This article will help you get a feel of how to mock functions globally without the use of any npm package. With this approach, you can mock any API globally that you need to.</p><p>Below code example shows how to mock fetch API globally in jest.</p><p>fileToTest.js has the sample code which we are planning to test.</p><pre>// fileToTest.js</pre><pre>export const callFetch = (url) =&gt; {<br>  fetch(url).then(<br>    response =&gt; response.json()<br>  )<br>}</pre><p>setupJestMock.js file will contain jest mock function with the assumption to resolve JSON object.</p><pre>// setupJestMock.js</pre><pre>/* global jest */<br>const mockFetchPromise = Promise.resolve({<br>  json: () =&gt; Promise.resolve({}),<br>});<br>global.fetch = jest.fn(() =&gt; mockFetchPromise);</pre><p>Now linksetupJestMock.js in your package.json</p><pre>// package.json</pre><pre>{<br>  &quot;name&quot;:&quot;jest-mock-setup&quot;,<br>  &quot;version&quot;: &quot;1.0.0,<br>  &quot;description&quot;: &quot;jest mock setup configuration&quot;,<br>  &quot;jest&quot;: {<br>    &quot;automock&quot;: false,<br>    &quot;setupFiles&quot;: [<br>      &quot;./setupJestMock.js&quot;<br>    ]<br>  }<br>}</pre><p><strong>Thats it!!..</strong> fetch is mocked globally. You are free to write test cases like the below in order to test fetch anywhere in your code.</p><pre>// fileToTest.spec.js</pre><pre>import { callFetch } from &#39;./filetoTest.js&#39;;</pre><pre>describe(&#39;jest global fetch test cases&#39;, () =&gt; {<br>  it(&#39;check global fetch been called&#39;, () =&gt; {<br>    callFetch(&#39;http://some.api.to.respond.json.com&#39;);<br>    expect(global.fetch).toHaveBeenCalledTimes(1);<br>  });<br>});</pre><p>// please leave me a comment if this is not correct for some reason</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f267fedf7683" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>