Skip to content

Commit

Permalink
Automated push - build 2619 [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
CircleCI committed Oct 28, 2019
1 parent f2014a9 commit cffc70b
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 21 deletions.
4 changes: 2 additions & 2 deletions docs/index.html
Expand Up @@ -98,7 +98,7 @@
<input class="md-toggle" data-md-toggle="search" type="checkbox" id="__search" autocomplete="off">
<label class="md-overlay" data-md-component="overlay" for="__drawer"></label>

<a href="#stark-docs" tabindex="1" class="md-skip">
<a href="#docs" tabindex="1" class="md-skip">
Skip to content
</a>

Expand Down Expand Up @@ -518,7 +518,7 @@
<a href="https://github.com/rcmedeiros/template/edit/develop/mkdocs/index.md" title="Edit this page" class="md-icon md-content__icon">&#xE3C9;</a>


<h1 id="stark-docs">Stark Docs</h1>
<h1 id="docs">Docs</h1>
<p>At first I had this project privately to myself where I could copy-and-paste already sorted out configurations to other projects, I think everyone has one of those. Now it&rsquo;s public and others can take advantage of it.</p>
<p>Docs were written for coders in a rush, but I&rsquo;ll try to balance the amount of information not to be too vague. I just want you to copy-and-paste something that actually works without too much reading. References to full documentation are always included for deep dive later on with time.</p>

Expand Down
102 changes: 99 additions & 3 deletions docs/mocks/index.html
Expand Up @@ -98,7 +98,7 @@
<input class="md-toggle" data-md-toggle="search" type="checkbox" id="__search" autocomplete="off">
<label class="md-overlay" data-md-component="overlay" for="__drawer"></label>

<a href="#sinon" tabindex="1" class="md-skip">
<a href="#mocks" tabindex="1" class="md-skip">
Skip to content
</a>

Expand Down Expand Up @@ -382,10 +382,39 @@



<label class="md-nav__link md-nav__link--active" for="__toc">
Mocks
</label>

<a href="./" title="Mocks" class="md-nav__link md-nav__link--active">
Mocks
</a>


<nav class="md-nav md-nav--secondary">





<label class="md-nav__title" for="__toc">Table of contents</label>
<ul class="md-nav__list" data-md-scrollfix>

<li class="md-nav__item">
<a href="#sinon" title="Sinon" class="md-nav__link">
Sinon
</a>

</li>





</ul>

</nav>

</li>


Expand Down Expand Up @@ -507,6 +536,22 @@



<label class="md-nav__title" for="__toc">Table of contents</label>
<ul class="md-nav__list" data-md-scrollfix>

<li class="md-nav__item">
<a href="#sinon" title="Sinon" class="md-nav__link">
Sinon
</a>

</li>





</ul>

</nav>
</div>
</div>
Expand All @@ -520,8 +565,59 @@
<a href="https://github.com/rcmedeiros/template/edit/develop/mkdocs/mocks.md" title="Edit this page" class="md-icon md-content__icon">&#xE3C9;</a>


<h1 id="sinon">Sinon</h1>
<p>TBD&hellip;</p>
<h1 id="mocks">Mocks</h1>
<h2 id="sinon">Sinon</h2>
<p>Maybe your software integrates with other APIs, probably it connects to a database, and such resources won&rsquo;t/shouldn&rsquo;t be available for testing. For those situations, try <a href="https://sinonjs.org/">sinon.js</a>. There are lots of convenient ways to mock resources and all of those can be found in the documentation, but the one I use the most is replacing my http client request.</p>
<div class="codehilite" style="background: #272822"><pre style="line-height: 125%"><span></span><span style="color: #66d9ef">import</span> <span style="color: #a6e22e">needle</span> <span style="color: #a6e22e">from</span> <span style="color: #e6db74">&#39;needle&#39;</span><span style="color: #f8f8f2">;</span>
<span style="color: #66d9ef">import</span> <span style="color: #a6e22e">sinon</span> <span style="color: #a6e22e">from</span> <span style="color: #e6db74">&#39;sinon&#39;</span><span style="color: #f8f8f2">;</span>
<span style="color: #66d9ef">import</span> <span style="color: #f8f8f2">{</span> <span style="color: #a6e22e">FakeNeedle</span> <span style="color: #f8f8f2">}</span> <span style="color: #a6e22e">from</span> <span style="color: #e6db74">&#39;./mocks/needle/fake-needle&#39;</span><span style="color: #f8f8f2">;</span>

<span style="color: #a6e22e">sinon</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">replace</span><span style="color: #f8f8f2">(</span><span style="color: #a6e22e">needle</span><span style="color: #f8f8f2">,</span> <span style="color: #e6db74">&#39;request&#39;</span><span style="color: #f8f8f2">,</span> <span style="color: #a6e22e">sinon</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">fake</span><span style="color: #f8f8f2">(</span><span style="color: #66d9ef">new</span> <span style="color: #a6e22e">FakeNeedle</span><span style="color: #f8f8f2">().</span><span style="color: #a6e22e">request</span><span style="color: #f8f8f2">));</span>
</pre></div>


<p>In this example, I switched <a href="https://github.com/tomas/needle#readme">needle</a>&lsquo;s request function for a fake one of mine, that will be called unbeknownst to the code. This way I can control exactly what happens during tests with HTTP calls to other APIs.</p>
<div class="codehilite" style="background: #272822"><pre style="line-height: 125%"><span></span><span style="color: #66d9ef">export</span> <span style="color: #66d9ef">class</span> <span style="color: #a6e22e">FakeNeedle</span> <span style="color: #f8f8f2">{</span>
<span style="color: #66d9ef">private</span> <span style="color: #a6e22e">readonly</span> <span style="color: #a6e22e">baseRequest</span>: <span style="color: #66d9ef">unknown</span><span style="color: #f8f8f2">;</span>
<span style="color: #66d9ef">constructor</span><span style="color: #f8f8f2">()</span> <span style="color: #f8f8f2">{</span>
<span style="color: #75715e">// This is a real request object that I stringified to a JSON</span>
<span style="color: #66d9ef">this</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">baseRequest</span> <span style="color: #f92672">=</span> <span style="color: #a6e22e">JSON</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">parse</span><span style="color: #f8f8f2">(</span>
<span style="color: #a6e22e">fs</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">readFileSync</span><span style="color: #f8f8f2">(</span><span style="color: #e6db74">&#39;test/mocks/needle/request.json&#39;</span><span style="color: #f8f8f2">).</span><span style="color: #a6e22e">toString</span><span style="color: #f8f8f2">());</span>
<span style="color: #f8f8f2">}</span>
<span style="color: #75715e">// This is the method that will be used instead of the real one</span>
<span style="color: #66d9ef">public</span> <span style="color: #a6e22e">request</span> <span style="color: #f92672">=</span> <span style="color: #f8f8f2">(</span><span style="color: #a6e22e">method</span>: <span style="color: #66d9ef">NeedleHttpVerbs</span><span style="color: #f8f8f2">,</span> <span style="color: #a6e22e">url</span>: <span style="color: #66d9ef">string</span><span style="color: #f8f8f2">,</span> <span style="color: #a6e22e">data</span>: <span style="color: #66d9ef">BodyData</span><span style="color: #f8f8f2">,</span>
<span style="color: #a6e22e">_options?</span>: <span style="color: #66d9ef">NeedleOptions</span><span style="color: #f8f8f2">,</span>
<span style="color: #a6e22e">callback?</span>: <span style="color: #66d9ef">NeedleCallback</span><span style="color: #f8f8f2">)</span><span style="color: #f92672">:</span> <span style="color: #a6e22e">ReadableStream</span> <span style="color: #f92672">=&gt;</span> <span style="color: #f8f8f2">{</span>

<span style="color: #75715e">// It looks for a file keyed by the URL and parameters received.</span>
<span style="color: #66d9ef">const</span> <span style="color: #a6e22e">fileName</span>: <span style="color: #66d9ef">string</span> <span style="color: #f92672">=</span> <span style="color: #a6e22e">crypto</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">createHash</span><span style="color: #f8f8f2">(</span><span style="color: #e6db74">&#39;md5&#39;</span><span style="color: #f8f8f2">)</span>
<span style="color: #f8f8f2">.</span><span style="color: #a6e22e">update</span><span style="color: #f8f8f2">(</span><span style="color: #a6e22e">url</span> <span style="color: #f92672">+</span> <span style="color: #a6e22e">JSON</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">stringify</span><span style="color: #f8f8f2">(</span><span style="color: #a6e22e">data</span><span style="color: #f8f8f2">)).</span><span style="color: #a6e22e">digest</span><span style="color: #f8f8f2">(</span><span style="color: #e6db74">&#39;hex&#39;</span><span style="color: #f8f8f2">)</span> <span style="color: #f92672">+</span> <span style="color: #e6db74">&#39;.json&#39;</span><span style="color: #f8f8f2">;</span>

<span style="color: #66d9ef">const</span> <span style="color: #a6e22e">filePath</span>: <span style="color: #66d9ef">string</span> <span style="color: #f92672">=</span> <span style="color: #a6e22e">path</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">join</span><span style="color: #f8f8f2">(</span><span style="color: #e6db74">&#39;test/mocks/needle&#39;</span><span style="color: #f8f8f2">,</span> <span style="color: #a6e22e">url</span><span style="color: #f8f8f2">,</span> <span style="color: #a6e22e">fileName</span><span style="color: #f8f8f2">);</span>
<span style="color: #75715e">// If the file is not found, your test is broken</span>
<span style="color: #66d9ef">if</span> <span style="color: #f8f8f2">(</span><span style="color: #f92672">!</span><span style="color: #a6e22e">fs</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">existsSync</span><span style="color: #f8f8f2">(</span><span style="color: #a6e22e">filePath</span><span style="color: #f8f8f2">))</span> <span style="color: #f8f8f2">{</span>
<span style="color: #a6e22e">console</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">debug</span><span style="color: #f8f8f2">(</span><span style="color: #e6db74">&#39;method: &#39;</span> <span style="color: #f92672">+</span> <span style="color: #a6e22e">method</span><span style="color: #f8f8f2">);</span>
<span style="color: #a6e22e">console</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">debug</span><span style="color: #f8f8f2">(</span><span style="color: #e6db74">&#39;call &#39;</span> <span style="color: #f92672">+</span> <span style="color: #a6e22e">url</span><span style="color: #f8f8f2">);</span>
<span style="color: #a6e22e">console</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">debug</span><span style="color: #f8f8f2">(</span><span style="color: #e6db74">&#39;data &#39;</span> <span style="color: #f92672">+</span> <span style="color: #a6e22e">JSON</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">stringify</span><span style="color: #f8f8f2">(</span><span style="color: #a6e22e">data</span><span style="color: #f8f8f2">));</span>
<span style="color: #a6e22e">console</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">debug</span><span style="color: #f8f8f2">(</span><span style="color: #e6db74">&#39;path: &#39;</span> <span style="color: #f92672">+</span> <span style="color: #a6e22e">fileName</span><span style="color: #f8f8f2">);</span>
<span style="color: #a6e22e">process</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">exit</span><span style="color: #f8f8f2">(</span><span style="color: #ae81ff">0</span><span style="color: #f8f8f2">);</span>
<span style="color: #f8f8f2">}</span> <span style="color: #66d9ef">else</span> <span style="color: #f8f8f2">{</span>
<span style="color: #75715e">// Based on the URL and parameters, a preset response is returned</span>
<span style="color: #66d9ef">const</span> <span style="color: #a6e22e">body</span>: <span style="color: #66d9ef">unknown</span> <span style="color: #f92672">=</span> <span style="color: #a6e22e">JSON</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">parse</span><span style="color: #f8f8f2">(</span><span style="color: #a6e22e">fs</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">readFileSync</span><span style="color: #f8f8f2">(</span><span style="color: #a6e22e">filePath</span><span style="color: #f8f8f2">).</span><span style="color: #a6e22e">toString</span><span style="color: #f8f8f2">());</span>

<span style="color: #66d9ef">const</span> <span style="color: #a6e22e">request</span>: <span style="color: #66d9ef">NeedleResponse</span> <span style="color: #f92672">=</span> <span style="color: #a6e22e">JSON</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">parse</span><span style="color: #f8f8f2">(</span>
<span style="color: #a6e22e">JSON</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">stringify</span><span style="color: #f8f8f2">(</span><span style="color: #66d9ef">this</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">baseRequest</span><span style="color: #f8f8f2">));</span>
<span style="color: #a6e22e">request</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">body</span> <span style="color: #f92672">=</span> <span style="color: #a6e22e">body</span><span style="color: #f8f8f2">;</span>

<span style="color: #a6e22e">callback</span><span style="color: #f8f8f2">(</span><span style="color: #66d9ef">undefined</span><span style="color: #f8f8f2">,</span> <span style="color: #a6e22e">request</span><span style="color: #f8f8f2">,</span> <span style="color: #a6e22e">body</span><span style="color: #f8f8f2">);</span>
<span style="color: #f8f8f2">}</span>
<span style="color: #66d9ef">return</span> <span style="color: #66d9ef">undefined</span><span style="color: #f8f8f2">;</span>
<span style="color: #f8f8f2">}</span>
<span style="color: #f8f8f2">}</span>
</pre></div>


<p>That it! Of course you can also give files a more intuitive name and choose them inside a switch statement, your call!</p>



Expand Down
2 changes: 1 addition & 1 deletion docs/search/search_index.json

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions docs/sitemap.xml
Expand Up @@ -2,67 +2,67 @@
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>None</loc>
<lastmod>2019-05-19</lastmod>
<lastmod>2019-10-28</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>None</loc>
<lastmod>2019-05-19</lastmod>
<lastmod>2019-10-28</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>None</loc>
<lastmod>2019-05-19</lastmod>
<lastmod>2019-10-28</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>None</loc>
<lastmod>2019-05-19</lastmod>
<lastmod>2019-10-28</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>None</loc>
<lastmod>2019-05-19</lastmod>
<lastmod>2019-10-28</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>None</loc>
<lastmod>2019-05-19</lastmod>
<lastmod>2019-10-28</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>None</loc>
<lastmod>2019-05-19</lastmod>
<lastmod>2019-10-28</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>None</loc>
<lastmod>2019-05-19</lastmod>
<lastmod>2019-10-28</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>None</loc>
<lastmod>2019-05-19</lastmod>
<lastmod>2019-10-28</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>None</loc>
<lastmod>2019-05-19</lastmod>
<lastmod>2019-10-28</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>None</loc>
<lastmod>2019-05-19</lastmod>
<lastmod>2019-10-28</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>None</loc>
<lastmod>2019-05-19</lastmod>
<lastmod>2019-10-28</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>None</loc>
<lastmod>2019-05-19</lastmod>
<lastmod>2019-10-28</lastmod>
<changefreq>daily</changefreq>
</url>
</urlset>
Binary file modified docs/sitemap.xml.gz
Binary file not shown.
4 changes: 2 additions & 2 deletions docs/tests/index.html
Expand Up @@ -600,7 +600,7 @@ <h2 id="mocha">Mocha</h2>
<h2 id="ts-node">TS-Node</h2>
<p>Mocha is a JavaScript testing framework, and the code to test is written in TypeScript. To make this work, we use ts-node, a TypeScript execution and <abbr title="read–eval–print loop">REPL</abbr> for node.js. In short, it transpiles the code on-the-fly, no need to build-ts or watch-ts. All you have to do is to add <code>--require ts-node/register</code> as a command line parameter.</p>
<h2 id="chai">Chai</h2>
<p>To make the tests clearer and semantically pleasant, we use Chai. it&rsquo;s an assertion library and has the sole function of turning this:</p>
<p>To make the tests semantically pleasant, we use Chai. it&rsquo;s an assertion library and has the sole function of turning this:</p>
<div class="codehilite" style="background: #272822"><pre style="line-height: 125%"><span></span><span style="color: #a6e22e">assert</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">equal</span><span style="color: #f8f8f2">(</span><span style="color: #a6e22e">res</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">status</span><span style="color: #f8f8f2">,</span> <span style="color: #ae81ff">200</span><span style="color: #f8f8f2">);</span>
</pre></div>

Expand All @@ -612,7 +612,7 @@ <h2 id="chai">Chai</h2>
</pre></div>


<p>It may look foolish, but after a couple hundred tests, code that looks like description helps a lot.</p>
<p>It may look foolish, but after a couple hundred tests, semantically pleasant code makes a difference.</p>



Expand Down

0 comments on commit cffc70b

Please sign in to comment.