<?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 Nitin Pamnani on Medium]]></title>
        <description><![CDATA[Stories by Nitin Pamnani on Medium]]></description>
        <link>https://medium.com/@nitinpamnani002?source=rss-804aa648e058------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*iXNJGEXuhrpGUzo7.jpg</url>
            <title>Stories by Nitin Pamnani on Medium</title>
            <link>https://medium.com/@nitinpamnani002?source=rss-804aa648e058------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 15:56:06 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@nitinpamnani002/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[Typescipt | NodeJs | Unit Testing : axios-retry using sinon stubs]]></title>
            <link>https://medium.com/@nitinpamnani002/typescipt-nodejs-unit-testing-axios-retry-using-sinon-stubs-c756cfe187fe?source=rss-804aa648e058------2</link>
            <guid isPermaLink="false">https://medium.com/p/c756cfe187fe</guid>
            <category><![CDATA[unit-testing]]></category>
            <category><![CDATA[sinonjs]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[Nitin Pamnani]]></dc:creator>
            <pubDate>Mon, 01 Sep 2025 20:54:49 GMT</pubDate>
            <atom:updated>2025-09-02T10:58:12.628Z</atom:updated>
            <content:encoded><![CDATA[<h3>Typescript | NodeJs | Unit Testing : testing axios-retry using sinon stubs</h3><p>I got stuck with a problem where I had to write a test case for retry strategy added to our axios instance factory class.</p><p>To make the implementation simple, I chose to avoid lightweight server creation and attempted to stub axios instance. There was a catch though, axios-retryworks as an interceptor and it wraps the part of axios that actually makes the network requests. If one uses sinon to stub a high level method like axios.get() , the method gets replaced before axios-retry even gets a chance to see the request.</p><p><strong>Solution:<br></strong>Think of a “Chain of Command” system. Example a school.<br>1. The Principal ( axios.get() ) issues orders.<br>2. The Teacher ( axios-retry ) checks if the orders were carried out or not. If not then attempt again.<br>3. The Student ( axios adapter ) actually goes out to perform the orders.</p><p>Stubbing the principal brings down the entire hierarchy and the teacher or student never receive any orders.</p><p><strong>Code snippet below showcasing the solution in action.</strong></p><pre>// A sample test case<br>describe(&#39;axios-retry with Sinon Adapter Stub&#39;, () =&gt; {<br>            let axiosInstance;<br>            let adapterStub;<br><br>            beforeEach(() =&gt; {<br><br>                // 1. THE KEY: Stub the default adapter, not the &#39;get&#39; method<br>                adapterStub = sinon.stub();<br><br>                // 2. Create and configure your client as you normally would<br>                const axiosConfig = {<br>                  adapter: adapterStub<br>                }<br>                // ClientBuilder is a factory class where I am building axios instances with axios-retry<br>                axiosInstance = ClientBuilder.build(axiosConfig)<br>            });<br>            <br>            afterEach(() =&gt; {<br>                adapterStub.restore();<br>            });<br><br>            it(&#39;should retry on a 503 error and succeed on the third attempt&#39;, async () =&gt; {<br>                // 3. Program the adapter stub&#39;s behavior<br>                const failureResponse = { response: { status: 503, data: &#39;Service Unavailable&#39; } };<br>                const successData = { message: &#39;Success!&#39; };<br>                <br>                // Note: The adapter resolves with a full response object structure<br>                adapterStub.onFirstCall().rejects(failureResponse);<br>                adapterStub.onSecondCall().rejects(failureResponse);<br>                adapterStub.onThirdCall().resolves({<br>                    data: successData,<br>                    status: 200,<br>                    statusText: &#39;OK&#39;,<br>                    headers: {},<br>                    config: {},<br>                });<br><br>                // 4. Make the call using the high-level method (e.g., .get)<br>                const response = await axiosInstance.get(&#39;/flaky-service&#39;);<br><br>                // 5. Assert the final response is the successful one<br>                expect(response.status).to.equal(200);<br>                expect(response.data).to.deep.equal(successData);<br><br>                // 6. Assert that the adapter was called 3 times, proving the retries happened<br>                expect(adapterStub.callCount).to.equal(3);<br>                console.log(&#39;Adapter was called 3 times, proving axios-retry is working with the stub.&#39;);<br>            });<br>        });</pre><p>Hope this helps!!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c756cfe187fe" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>