<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Mocha &#8211; My Shitty Code</title>
	<atom:link href="https://myshittycode.com/tag/mocha/feed/" rel="self" type="application/rss+xml" />
	<link>https://myshittycode.com</link>
	<description>Embracing the Messiness in Search of Epic Solutions</description>
	<lastBuildDate>Fri, 06 Jan 2023 16:27:39 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>

<image>
	<url>https://myshittycode.com/wp-content/uploads/2022/04/cropped-icon-32x32.png</url>
	<title>Mocha &#8211; My Shitty Code</title>
	<link>https://myshittycode.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">205304208</site>	<item>
		<title>ES6 + Mocha + Sinon: Mocking Imported Dependency</title>
		<link>https://myshittycode.com/2017/05/10/es6-mocha-sinon-mocking-imported-dependency/</link>
					<comments>https://myshittycode.com/2017/05/10/es6-mocha-sinon-mocking-imported-dependency/#comments</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Wed, 10 May 2017 20:46:35 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[ES6]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Mocha]]></category>
		<category><![CDATA[Sinon]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=1014</guid>

					<description><![CDATA[<p>PROBLEM Let&#8217;s assume we have the following 2 files:- apis.js service.js Let&#8217;s assume we want to test the logic in service.js without using nock to mock the HTTP call in apis.js. While proxyquireify allows us to mock out the apis.js dependency in service.js, sometimes it is a little more complicated than needed. SOLUTION A simpler [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2017/05/10/es6-mocha-sinon-mocking-imported-dependency/">ES6 + Mocha + Sinon: Mocking Imported Dependency</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">PROBLEM</h2>



<p>Let&#8217;s assume we have the following 2 files:-</p>



<p><b><b>apis.js</b></b></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
import fetch from &#039;isomorphic-fetch&#039;;

export const logout = () =&gt; (
  fetch(&#039;/logout&#039;)
    .then(resp =&gt; resp.json())
    .catch(err =&gt; err)
);
</pre></div>


<p><b><b>service.js</b></b></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
import { logout } from &#039;./apis&#039;;

export const kickUserOut = activeSession =&gt; (
  activeSession ? logout() : undefined
);
</pre></div>


<p>Let&#8217;s assume we want to test the logic in <b>service.js</b> without using <a href="https://github.com/node-nock/nock" target="_blank" rel="noopener">nock</a> to mock the HTTP call in <b>apis.js</b>.</p>



<p>While <a href="https://github.com/thlorenz/proxyquireify" target="_blank" rel="noopener">proxyquireify</a> allows us to mock out the <b>apis.js</b> dependency in <b>service.js</b>, sometimes it is a little more complicated than needed.</p>



<h2 class="wp-block-heading">SOLUTION</h2>



<p>A simpler approach is to use <a href="https://github.com/sinonjs/sinon" target="_blank" rel="noopener">sinon</a> to stub out <b>logout()</b> defined in <b>apis.js</b>.</p>



<p><b><b>service-spec.js</b></b></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
import { beforeEach, afterEach, describe, it } from &#039;mocha&#039;;
import { expect } from &#039;chai&#039;;
import sinon from &#039;sinon&#039;;
import { kickUserOut } from &#039;./service&#039;;

// import everything as an object
import * as apis from &#039;./apis&#039;;

describe(&#039;service =&gt; kickUserOut&#039;, () =&gt; {
  let logoutStub;

  // before running each test, stub out `logout()`
  beforeEach(() =&gt; {
    logoutStub = sinon.stub(apis, &#039;logout&#039;).returns(&#039;success&#039;);
  });

  // after running each test, restore to the original method to
  // prevent &quot;TypeError: Attempted to wrap logout which is already wrapped&quot;
  // error when executing subsequent specs.
  afterEach(() =&gt; {
    apis.logout.restore();
  });

  it(&#039;given active session, should invoke logout API&#039;, () =&gt; {
    expect(kickUserOut(true)).to.deep.equal(&#039;success&#039;);
    expect(logoutStub.calledOnce).to.equal(true);
  });

  it(&#039;given expired session, should not invoke logout API&#039;, () =&gt; {
    expect(kickUserOut(false)).to.equal(undefined);
    expect(logoutStub.calledOnce).to.equal(false);
  });
});
</pre></div><p>The post <a rel="nofollow" href="https://myshittycode.com/2017/05/10/es6-mocha-sinon-mocking-imported-dependency/">ES6 + Mocha + Sinon: Mocking Imported Dependency</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2017/05/10/es6-mocha-sinon-mocking-imported-dependency/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1014</post-id>	</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Lazy Loading (feed)
Database Caching 7/65 queries in 0.016 seconds using Disk

Served from: myshittycode.com @ 2026-02-18 01:15:32 by W3 Total Cache
-->