<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>rickschubert.net blog</title><link>https://rickschubert.net/blog/</link><description>rickschubert.net blog</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Fri, 19 Apr 2024 16:22:41 +0100</lastBuildDate><atom:link href="https://rickschubert.net/blog/index.xml" rel="self" type="application/rss+xml"/><item><title>JavaScript, the perfect programming language for the QA domain</title><link>https://rickschubert.net/blog/posts/javascript-for-qas/</link><pubDate>Fri, 19 Apr 2024 16:22:41 +0100</pubDate><guid>https://rickschubert.net/blog/posts/javascript-for-qas/</guid><description>&lt;p>Yet again, I have been a guest blogger over at qajobs.co. My latest article details why I think that JavaScript is the perfect programming language for the QA and testing domain. &lt;a href="https://www.qajobs.co/blog/why-javascript-is-the-best-programming-language-for-the-qa-domain">You can check out the article here&lt;/a>.&lt;/p>
&lt;hr>
&lt;p>Monkey image created with Bing AI&lt;/p></description></item><item><title>My Favourite Browser Automation Tools</title><link>https://rickschubert.net/blog/posts/favourite-browser-automation-tech/</link><pubDate>Thu, 11 Apr 2024 15:53:09 +0100</pubDate><guid>https://rickschubert.net/blog/posts/favourite-browser-automation-tech/</guid><description>&lt;p>Recently, I wrote a blog article on my favourite browser automation tools. &lt;a href="https://www.qajobs.co/blog/exploring-browser-automation-tools">You can read the complete post on the blog of qajobs.co&lt;/a>.&lt;/p>
&lt;p>Due to reasons of SEO deranking, I was asked not to re-post the content on my own blog which is why I&amp;rsquo;m merely linking to it. It was great fun collaborating with another blog!&lt;/p>
&lt;hr>
&lt;p>Cat image created with Bing AI&lt;/p></description></item><item><title>How I wrote an application to help me write a screenplay</title><link>https://rickschubert.net/blog/posts/how-i-wrote-an-application-to-help-me-write-a-screenplay/</link><pubDate>Mon, 28 Mar 2022 19:34:34 +0100</pubDate><guid>https://rickschubert.net/blog/posts/how-i-wrote-an-application-to-help-me-write-a-screenplay/</guid><description>&lt;p>Programming and writing are my two passions, although in different measures depending on my life state. I started writing a screenplay a while back but progress stalled. At the same time, my drive to program had been flaring up so what would have been better than to program a tool that supports my writing ambitions!&lt;/p>
&lt;p>Thus, the &amp;ldquo;Scene Mover&amp;rdquo; was born. This application allows me to compose a screenplay out of scenes: Instead of writing and adding scenes to one single document, this tool compiles a final script out of multiple files, one per scene. Using drag and drop, I can reorder scenes which should help tremendously when writing my screenplay in a non-chronological order where I would want to concentrate on small chunks individually.&lt;/p>
&lt;p>Using this tool, I can have a folder &lt;code>/scenes&lt;/code> in my project which holds different files for different scenes. The extension assembles a full script out of the individual scenes and recompiles the script every time something changes in the directory, either because a scene is updated, a scene is added or because scenes are reordered using the drag and drop feature.&lt;/p>
&lt;p>A short recording of the rearranging functionality:&lt;/p>
&lt;img src="https://rickschubert.net/blog/images/scenemover_moving_scene_with_files_in_view.gif" />
&lt;h2 id="technical-implementation">Technical Implementation&lt;/h2>
&lt;h3 id="backend">Backend&lt;/h3>
&lt;p>My initial idea was to create a Visual Studio Code extension that would allow me to bring up a new tab right in my editor that shows the application. This idea arose because I use Visual Studio Code as my writing tool of choice along with the fantastic screenwriting extension &lt;a href="https://github.com/piersdeseilligny/betterfountain">betterfountain&lt;/a> (to which I have also contributed a small number of features over the past years).&lt;/p>
&lt;p>It was clear to me that I needed to render the app in Visual Studio Code using a &lt;a href="https://code.visualstudio.com/api/extension-guides/webview">webview&lt;/a>. Webviews in VS Code can receive events from the outside as well as send events to the outside. On rearranging a scene using drag and drop, the webview could notify the rest of the extension that a scene has moved and it could then take care of rearranging the files.&lt;/p>
&lt;p>But since visual studio code extensions are written in JavaScript/TypeScript, using this approach, I would have had to write code to assemble the final script and to move files in nodejs. Although JavaScript is a big love of mine, I wanted to write a lot of this application in Golang as I believe in its processing speed and as I find working with the language extremely satisfying and joyful. While I could have written several small CLI tools in Go that could then be started from within nodejs using child processes, I decided to create a local webserver instead. Although this decision forces users to always have a webserver running locally, it helped me tremendously with writing this tool as I am quite familiar with web apps and as it meant I could easily test the file moving and script reassembling functionalities. Additionally, it was clear to me from day one that this project would be specifically written just for myself - thus, anything goes.&lt;/p>
&lt;p>Although I am calling this backend application a server, the program actually does a little bit more: aside from the webserver responsible for reordering scenes, the program also launches a file watcher that looks out for any changes to the &lt;code>scenes/&lt;/code> directory and recompiles the full script. The webserver itself exposes two endpoints: &lt;code>GET /scenes&lt;/code> which lists all of the scenes so that the web interface can display them, as well as &lt;code>POST /scenes/transition&lt;/code> which is called by the web app to reorder scenes when the user reorders them using drag and drop.&lt;/p>
&lt;p>For &lt;code>POST /scenes/transition&lt;/code>, the user needs to supply a JSON body with two parameters: the name of the file that is about to be moved as well as the new position of the scene. Changing the position of an item in a slice might not seem like much, but I found it to be quite the challenge. What I ended up doing was what any respectable programmer would do: steal someone else&amp;rsquo;s genius and reap the rewards! I discovered a JavaScript library that did exactly what I wanted, &lt;a href="https://github.com/sindresorhus/array-move/blob/main/index.js">copied the algorithm&lt;/a> and translated it into Golang. Since Golang does not have an equivalent of &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice">JavaScript&amp;rsquo;s splice method&lt;/a>, I also additionally stole - um, &lt;em>relocated&lt;/em> - &lt;a href="https://github.com/zzwx/splice">an implementation&lt;/a> of the &lt;code>splice&lt;/code> method in Golang.&lt;/p>
&lt;h3 id="frontend">Frontend&lt;/h3>
&lt;p>As outlined previously, the original goal of the project was to spawn a webview from within VSCode. Thus, I did some research and noticed the existing VSCode extension &lt;a href="https://github.com/iskenxan/sheepy-fp-guide">sheepy-fp-guilde&lt;/a> that spawns a React app within a webview. I used this extension as a starting point but tore out all of its pieces except for the foundations.&lt;/p>
&lt;p>Choosing React was a no-brainer to me as I have extensive experience with it. But since I am not a frontend developer by profession, I find it usually quite the surprise coming back: every time I revisit the framework, new programming paradigms, toolchains and API methods come up; in my case most notably memoisation and Redux Toolkit. I found it equally challenging as well as rewarding to learn about these.&lt;/p>
&lt;p>Aside from React, I use Redux to handle state changes and Sass to create my (minimal) stylesheets. The main chunk of the project is the drag and drop feature which is powered by &lt;a href="https://react-dnd.github.io/react-dnd/about">React DnD&lt;/a>. Getting this to work was by far the biggest challenge of the entire project. The tutorial for React DnD is very detailed which caused me to lose focus. I ended up relying very little on the tutorial and instead experimented for a while with the various functionalities, fueled by spot-reading of the documentation. But alas, once the drag and drop worked&amp;hellip; it was incredible! I jumped out of my chair, clenched my fists and proclaimed &amp;ldquo;It works! It works!&amp;rdquo;&lt;/p>
&lt;h2 id="view-into-the-future-and-summary">View into the future and summary&lt;/h2>
&lt;p>What helped me tremendously with this project was my mindset of imperfectionism. Instead of trying to create something impressive which could be used by others, I focused on getting things to work &lt;em>for myself&lt;/em> using the tools I already know. Otherwise, I might not have been able to finish the project at all.&lt;/p>
&lt;p>I know that there would be plenty of things to improve on this app, but that&amp;rsquo;s the beauty - I don&amp;rsquo;t care! For example, the app currently cannot be rendered in a Visual Studio Code webview, instead I need to open it in a browser. But despite this, it works and does what I wanted it to do; I am now able to rearrange scenes and write my screenplay in a more compositional manner. Therefore, I should focus on what I actually built this for: writing my screenplay!&lt;/p>
&lt;hr>
&lt;p>&lt;a href="https://github.com/rickschubert/scenemover-go-tools">Backend code for the project on GitHub&lt;/a>&lt;/p>
&lt;p>&lt;a href="https://github.com/rickschubert/scenemover-visual-studio-code-extension">Frotend code for the project on GitHub&lt;/a>&lt;/p>
&lt;p>Original screenplay image from &lt;a href="https://www.rrfedu.com/blog/2016/09/26/screenwriting-101-read-screenplay/">rrfedu.com&lt;/a>, modified&lt;/p></description></item><item><title>mock-inspect: A simple way to achieve contract testing</title><link>https://rickschubert.net/blog/posts/mock-inspect-contract-testing/</link><pubDate>Thu, 27 May 2021 23:23:02 +0100</pubDate><guid>https://rickschubert.net/blog/posts/mock-inspect-contract-testing/</guid><description>&lt;div class="callout">
This article is part of a series about the open-source Node.js testing library _mock-inspect_ of which I am the main contributor and author. &lt;a href="https://rickschubert.net/blog/tags/mock-inspect">Check out the entire series here!&lt;/a>
&lt;/div>
&lt;p>In &lt;a href="https://rickschubert.net/blog/posts/mock-inspect-what-it-is-and-how-to-use-it">last week&amp;rsquo;s article&lt;/a>, we discussed how we can use &lt;a href="https://github.com/trayio/mock-inspect">&lt;em>mock-inspect&lt;/em>&lt;/a> to assert &lt;em>if&lt;/em> and &lt;em>how&lt;/em> the application under test makes network requests to API endpoints while simultaneously mocking out their responses. This week we are going to explore how we can use &lt;em>mock-inspect&lt;/em> to implement a contract testing strategy without complicated frameworks like PACT.&lt;/p>
&lt;p>This post is accompanied by a &lt;a href="https://github.com/rickschubert/mock-inspect-contract-testing-example">GitHub repository&lt;/a> which holds all the code examples so that they can be tried out locally.&lt;/p>
&lt;h2 id="understanding-contract-testing">Understanding contract testing&lt;/h2>
&lt;p>A network request is made out of two steps:&lt;/p>
&lt;ul>
&lt;li>The &lt;strong>consumer&lt;/strong> &lt;em>asks&lt;/em> for something&lt;/li>
&lt;li>The &lt;strong>provider&lt;/strong> &lt;em>answers&lt;/em> with something&lt;/li>
&lt;/ul>
&lt;p>The problem is usually that provider and consumer sit in different code bases. Think of a web app: The provider would be the backend server and the consumer would be a frontend application questioning the backend server. Both projects usually live in separate repositories.&lt;/p>
&lt;p>Contract testing aims to ensure that if the consumer &lt;em>asks&lt;/em> for the right thing, the provider &lt;em>answers&lt;/em> with the right thing. A popular testing framework for this is &lt;a href="https://docs.pact.io/">PACT&lt;/a> which is a monster on its own, providing beefy implementations for a multitude of programming languages. The way it works is by setting up a &amp;ldquo;contract&amp;rdquo; which contains information about how the request should be made and what the API should respond with. To check the consumer side, PACT would tell you to create a mock server that responds with some mocked data given that the request has been made the right way.&lt;/p>
&lt;p>&lt;em>mock-inspect&lt;/em> is a Node.js tool which allows you to mock network requests and inspect how these requests were made. You can see where I am going with this: As long as the consumer is written in Node.js (like a frontend application or a backend server), we can use &lt;em>mock-inspect&lt;/em> for contract testing - no need for the complicated PACT framework.&lt;/p>
&lt;h2 id="strategy-overview-no-contracts-yet">Strategy overview (no contracts yet)&lt;/h2>
&lt;p>In contract testing, we want to test both aspects of the flow: the provider and the consumer side.&lt;/p>
&lt;p>A provider test is simple to do and doesn&amp;rsquo;t even need &lt;em>mock-inspect&lt;/em>. As long as we write a test for our backend server which asserts that the response is correct (given the correct request was made), we have already the provider side covered. Pretty easy.&lt;/p>
&lt;p>More interesting is the consumer side which is where &lt;em>mock-inspect&lt;/em> can be used. No matter if your consumer is a backend server or a frontend application, as long as it is written in Node.js and making a network request, &lt;em>mock-inspect&lt;/em> can help you. All we have to do is to write a test with a mocked network request and then assert that the consumer has asked the right question, i.e. has included the correct headers and payload. This is how such a consumer test could look like in mock-inspect:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-js" data-lang="js">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">const&lt;/span> {&lt;span style="color:#a6e22e">mockRequest&lt;/span>} &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#a6e22e">require&lt;/span>(&lt;span style="color:#e6db74">&amp;#34;mock-inspect&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">// Inside your test
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">// Step 1: Set up the mock for the third party API so that your test
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">// does not hit the real API. Provide a fixed response body.
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">&lt;/span>&lt;span style="color:#66d9ef">const&lt;/span> &lt;span style="color:#a6e22e">thirdPartyMock&lt;/span> &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#a6e22e">mockRequest&lt;/span>({
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">url&lt;/span>&lt;span style="color:#f92672">:&lt;/span> &lt;span style="color:#e6db74">&amp;#34;http://localhost:5000/v1/cities_you_can_fly_to&amp;#34;&lt;/span>,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">method&lt;/span>&lt;span style="color:#f92672">:&lt;/span> &lt;span style="color:#e6db74">&amp;#34;POST&amp;#34;&lt;/span>,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">responseBody&lt;/span>&lt;span style="color:#f92672">:&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">London&lt;/span>&lt;span style="color:#f92672">:&lt;/span> &lt;span style="color:#66d9ef">true&lt;/span>,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">Birmingham&lt;/span>&lt;span style="color:#f92672">:&lt;/span> &lt;span style="color:#66d9ef">true&lt;/span>,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">Edinburgh&lt;/span>&lt;span style="color:#f92672">:&lt;/span> &lt;span style="color:#66d9ef">false&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>})
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">// Step 2: Have your test execute the code which is expected
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">// to call the provider (backend server)
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">&lt;/span>&lt;span style="color:#66d9ef">await&lt;/span> &lt;span style="color:#a6e22e">printPossibleLocations&lt;/span>()
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">// Step 3: Using mock-inspect&amp;#39;s `inspect()` method available on
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">// instances returned by `mockRequest`, check out how the request
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">// was made and match it against your expectations
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">&lt;/span>&lt;span style="color:#66d9ef">const&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">requestPayload&lt;/span>,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">requestHeaders&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>} &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#a6e22e">thirdPartyMock&lt;/span>.&lt;span style="color:#a6e22e">inspect&lt;/span>()
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">expect&lt;/span>(&lt;span style="color:#a6e22e">requestHeaders&lt;/span>[&lt;span style="color:#e6db74">&amp;#34;authorization&amp;#34;&lt;/span>]).&lt;span style="color:#a6e22e">toEqual&lt;/span>(&lt;span style="color:#e6db74">&amp;#34;Bearer my_API_token_was_used&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">expect&lt;/span>(&lt;span style="color:#a6e22e">requestPayload&lt;/span>).&lt;span style="color:#a6e22e">toEqual&lt;/span>({&lt;span style="color:#a6e22e">filterByCountry&lt;/span>&lt;span style="color:#f92672">:&lt;/span> &lt;span style="color:#e6db74">&amp;#34;England&amp;#34;&lt;/span>})
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The above test executes our function which is making a network request to our backend server (the &amp;ldquo;provider&amp;rdquo;) and then asserts that the request has been made correctly. In the example above, we are checking that the API token has been forwarded in the request headers and that a request payload has been used to filter the list of cities by a specific country.&lt;/p>
&lt;h2 id="adding-a-contract">Adding a contract&lt;/h2>
&lt;p>We now have two separate test suites which do their job. But the problem is that these tests don&amp;rsquo;t yet relate to each other: we could end up modifying the expectations on one side only to realise that these don&amp;rsquo;t match up anymore with the behaviour on the other side.&lt;/p>
&lt;p>This is where contracts come in. A contract is a way of codifying the behaviour for both sides of the request, consumer and provider. The provider (the backend server) agrees to respond with a certain message as long as it is asked the correct question. The consumer in turn can be sure that it will receive the correct answer given it asks the right question.&lt;/p>
&lt;p>A suitable format for this information is JSON as it can be understood by any programming language. That way, we can use contract testing even for non-Node.js providers, no matter if these are written in Java, Golang or something else. I would structure a contract as follows:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-json" data-lang="json">&lt;span style="display:flex;">&lt;span>{
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">&amp;#34;request&amp;#34;&lt;/span>: {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">&amp;#34;url&amp;#34;&lt;/span>: &lt;span style="color:#e6db74">&amp;#34;http://localhost:5000/v1/cities_you_can_fly_to&amp;#34;&lt;/span>,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">&amp;#34;method&amp;#34;&lt;/span>: &lt;span style="color:#e6db74">&amp;#34;POST&amp;#34;&lt;/span>,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">&amp;#34;payload&amp;#34;&lt;/span>: {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">&amp;#34;type&amp;#34;&lt;/span>: &lt;span style="color:#e6db74">&amp;#34;object&amp;#34;&lt;/span>,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">&amp;#34;properties&amp;#34;&lt;/span>: {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">&amp;#34;filterByCountry&amp;#34;&lt;/span>: {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">&amp;#34;type&amp;#34;&lt;/span>: &lt;span style="color:#e6db74">&amp;#34;string&amp;#34;&lt;/span>,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">&amp;#34;minLength&amp;#34;&lt;/span>: &lt;span style="color:#ae81ff">1&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> },
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">&amp;#34;additionalProperties&amp;#34;&lt;/span>: &lt;span style="color:#66d9ef">true&lt;/span>,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">&amp;#34;required&amp;#34;&lt;/span>: [
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#e6db74">&amp;#34;filterByCountry&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> ]
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> },
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">&amp;#34;headers&amp;#34;&lt;/span>: {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">&amp;#34;authorization&amp;#34;&lt;/span>: &lt;span style="color:#e6db74">&amp;#34;Bearer my_API_token_was_used&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> },
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">&amp;#34;response&amp;#34;&lt;/span>: {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">&amp;#34;body&amp;#34;&lt;/span>: {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">&amp;#34;type&amp;#34;&lt;/span>: &lt;span style="color:#e6db74">&amp;#34;object&amp;#34;&lt;/span>,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">&amp;#34;minProperties&amp;#34;&lt;/span>: &lt;span style="color:#ae81ff">3&lt;/span>,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">&amp;#34;patternProperties&amp;#34;&lt;/span>: {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">&amp;#34;^.*$&amp;#34;&lt;/span>: {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">&amp;#34;type&amp;#34;&lt;/span>: &lt;span style="color:#e6db74">&amp;#34;boolean&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> },
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">&amp;#34;statusCode&amp;#34;&lt;/span>: &lt;span style="color:#ae81ff">200&lt;/span>,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">&amp;#34;headers&amp;#34;&lt;/span>: {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">&amp;#34;content-type&amp;#34;&lt;/span>: &lt;span style="color:#e6db74">&amp;#34;application/json; charset=utf-8&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>We are defining two pieces of information here:&lt;/p>
&lt;ul>
&lt;li>In &lt;code>request&lt;/code>, we define &lt;strong>how the request should be made&lt;/strong>. We should be hitting the desired URL with the &lt;code>POST&lt;/code> method and we need to provide a JSON payload. The JSON payload needs to follow a specific &lt;a href="https://json-schema.org/">JSON schema&lt;/a>. To explain JSON schema would be out of scope for this article but in short, it is a way of describing JSON structures. In the case above, we say that the &lt;code>payload&lt;/code> needs to be a JSON object with a required property called &lt;code>filterByCountry&lt;/code> which holds a string value.&lt;/li>
&lt;li>In &lt;code>response&lt;/code>, we define &lt;strong>what the API should return&lt;/strong>. The API should respond with a status code of 200 and the &lt;code>application/json; charset=utf-8&lt;/code> header as Content-Type. The response body should be a JSON object with at least three properties which should all hold a boolean value.&lt;/li>
&lt;/ul>
&lt;p>These contracts are best placed into a separate repository so that they can be accessed by different test suites. Ideally, you would version this repository either using NPM modules or with git tags. Versioning allows for different test suites to use these contracts independently and makes updating test suites a breeze.&lt;/p>
&lt;p>Once we have the contract finished, all we have to do is set up our two tests so that they make use of the contract instead of the previously hard-coded values. The provider test (for the backend server) will now take the JSON schema defined under &lt;code>response.body&lt;/code> and assert that the API does indeed serve a JSON structure matching that schema. The test will now also check that the status code and the headers match those defined in &lt;code>response&lt;/code>.&lt;/p>
&lt;p>Below is how the provider test looks with contracts. We are using the NPM library &lt;a href="https://json-schema-faker.js.org/">&lt;code>json-schema-faker&lt;/code>&lt;/a> to generate a sample request payload that abides by the rules stated in the contract&amp;rsquo;s JSON schema. Additionally, we are using the &lt;a href="https://github.com/ajv-validator/ajv">AJV library&lt;/a> to assert that the response body matches the JSON schema.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-js" data-lang="js">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">const&lt;/span> &lt;span style="color:#a6e22e">request&lt;/span> &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#a6e22e">require&lt;/span>(&lt;span style="color:#e6db74">&amp;#34;request-promise&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">const&lt;/span> &lt;span style="color:#a6e22e">contract&lt;/span> &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#a6e22e">require&lt;/span>(&lt;span style="color:#e6db74">&amp;#34;../../contracts/v1/cities_you_can_fly_to.json&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">// Used to auto-generate a request payload from the contract&amp;#39;s JSON schema
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">&lt;/span>&lt;span style="color:#66d9ef">const&lt;/span> &lt;span style="color:#a6e22e">jsonSchemaFaker&lt;/span> &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#a6e22e">require&lt;/span>(&lt;span style="color:#e6db74">&amp;#34;json-schema-faker&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">// Used to validate that the response body matches the JSON schema
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">&lt;/span>&lt;span style="color:#66d9ef">const&lt;/span> &lt;span style="color:#a6e22e">AJV&lt;/span> &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#a6e22e">require&lt;/span>(&lt;span style="color:#e6db74">&amp;#34;ajv&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">const&lt;/span> &lt;span style="color:#a6e22e">ajv&lt;/span> &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#66d9ef">new&lt;/span> &lt;span style="color:#a6e22e">AJV&lt;/span>({&lt;span style="color:#a6e22e">allErrors&lt;/span>&lt;span style="color:#f92672">:&lt;/span> &lt;span style="color:#66d9ef">true&lt;/span>})
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">const&lt;/span> &lt;span style="color:#a6e22e">result&lt;/span> &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#66d9ef">await&lt;/span> &lt;span style="color:#a6e22e">request&lt;/span>({
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">url&lt;/span>&lt;span style="color:#f92672">:&lt;/span> &lt;span style="color:#a6e22e">contract&lt;/span>.&lt;span style="color:#a6e22e">request&lt;/span>.&lt;span style="color:#a6e22e">url&lt;/span>,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">method&lt;/span>&lt;span style="color:#f92672">:&lt;/span> &lt;span style="color:#a6e22e">contract&lt;/span>.&lt;span style="color:#a6e22e">request&lt;/span>.&lt;span style="color:#a6e22e">method&lt;/span>,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">headers&lt;/span>&lt;span style="color:#f92672">:&lt;/span> &lt;span style="color:#a6e22e">contract&lt;/span>.&lt;span style="color:#a6e22e">request&lt;/span>.&lt;span style="color:#a6e22e">headers&lt;/span>,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">body&lt;/span>&lt;span style="color:#f92672">:&lt;/span> &lt;span style="color:#a6e22e">jsonSchemaFaker&lt;/span>.&lt;span style="color:#a6e22e">generate&lt;/span>(&lt;span style="color:#a6e22e">contract&lt;/span>.&lt;span style="color:#a6e22e">request&lt;/span>.&lt;span style="color:#a6e22e">payload&lt;/span>),
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">resolveWithFullResponse&lt;/span>&lt;span style="color:#f92672">:&lt;/span> &lt;span style="color:#66d9ef">true&lt;/span>,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">json&lt;/span>&lt;span style="color:#f92672">:&lt;/span> &lt;span style="color:#66d9ef">true&lt;/span>,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>})
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">expect&lt;/span>(&lt;span style="color:#a6e22e">result&lt;/span>.&lt;span style="color:#a6e22e">statusCode&lt;/span>).&lt;span style="color:#a6e22e">toBe&lt;/span>(&lt;span style="color:#a6e22e">contract&lt;/span>.&lt;span style="color:#a6e22e">response&lt;/span>.&lt;span style="color:#a6e22e">statusCode&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">for&lt;/span> (&lt;span style="color:#66d9ef">const&lt;/span> &lt;span style="color:#a6e22e">header&lt;/span> &lt;span style="color:#66d9ef">in&lt;/span> &lt;span style="color:#a6e22e">contract&lt;/span>.&lt;span style="color:#a6e22e">response&lt;/span>.&lt;span style="color:#a6e22e">headers&lt;/span>) {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">expect&lt;/span>(&lt;span style="color:#a6e22e">result&lt;/span>.&lt;span style="color:#a6e22e">headers&lt;/span>[&lt;span style="color:#a6e22e">header&lt;/span>]).&lt;span style="color:#a6e22e">toBe&lt;/span>(
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">contract&lt;/span>.&lt;span style="color:#a6e22e">response&lt;/span>.&lt;span style="color:#a6e22e">headers&lt;/span>[&lt;span style="color:#a6e22e">header&lt;/span>],
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> )
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">const&lt;/span> &lt;span style="color:#a6e22e">validator&lt;/span> &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#a6e22e">ajv&lt;/span>.&lt;span style="color:#a6e22e">compile&lt;/span>(&lt;span style="color:#a6e22e">contract&lt;/span>.&lt;span style="color:#a6e22e">response&lt;/span>.&lt;span style="color:#a6e22e">body&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">const&lt;/span> &lt;span style="color:#a6e22e">isValid&lt;/span> &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#a6e22e">validator&lt;/span>(&lt;span style="color:#a6e22e">result&lt;/span>.&lt;span style="color:#a6e22e">body&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">if&lt;/span> (&lt;span style="color:#f92672">!&lt;/span>&lt;span style="color:#a6e22e">isValid&lt;/span>) {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">throw&lt;/span> &lt;span style="color:#66d9ef">new&lt;/span> Error(
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#e6db74">&amp;#34;Our provider did not send a response body matching&amp;#34;&lt;/span> &lt;span style="color:#f92672">+&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#e6db74">&amp;#34;the contract response body.&amp;#34;&lt;/span>,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> )
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The consumer test works similarly, but this time we are generating a sample response and validate against the request payload. Thanks to &lt;em>mock-inspect&lt;/em>, we don&amp;rsquo;t need to run the backend server for our unit tests: &lt;em>mock-inspect&lt;/em> mocks the outgoing network request and returns the specified sample response. We can make use of the &lt;code>inspect()&lt;/code> method exposed by &lt;em>mock-inspect&lt;/em> to see how a request has been made. This is exactly what we want to assert on the consumer side: we want to ensure that the consumer makes the network request as stated in the contract.&lt;/p>
&lt;p>This is how our consumer test looks like after introducing contracts:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-js" data-lang="js">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">const&lt;/span> {&lt;span style="color:#a6e22e">printPossibleLocations&lt;/span>} &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#a6e22e">require&lt;/span>(&lt;span style="color:#e6db74">&amp;#34;../index.js&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">const&lt;/span> &lt;span style="color:#a6e22e">contract&lt;/span> &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#a6e22e">require&lt;/span>(&lt;span style="color:#e6db74">&amp;#34;../../contracts/v1/cities_you_can_fly_to.json&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">// Used to mock the network request going to the API
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">&lt;/span>&lt;span style="color:#66d9ef">const&lt;/span> {&lt;span style="color:#a6e22e">mockRequest&lt;/span>} &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#a6e22e">require&lt;/span>(&lt;span style="color:#e6db74">&amp;#34;mock-inspect&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">// Used to auto-generate a response from the contract&amp;#39;s JSON schema
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">&lt;/span>&lt;span style="color:#66d9ef">const&lt;/span> &lt;span style="color:#a6e22e">jsonSchemaFaker&lt;/span> &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#a6e22e">require&lt;/span>(&lt;span style="color:#e6db74">&amp;#34;json-schema-faker&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">// Used to validate that the request payload matches the JSON schema
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">&lt;/span>&lt;span style="color:#66d9ef">const&lt;/span> &lt;span style="color:#a6e22e">AJV&lt;/span> &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#a6e22e">require&lt;/span>(&lt;span style="color:#e6db74">&amp;#34;ajv&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">const&lt;/span> &lt;span style="color:#a6e22e">ajv&lt;/span> &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#66d9ef">new&lt;/span> &lt;span style="color:#a6e22e">AJV&lt;/span>({&lt;span style="color:#a6e22e">allErrors&lt;/span>&lt;span style="color:#f92672">:&lt;/span> &lt;span style="color:#66d9ef">true&lt;/span>})
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">const&lt;/span> &lt;span style="color:#a6e22e">citiesRequest&lt;/span> &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#a6e22e">mockRequest&lt;/span>({
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">requestPattern&lt;/span>&lt;span style="color:#f92672">:&lt;/span> &lt;span style="color:#a6e22e">contract&lt;/span>.&lt;span style="color:#a6e22e">request&lt;/span>.&lt;span style="color:#a6e22e">url&lt;/span>,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">requestMethod&lt;/span>&lt;span style="color:#f92672">:&lt;/span> &lt;span style="color:#a6e22e">contract&lt;/span>.&lt;span style="color:#a6e22e">request&lt;/span>.&lt;span style="color:#a6e22e">method&lt;/span>,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">responseBody&lt;/span>&lt;span style="color:#f92672">:&lt;/span> &lt;span style="color:#a6e22e">jsonSchemaFaker&lt;/span>.&lt;span style="color:#a6e22e">generate&lt;/span>(&lt;span style="color:#a6e22e">contract&lt;/span>.&lt;span style="color:#a6e22e">response&lt;/span>.&lt;span style="color:#a6e22e">body&lt;/span>),
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">responseStatus&lt;/span>&lt;span style="color:#f92672">:&lt;/span> &lt;span style="color:#a6e22e">contract&lt;/span>.&lt;span style="color:#a6e22e">response&lt;/span>.&lt;span style="color:#a6e22e">statusCode&lt;/span>,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">responseHeaders&lt;/span>&lt;span style="color:#f92672">:&lt;/span> &lt;span style="color:#a6e22e">contract&lt;/span>.&lt;span style="color:#a6e22e">response&lt;/span>.&lt;span style="color:#a6e22e">headers&lt;/span>,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>})
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">await&lt;/span> &lt;span style="color:#a6e22e">printPossibleLocations&lt;/span>()
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">const&lt;/span> {&lt;span style="color:#a6e22e">requestBody&lt;/span>, &lt;span style="color:#a6e22e">requestHeaders&lt;/span>} &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#a6e22e">citiesRequest&lt;/span>.&lt;span style="color:#a6e22e">inspect&lt;/span>()
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">for&lt;/span> (&lt;span style="color:#66d9ef">const&lt;/span> &lt;span style="color:#a6e22e">header&lt;/span> &lt;span style="color:#66d9ef">in&lt;/span> &lt;span style="color:#a6e22e">contract&lt;/span>.&lt;span style="color:#a6e22e">request&lt;/span>.&lt;span style="color:#a6e22e">headers&lt;/span>) {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">expect&lt;/span>(&lt;span style="color:#a6e22e">requestHeaders&lt;/span>[&lt;span style="color:#a6e22e">header&lt;/span>]).&lt;span style="color:#a6e22e">toBe&lt;/span>(
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">contract&lt;/span>.&lt;span style="color:#a6e22e">request&lt;/span>.&lt;span style="color:#a6e22e">headers&lt;/span>[&lt;span style="color:#a6e22e">header&lt;/span>],
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> )
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">const&lt;/span> &lt;span style="color:#a6e22e">validator&lt;/span> &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#a6e22e">ajv&lt;/span>.&lt;span style="color:#a6e22e">compile&lt;/span>(&lt;span style="color:#a6e22e">contract&lt;/span>.&lt;span style="color:#a6e22e">request&lt;/span>.&lt;span style="color:#a6e22e">payload&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">const&lt;/span> &lt;span style="color:#a6e22e">isValid&lt;/span> &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#a6e22e">validator&lt;/span>(&lt;span style="color:#a6e22e">requestBody&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">if&lt;/span> (&lt;span style="color:#f92672">!&lt;/span>&lt;span style="color:#a6e22e">isValid&lt;/span>) {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">throw&lt;/span> &lt;span style="color:#66d9ef">new&lt;/span> Error(
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#e6db74">&amp;#34;Our consumer did not send a request payload matching&amp;#34;&lt;/span> &lt;span style="color:#f92672">+&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#e6db74">&amp;#34;the contract request payload.&amp;#34;&lt;/span>,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> )
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="closing-words">Closing words&lt;/h2>
&lt;p>As you saw, it is easy to do contract testing with &lt;em>mock-inspect&lt;/em>, there is no need for complicated frameworks like PACT. All we need is a consumer written in Node.js so that we can use &lt;em>mock-inspect&lt;/em> to mock network requests. Since any programming language can read JSON files, providers can make use of the contracts in their test suites, no matter if these provider services are written in Java, Golang or any other language - the same principles apply.&lt;/p>
&lt;p>I wish you a lot of fun with contract testing! Should you have any questions, don&amp;rsquo;t hesitate to reach out to us over at &lt;a href="https://github.com/trayio/mock-inspect">&lt;em>mock-inspect&lt;/em>&lt;/a>.&lt;/p>
&lt;hr>
&lt;p>&lt;a href="https://github.com/trayio/mock-inspect/tree/main/assets/chameleon.png">Chameleon&lt;/a> graphic by &lt;a href="https://pixabay.com/users/monstreh-637659/?utm_source=link-attribution&amp;amp;utm_medium=referral&amp;amp;utm_campaign=image&amp;amp;utm_content=3340930">Анна Куликова&lt;/a> from &lt;a href="https://pixabay.com/?utm_source=link-attribution&amp;amp;utm_medium=referral&amp;amp;utm_campaign=image&amp;amp;utm_content=3340930">Pixabay&lt;/a>&lt;/p></description></item><item><title>mock-inspect: Mock network requests and observe how these requests were made</title><link>https://rickschubert.net/blog/posts/mock-inspect-what-it-is-and-how-to-use-it/</link><pubDate>Mon, 10 May 2021 23:19:54 +0100</pubDate><guid>https://rickschubert.net/blog/posts/mock-inspect-what-it-is-and-how-to-use-it/</guid><description>&lt;div class="callout">
This article is part of a series about the open-source Node.js testing library mock-inspect of which I am the main contributor and author. &lt;a href="https://rickschubert.net/blog/tags/mock-inspect">Check out the entire series here!&lt;/a>
&lt;/div>
&lt;p>&lt;a href="https://github.com/trayio/mock-inspect">mock-inspect is an open-source library&lt;/a> for Node.js which allows you to mock network requests in your tests and to inspect how these requests have been made in the application. Has the endpoint been called? Which request body and request headers did the application pass along? This knowledge can be used to create your very own assertions, entirely depending on your use case.&lt;/p>
&lt;p>Since mock-inspect is a Node.js library, it can be used both for frontend as well as backend applications. In this blog post, we want to explore the use case in a frontend project using React and how we can elevate our tests thanks to mock-inspect.&lt;/p>
&lt;h2 id="application-outline">Application Outline&lt;/h2>
&lt;p>To demonstrate the usage of mock-inspect, I have prepared an &lt;a href="http://github.com/rickschubert/mock-inspect-react-app-example">example frontend application&lt;/a> which is inspired by a real-life use case, the &lt;a href="https://tray.io">tray.io marketing website&lt;/a>. Users can fill out a form to request a specific type of demo for the product. When the form is submitted, an API endpoint is pinged with all the properties that the user supplied. The frontend application always calls the same endpoint, no matter which selection the user makes. That endpoint will return a 200 status code, no matter what. Once the frontend application has forwarded the properties, it displays a message which says that the user is going to receive an e-mail. Note that the e-mail is &lt;em>scheduled&lt;/em> to be sent; it could also be sent a few hours later.&lt;/p>
&lt;p>A recording of the user flow:&lt;/p>
&lt;img src="https://rickschubert.net/blog/images/mock-inspect_example_app_flow.gif" />
&lt;p>Below is the React code for this simple application (feel free to skip over this one):&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-jsx" data-lang="jsx">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">import&lt;/span> &lt;span style="color:#e6db74">&amp;#34;./App.css&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">import&lt;/span> {&lt;span style="color:#a6e22e">useState&lt;/span>} &lt;span style="color:#a6e22e">from&lt;/span> &lt;span style="color:#e6db74">&amp;#34;react&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">function&lt;/span> &lt;span style="color:#a6e22e">App&lt;/span>() {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">const&lt;/span> [&lt;span style="color:#a6e22e">name&lt;/span>, &lt;span style="color:#a6e22e">setName&lt;/span>] &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#a6e22e">useState&lt;/span>(&lt;span style="color:#e6db74">&amp;#34;&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">const&lt;/span> [&lt;span style="color:#a6e22e">email&lt;/span>, &lt;span style="color:#a6e22e">setEmail&lt;/span>] &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#a6e22e">useState&lt;/span>(&lt;span style="color:#e6db74">&amp;#34;&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">const&lt;/span> [&lt;span style="color:#a6e22e">demoType&lt;/span>, &lt;span style="color:#a6e22e">setDemoType&lt;/span>] &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#a6e22e">useState&lt;/span>(&lt;span style="color:#e6db74">&amp;#34;&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">const&lt;/span> [&lt;span style="color:#a6e22e">submitted&lt;/span>, &lt;span style="color:#a6e22e">setSubmitted&lt;/span>] &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#a6e22e">useState&lt;/span>(&lt;span style="color:#66d9ef">false&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">const&lt;/span> &lt;span style="color:#a6e22e">nameHandler&lt;/span> &lt;span style="color:#f92672">=&lt;/span> (&lt;span style="color:#a6e22e">event&lt;/span>) =&amp;gt; {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">setName&lt;/span>(&lt;span style="color:#a6e22e">event&lt;/span>.&lt;span style="color:#a6e22e">target&lt;/span>.&lt;span style="color:#a6e22e">value&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">const&lt;/span> &lt;span style="color:#a6e22e">emailHandler&lt;/span> &lt;span style="color:#f92672">=&lt;/span> (&lt;span style="color:#a6e22e">event&lt;/span>) =&amp;gt; {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">setEmail&lt;/span>(&lt;span style="color:#a6e22e">event&lt;/span>.&lt;span style="color:#a6e22e">target&lt;/span>.&lt;span style="color:#a6e22e">value&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">const&lt;/span> &lt;span style="color:#a6e22e">radioButtonHandler&lt;/span> &lt;span style="color:#f92672">=&lt;/span> (&lt;span style="color:#a6e22e">event&lt;/span>) =&amp;gt; {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">setDemoType&lt;/span>(&lt;span style="color:#a6e22e">event&lt;/span>.&lt;span style="color:#a6e22e">type&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">const&lt;/span> &lt;span style="color:#a6e22e">forwardInputs&lt;/span> &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#66d9ef">async&lt;/span> () =&amp;gt; {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">const&lt;/span> &lt;span style="color:#a6e22e">dataToSend&lt;/span> &lt;span style="color:#f92672">=&lt;/span> {&lt;span style="color:#a6e22e">name&lt;/span>, &lt;span style="color:#a6e22e">email&lt;/span>, &lt;span style="color:#a6e22e">demoType&lt;/span>}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">const&lt;/span> &lt;span style="color:#a6e22e">response&lt;/span> &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#66d9ef">await&lt;/span> &lt;span style="color:#a6e22e">fetch&lt;/span>(&lt;span style="color:#e6db74">&amp;#34;https://someapi.com/request-demo&amp;#34;&lt;/span>, {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">method&lt;/span>&lt;span style="color:#f92672">:&lt;/span> &lt;span style="color:#e6db74">&amp;#34;POST&amp;#34;&lt;/span>,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">headers&lt;/span>&lt;span style="color:#f92672">:&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#e6db74">&amp;#34;Content-Type&amp;#34;&lt;/span>&lt;span style="color:#f92672">:&lt;/span> &lt;span style="color:#e6db74">&amp;#34;application/json&amp;#34;&lt;/span>,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> },
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">body&lt;/span>&lt;span style="color:#f92672">:&lt;/span> &lt;span style="color:#a6e22e">JSON&lt;/span>.&lt;span style="color:#a6e22e">stringify&lt;/span>(&lt;span style="color:#a6e22e">dataToSend&lt;/span>),
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> })
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">if&lt;/span> (&lt;span style="color:#a6e22e">response&lt;/span>.&lt;span style="color:#a6e22e">status&lt;/span> &lt;span style="color:#f92672">===&lt;/span> &lt;span style="color:#ae81ff">200&lt;/span>) {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">setSubmitted&lt;/span>(&lt;span style="color:#66d9ef">true&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">const&lt;/span> &lt;span style="color:#a6e22e">getDemoForm&lt;/span> &lt;span style="color:#f92672">=&lt;/span> () =&amp;gt; {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">return&lt;/span> (
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &amp;lt;&lt;span style="color:#f92672">div&lt;/span> &lt;span style="color:#a6e22e">className&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">&amp;#34;App-header&amp;#34;&lt;/span>&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &amp;lt;&lt;span style="color:#f92672">input&lt;/span> &lt;span style="color:#a6e22e">type&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">&amp;#34;text&amp;#34;&lt;/span> &lt;span style="color:#a6e22e">placeholder&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">&amp;#34;Your name&amp;#34;&lt;/span> &lt;span style="color:#a6e22e">onChange&lt;/span>&lt;span style="color:#f92672">=&lt;/span>{&lt;span style="color:#a6e22e">nameHandler&lt;/span>} /&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &amp;lt;&lt;span style="color:#f92672">input&lt;/span> &lt;span style="color:#a6e22e">type&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">&amp;#34;text&amp;#34;&lt;/span> &lt;span style="color:#a6e22e">placeholder&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">&amp;#34;Your e-mail&amp;#34;&lt;/span> &lt;span style="color:#a6e22e">onChange&lt;/span>&lt;span style="color:#f92672">=&lt;/span>{&lt;span style="color:#a6e22e">emailHandler&lt;/span>} /&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &amp;lt;&lt;span style="color:#f92672">label&lt;/span> &lt;span style="color:#a6e22e">htmlFor&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">&amp;#34;live&amp;#34;&lt;/span>&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">Live&lt;/span> &lt;span style="color:#a6e22e">Demo&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &amp;lt;&lt;span style="color:#f92672">input&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">type&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">&amp;#34;radio&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">id&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">&amp;#34;live&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">value&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">&amp;#34;live&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">onChange&lt;/span>&lt;span style="color:#f92672">=&lt;/span>{&lt;span style="color:#a6e22e">radioButtonHandler&lt;/span>}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> /&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &amp;lt;/&lt;span style="color:#f92672">label&lt;/span>&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &amp;lt;&lt;span style="color:#f92672">label&lt;/span> &lt;span style="color:#a6e22e">htmlFor&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">&amp;#34;recorded&amp;#34;&lt;/span>&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">Recorded&lt;/span> &lt;span style="color:#a6e22e">Demo&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &amp;lt;&lt;span style="color:#f92672">input&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">type&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">&amp;#34;radio&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">id&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">&amp;#34;recorded&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">value&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">&amp;#34;recorded&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">onChange&lt;/span>&lt;span style="color:#f92672">=&lt;/span>{&lt;span style="color:#a6e22e">radioButtonHandler&lt;/span>}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> /&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &amp;lt;/&lt;span style="color:#f92672">label&lt;/span>&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &amp;lt;&lt;span style="color:#f92672">label&lt;/span> &lt;span style="color:#a6e22e">htmlFor&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">&amp;#34;1-2-1&amp;#34;&lt;/span>&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ae81ff">1&lt;/span>&lt;span style="color:#f92672">-&lt;/span>&lt;span style="color:#ae81ff">2&lt;/span>&lt;span style="color:#f92672">-&lt;/span>&lt;span style="color:#ae81ff">1&lt;/span> &lt;span style="color:#66d9ef">with&lt;/span> &lt;span style="color:#a6e22e">a&lt;/span> &lt;span style="color:#a6e22e">representative&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &amp;lt;&lt;span style="color:#f92672">input&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">type&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">&amp;#34;radio&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">id&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">&amp;#34;1-2-1&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">value&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">&amp;#34;1-2-1&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">onChange&lt;/span>&lt;span style="color:#f92672">=&lt;/span>{&lt;span style="color:#a6e22e">radioButtonHandler&lt;/span>}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> /&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &amp;lt;/&lt;span style="color:#f92672">label&lt;/span>&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &amp;lt;&lt;span style="color:#f92672">br&lt;/span> /&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &amp;lt;&lt;span style="color:#f92672">input&lt;/span> &lt;span style="color:#a6e22e">type&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">&amp;#34;button&amp;#34;&lt;/span> &lt;span style="color:#a6e22e">value&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">&amp;#34;Submit&amp;#34;&lt;/span> &lt;span style="color:#a6e22e">onClick&lt;/span>&lt;span style="color:#f92672">=&lt;/span>{&lt;span style="color:#a6e22e">forwardInputs&lt;/span>} /&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &amp;lt;/&lt;span style="color:#f92672">div&lt;/span>&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> )
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">const&lt;/span> &lt;span style="color:#a6e22e">getThankYouPage&lt;/span> &lt;span style="color:#f92672">=&lt;/span> () =&amp;gt; {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">return&lt;/span> (
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &amp;lt;&lt;span style="color:#f92672">div&lt;/span> &lt;span style="color:#a6e22e">className&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">&amp;#34;App-header&amp;#34;&lt;/span>&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">Thank&lt;/span> &lt;span style="color:#a6e22e">you&lt;/span>&lt;span style="color:#f92672">!&lt;/span> &lt;span style="color:#a6e22e">We&lt;/span> &lt;span style="color:#a6e22e">will&lt;/span> &lt;span style="color:#a6e22e">send&lt;/span> &lt;span style="color:#a6e22e">you&lt;/span> &lt;span style="color:#a6e22e">a&lt;/span> &lt;span style="color:#a6e22e">confirmation&lt;/span> &lt;span style="color:#a6e22e">e&lt;/span>&lt;span style="color:#f92672">-&lt;/span>&lt;span style="color:#a6e22e">mail&lt;/span>.
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &amp;lt;/&lt;span style="color:#f92672">div&lt;/span>&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> )
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">return&lt;/span> (
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &amp;lt;&lt;span style="color:#f92672">div&lt;/span> &lt;span style="color:#a6e22e">className&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">&amp;#34;App&amp;#34;&lt;/span>&amp;gt;{&lt;span style="color:#f92672">!&lt;/span>&lt;span style="color:#a6e22e">submitted&lt;/span> &lt;span style="color:#f92672">?&lt;/span> &lt;span style="color:#a6e22e">getDemoForm&lt;/span>() &lt;span style="color:#f92672">:&lt;/span> &lt;span style="color:#a6e22e">getThankYouPage&lt;/span>()}&amp;lt;/&lt;span style="color:#f92672">div&lt;/span>&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> )
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">export&lt;/span> &lt;span style="color:#66d9ef">default&lt;/span> &lt;span style="color:#a6e22e">App&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>How do we test this? In order to make our unit tests stable, we will want to mock the network request so that the tests work independently from the real-world application. &lt;a href="https://mswjs.io/docs/recipes/request-assertions">Many mocking tools&lt;/a> will advise that a good test should check if the application correctly &lt;em>consumes&lt;/em> the API response. But as we have seen in the use case description, there is nothing for our frontend application to consume - the API will return a 200 status code no matter what! In essence, all our application does is forwarding some properties. This is precisely why designing our test around the consumption of the API response would be too simple; we would not test enough by merely testing whether the application reacts to the 200 status code. What is actually important about our use case is that the frontend forwards the correct properties and in the correct format.&lt;/p>
&lt;p>Let&amp;rsquo;s start writing a test for our application. As a start, we are going to use the approach of checking that the application consumes the API response. Below is a test for this in which we are already using mock-inspect to set up the request mock. Apart from that, we are using &lt;a href="https://testing-library.com/docs/react-testing-library/intro/">React Testing Library&lt;/a> to enter text into the inputs and to select our desired demo type.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-js" data-lang="js">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">import&lt;/span> {&lt;span style="color:#a6e22e">render&lt;/span>, &lt;span style="color:#a6e22e">screen&lt;/span>, &lt;span style="color:#a6e22e">fireEvent&lt;/span>, &lt;span style="color:#a6e22e">waitFor&lt;/span>} &lt;span style="color:#a6e22e">from&lt;/span> &lt;span style="color:#e6db74">&amp;#34;@testing-library/react&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">import&lt;/span> &lt;span style="color:#a6e22e">App&lt;/span> &lt;span style="color:#a6e22e">from&lt;/span> &lt;span style="color:#e6db74">&amp;#34;./App&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">import&lt;/span> {&lt;span style="color:#a6e22e">mockRequest&lt;/span>} &lt;span style="color:#a6e22e">from&lt;/span> &lt;span style="color:#e6db74">&amp;#34;mock-inspect&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">test&lt;/span>(&lt;span style="color:#e6db74">&amp;#34;shows Thank You page on submitting the form&amp;#34;&lt;/span>, &lt;span style="color:#66d9ef">async&lt;/span> () =&amp;gt; {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">mockRequest&lt;/span>({
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">requestPattern&lt;/span>&lt;span style="color:#f92672">:&lt;/span> &lt;span style="color:#e6db74">&amp;#34;https://someapi.com/request-demo&amp;#34;&lt;/span>,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">requestMethod&lt;/span>&lt;span style="color:#f92672">:&lt;/span> &lt;span style="color:#e6db74">&amp;#34;POST&amp;#34;&lt;/span>,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">responseStatus&lt;/span>&lt;span style="color:#f92672">:&lt;/span> &lt;span style="color:#ae81ff">200&lt;/span>,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> })
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">render&lt;/span>(&lt;span style="color:#f92672">&amp;lt;&lt;/span>&lt;span style="color:#a6e22e">App&lt;/span> &lt;span style="color:#f92672">/&amp;gt;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">const&lt;/span> &lt;span style="color:#a6e22e">nameInput&lt;/span> &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#a6e22e">screen&lt;/span>.&lt;span style="color:#a6e22e">getByPlaceholderText&lt;/span>(&lt;span style="color:#e6db74">&amp;#34;Your name&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">fireEvent&lt;/span>.&lt;span style="color:#a6e22e">change&lt;/span>(&lt;span style="color:#a6e22e">nameInput&lt;/span>, {&lt;span style="color:#a6e22e">target&lt;/span>&lt;span style="color:#f92672">:&lt;/span> {&lt;span style="color:#a6e22e">value&lt;/span>&lt;span style="color:#f92672">:&lt;/span> &lt;span style="color:#e6db74">&amp;#34;Han Solo&amp;#34;&lt;/span>}})
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">const&lt;/span> &lt;span style="color:#a6e22e">emailInput&lt;/span> &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#a6e22e">screen&lt;/span>.&lt;span style="color:#a6e22e">getByPlaceholderText&lt;/span>(&lt;span style="color:#e6db74">&amp;#34;Your e-mail&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">fireEvent&lt;/span>.&lt;span style="color:#a6e22e">change&lt;/span>(&lt;span style="color:#a6e22e">emailInput&lt;/span>, {&lt;span style="color:#a6e22e">target&lt;/span>&lt;span style="color:#f92672">:&lt;/span> {&lt;span style="color:#a6e22e">value&lt;/span>&lt;span style="color:#f92672">:&lt;/span> &lt;span style="color:#e6db74">&amp;#34;i-shot-first@coruscant.sw&amp;#34;&lt;/span>}})
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">const&lt;/span> &lt;span style="color:#a6e22e">recordedRadio&lt;/span> &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#a6e22e">screen&lt;/span>.&lt;span style="color:#a6e22e">getByLabelText&lt;/span>(&lt;span style="color:#e6db74">&amp;#34;Recorded Demo&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">fireEvent&lt;/span>.&lt;span style="color:#a6e22e">click&lt;/span>(&lt;span style="color:#a6e22e">recordedRadio&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">const&lt;/span> &lt;span style="color:#a6e22e">submitButton&lt;/span> &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#a6e22e">screen&lt;/span>.&lt;span style="color:#a6e22e">getByDisplayValue&lt;/span>(&lt;span style="color:#e6db74">&amp;#34;Submit&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">expect&lt;/span>(&lt;span style="color:#a6e22e">submitButton&lt;/span>).&lt;span style="color:#a6e22e">toBeInTheDocument&lt;/span>()
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">fireEvent&lt;/span>.&lt;span style="color:#a6e22e">click&lt;/span>(&lt;span style="color:#a6e22e">submitButton&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">await&lt;/span> &lt;span style="color:#a6e22e">waitFor&lt;/span>(() =&amp;gt; {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">expect&lt;/span>(&lt;span style="color:#a6e22e">submitButton&lt;/span>).&lt;span style="color:#a6e22e">not&lt;/span>.&lt;span style="color:#a6e22e">toBeInTheDocument&lt;/span>()
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">const&lt;/span> &lt;span style="color:#a6e22e">thankYouPage&lt;/span> &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#a6e22e">screen&lt;/span>.&lt;span style="color:#a6e22e">getByText&lt;/span>(
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#e6db74">&amp;#34;Thank you! We will send you a confirmation e-mail.&amp;#34;&lt;/span>,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> )
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">expect&lt;/span>(&lt;span style="color:#a6e22e">thankYouPage&lt;/span>).&lt;span style="color:#a6e22e">toBeInTheDocument&lt;/span>()
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> })
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>})
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Great, this test passes! Job done, right? After all, our application is forwarding the request payload of &lt;code>{&amp;quot;demoType&amp;quot;: &amp;quot;recorded&amp;quot;, &amp;quot;name:&amp;quot; &amp;quot;Han Solo&amp;quot;, &amp;quot;email&amp;quot;: &amp;quot;i-shot-first@coruscant.sw&amp;quot;}&lt;/code> to the API. Well, &amp;hellip; it doesn&amp;rsquo;t. Our test let a bug slip: instead of forwarding &lt;code>&amp;quot;demoType&amp;quot;: &amp;quot;recorded&amp;quot;&lt;/code>, it sends &lt;code>&amp;quot;demoType&amp;quot;: &amp;quot;change&amp;quot;&lt;/code>. Why is that? This is caused by a problem with the handler function of the radio buttons. We made a human error and sent &lt;code>event.type&lt;/code> instead of &lt;code>event.target.value&lt;/code>, probably because we were too focused on the word &amp;ldquo;type&amp;rdquo; in the &lt;code>setDemoType&lt;/code> function. But we can fix this:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-diff" data-lang="diff">&lt;span style="display:flex;">&lt;span> // Inside App.js
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> const radioButtonHandler = (event) =&amp;gt; {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#f92672">- setDemoType(event.type)
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#f92672">&lt;/span>&lt;span style="color:#a6e22e">+ setDemoType(event.target.value)
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">&lt;/span> }
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>But our test still doesn&amp;rsquo;t confirm whether the right properties are forwarded. Yes, we land on the Thank You page if we get a 200 status code from the backend; but we don&amp;rsquo;t know whether we send the right things. This is where mock-inspect comes to the rescue.&lt;/p>
&lt;p>Whenever you mock a network request using mock-inspect&amp;rsquo;s &lt;code>mockRequest&lt;/code> method, you receive a class instance. This instance contains a few useful methods, such as the method &lt;code>expectRequestToHaveBeenMade()&lt;/code> which would throw an error if the application hasn&amp;rsquo;t made the request. Another useful method is &lt;code>inspect()&lt;/code> which will return the request payload and the request headers which have been used by the application when it made the request. Should the request not have been made at the time of calling &lt;code>inspect()&lt;/code>, the method would throw an error (as it internally calls &lt;code>expectRequestToHaveBeenMade()&lt;/code>).&lt;/p>
&lt;p>Thanks to the &lt;code>inspect()&lt;/code> method, we can complete our test and assert that the frontend application does indeed forward the correct properties. Now we really can be sure everything works as expected!&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-diff" data-lang="diff">&lt;span style="display:flex;">&lt;span>test(&amp;#34;shows Thank You page on submitting the form&amp;#34;, async () =&amp;gt; {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#f92672">- mockRequest({
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#f92672">&lt;/span>&lt;span style="color:#a6e22e">+ const demoRequest = mockRequest({
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">&lt;/span> requestPattern: &amp;#34;https://someapi.com/request-demo&amp;#34;,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> requestMethod: &amp;#34;POST&amp;#34;,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> responseStatus: 200,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> })
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> render(&amp;lt;App /&amp;gt;)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> const nameInput = screen.getByPlaceholderText(&amp;#34;Your name&amp;#34;)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> fireEvent.change(nameInput, {target: {value: &amp;#34;Han Solo&amp;#34;}})
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> const emailInput = screen.getByPlaceholderText(&amp;#34;Your e-mail&amp;#34;)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> fireEvent.change(emailInput, {target: {value: &amp;#34;i-shot-first@coruscant.sw&amp;#34;}})
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> const recordedRadio = screen.getByLabelText(&amp;#34;Recorded Demo&amp;#34;)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> fireEvent.click(recordedRadio)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> const submitButton = screen.getByDisplayValue(&amp;#34;Submit&amp;#34;)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> expect(submitButton).toBeInTheDocument()
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> fireEvent.click(submitButton)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> await waitFor(() =&amp;gt; {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> expect(submitButton).not.toBeInTheDocument()
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> const thankYouPage = screen.getByText(
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &amp;#34;Thank you! We will send you a confirmation e-mail.&amp;#34;,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> )
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> expect(thankYouPage).toBeInTheDocument()
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> })
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">+ // is also called inside `inspect()` - here for clarity
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">+ demoRequest.expectRequestToHaveBeenMade()
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">+ const {requestBody} = demoRequest.inspect()
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">+ expect(requestBody).toEqual({
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">+ name: &amp;#34;Han Solo&amp;#34;,
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">+ email: &amp;#34;i-shot-first@coruscant.sw&amp;#34;,
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">+ demoType: &amp;#34;recorded&amp;#34;,
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">+ })
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">&lt;/span>})
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="conclusion">Conclusion&lt;/h2>
&lt;p>We have seen how mock-inspect can help you write better and more meaningful tests. If you want to learn more about the library, head over to our &lt;a href="http://github.com/trayio/mock-inspect">GitHub page&lt;/a> where you can find detailed instructions on how to use mock-inspect. There, we also outline other functionalities we haven&amp;rsquo;t discussed yet, such as how to handle GraphQL and how to auto-generate responses using a GraphQL schema!&lt;/p>
&lt;p>Keep an eye out for other upcoming articles &lt;a href="https://rickschubert.net/blog/tags/mock-inspect">in this series&lt;/a>. In future blog posts, we will look at how mock-inspect works behind the scenes and how it can be used to facilitate contract testing.&lt;/p>
&lt;hr>
&lt;p>&lt;a href="https://github.com/trayio/mock-inspect/tree/main/assets/chameleon.png">Chameleon&lt;/a> graphic by &lt;a href="https://pixabay.com/users/monstreh-637659/?utm_source=link-attribution&amp;amp;utm_medium=referral&amp;amp;utm_campaign=image&amp;amp;utm_content=3340930">Анна Куликова&lt;/a> from &lt;a href="https://pixabay.com/?utm_source=link-attribution&amp;amp;utm_medium=referral&amp;amp;utm_campaign=image&amp;amp;utm_content=3340930">Pixabay&lt;/a>&lt;/p></description></item><item><title>Windows Subsystem for Linux</title><link>https://rickschubert.net/blog/posts/windows-linux-subsystem/</link><pubDate>Sat, 20 Feb 2021 19:09:03 +0000</pubDate><guid>https://rickschubert.net/blog/posts/windows-linux-subsystem/</guid><description>&lt;p>I have been using the Linux subsystem on Windows for a while now and I am quite thrilled about it. I do think that this is something everyone who is doing development on a Windows computer should set up.&lt;/p>
&lt;h2 id="what-the-windows-subsystem-for-linux-is-and-what-it-isnt">What the Windows Subsystem for Linux is (and what it isn&amp;rsquo;t)&lt;/h2>
&lt;p>For a few years now, thanks to Windows 10, it has been possible to have a Linux subsystem available on your Windows operating system (often referred to as WSL). It is important to note that this just exposes a &lt;em>shell&lt;/em> - you are still regularly interacting with Windows. Effectively, you just have an app that launches a Linux terminal. There is therefore no way to use graphical Linux tools such as video software and the Linux subsystem is also not a virtual machine.&lt;/p>
&lt;figure>
&lt;img src="https://rickschubert.net/blog/images/wsl.PNG" />
&lt;figcaption>
&lt;h4>The Linux subsystem in action&lt;/h4>
&lt;/figcaption>
&lt;/figure>
&lt;p>Why would you want to use it? At work, I am using a Mac which is a UNIX operating system just like Linux. At least in my line of work (web development, testing and DevOps) developing on Linux is extremely common because this is usually the target operating system on which our programs run. Furthermore, there are a lot of command-line tools out there for Linux which don&amp;rsquo;t have a Windows equivalent. And even if these exist, it is simply nicer to work with the tools one already knows. This goes especially for scripting: bash/sh is a powerful scripting language and I have gotten quite good at it over the years - the same can &lt;em>not&lt;/em> be said about my scripting skills with Windows and &lt;code>.bat&lt;/code> files. I have a few projects (like my website) where I wrote node.js scripts to automate build tasks just because I couldn&amp;rsquo;t use bash and struggled with &lt;code>.bat&lt;/code> files.&lt;/p>
&lt;p>Yes, I could have a second operating system on my computer which would eliminate the need for a subsystem - but this comes with a different set of problems. In fact, I do have an installation of &lt;a href="https://elementary.io/">elementary OS&lt;/a> on my computer and have dabbled with it for years but never managed to iron out the problems. The biggest hurdle is that I have a widescreen monitor but a very old graphics card. Getting Linux to display in full widescreen format is a task I could not achieve even after days of configuration - and judging from the hundreds of forum posts out there I am not alone with this.&lt;/p>
&lt;p>My Windows dependent situation is amplified by the fact that my eyes &lt;em>need&lt;/em> the app &lt;a href="https://justgetflux.com/">f.lux&lt;/a> which takes away the eye-straining blue light elements. There is also a Linux version but it is far inferior to the Windows and Mac pendants. The final nail in the coffin was that I have an extensive set of &lt;a href="https://www.autohotkey.com/">AutoHotkey&lt;/a> scripts to automate dozens of tasks with keyboard shortcuts - which is Windows only. On top of that, in order to switch operating systems while booting, I have to activate the &lt;a href="https://help.corsair.com/hc/en-us/articles/360053198052-How-to-Use-a-CORSAIR-keyboard-to-enter-BIOS-mode">BIOS mode on my Corsair keyboard&lt;/a> as it otherwise only works once the keyboard driver installed on the operating system kicks in.&lt;/p>
&lt;h2 id="installation-and-setup">Installation and Setup&lt;/h2>
&lt;p>Installing the subsystem was relatively easy, there is a &lt;a href="https://docs.microsoft.com/en-us/windows/wsl/install-win10">comprehensive guide on the Microsoft website&lt;/a>. The word &amp;ldquo;relative&amp;rdquo; should be pointed out here as it is not as easy as installing let&amp;rsquo;s say a web browser - but hey, you are diving into the world of Linux. Get used to pain! Once you have the subsystem installed, you will have an app called &amp;ldquo;Ubuntu&amp;rdquo; available in your start menu which opens a Linux shell. All the standard shell commands work there, such as &lt;code>uname -r&lt;/code> which shows the name of the Linux installation. In my case, this is &lt;code>4.4.0-19041-Microsoft&lt;/code>.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-sh" data-lang="sh">&lt;span style="display:flex;">&lt;span>$ uname -r
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>4.4.0-19041-Microsoft
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>By default, the standard Ubuntu shell is rather unpleasing, both visually as well as functionally. Therefore your next first step should be to install zsh. I found &lt;a href="https://blog.joaograssi.com/windows-subsystem-for-linux-with-oh-my-zsh-conemu/">a wonderful article by Joao Grassi&lt;/a> which goes into great detail on how to set it up. An important step here is to use the Windows application ConEmu which is the Windows equivalent to iTerm2 - a comfortable shell window manager which many features such as splitting windows.&lt;/p>
&lt;h2 id="usage-and-development">Usage and Development&lt;/h2>
&lt;p>Let&amp;rsquo;s get to the fun bit - developing with Linux on your Windows machine. Installing programming languages and tools works like on Ubuntu - don&amp;rsquo;t forget, you &lt;em>are&lt;/em> using Ubuntu now! Thus, there is a lot of &lt;code>apt-get&lt;/code> involved as well as downloading tools via curl and moving them into &lt;code>/usr/bin&lt;/code> (or adding them to the &lt;code>$PATH&lt;/code> variable on each shell start). This is quite pleasant since most tutorials and Getting Started guides are usually written for UNIX systems. I&amp;rsquo;m sure every Windows developer has been in a situation at least once where things errored on installing - no such problems with WSL.&lt;/p>
&lt;p>While you are in the Linux subsystem, you have access to all the files on your Windows computer using the &lt;code>/mnt&lt;/code> directory. For example, my Windows directory &lt;code>C:\code&lt;/code> can be accessed from within the Linux subsystem on &lt;code>/mnt/c/code&lt;/code>.&lt;/p>
&lt;p>I have all my programming languages installed in WSL but some of these I also have installed on Windows, i.e. Golang. This is extremely useful because that way I can compile programs both for Linux and for Windows on the same computer.&lt;/p>
&lt;h3 id="visual-studio-code-for-windows-linux-subsystems">Visual Studio Code for Windows Linux Subsystems&lt;/h3>
&lt;p>Having a separate Linux shell is great but you don&amp;rsquo;t want to do everything in the terminal, especially when it comes to writing code. Sure, some people love vim but most of us are probably dependent on actual text editors and IDEs. This is where Visual Studio Code comes heroically to the rescue with its incredible &lt;a href="https://code.visualstudio.com/docs/remote/wsl">Remote WSL&lt;/a> extension. Once this extension is installed, you can launch Visual Studio Code on Windows like you usually would - but VSCode will run on your Linux subsystem!&lt;/p>
&lt;figure>
&lt;img src="https://rickschubert.net/blog/images/wsl_vscode.PNG" />
&lt;figcaption>
&lt;h4> This icon shows you that VSCode is running inside the Linux subsystem on Windows&lt;/h4>
&lt;/figcaption>
&lt;/figure>
&lt;p>The important point here is that the entire process of VSCode will run on the Linux subsystem. What this means in action: For example, when you are installing language formatters such as for Golang, these will go into your Linux subshell. For me, this is ground-breaking because it means that I can for the first time comfortably develop in Golang using Visual Studio Code. Many of the tools used by the Golang VSCode extension did not work properly on Windows. For years, I was reluctantly living with these problems. But now when using Linux, these formatters and tools suddenly all work as they should. It&amp;rsquo;s incredible.&lt;/p>
&lt;hr>
&lt;p>In summary, the Windows subsystem for Linux is a fantastic addition that should be a strong consideration of every Windows developer&amp;rsquo;s toolchain. While the setup process is not easy, it is worth it. My prediction is that it will find a wide adoption over the next few years and will only advance the strong position UNIX operating systems have in the development community.&lt;/p></description></item><item><title>How to make a blog such as this one with Hugo</title><link>https://rickschubert.net/blog/posts/how-i-make-this-blog-with-gohugo/</link><pubDate>Fri, 01 Jan 2021 15:29:34 +0000</pubDate><guid>https://rickschubert.net/blog/posts/how-i-make-this-blog-with-gohugo/</guid><description>&lt;p>Creating a new article in this blog is a matter of writing a new text file and pushing it to my GitHub repository - while keeping it 100% free. Here&amp;rsquo;s how I achieved it to be this simple.&lt;/p>
&lt;ol>
&lt;li>&lt;a href="#generate">How to generate the website&lt;/a>&lt;/li>
&lt;li>&lt;a href="#host">How to host the website&lt;/a>&lt;/li>
&lt;/ol>
&lt;h2 id="a-namegenerateahow-to-generate-the-website">&lt;a name="generate">&lt;/a>How to generate the website&lt;/h2>
&lt;p>I use the tool &lt;a href="https://gohugo.io/">Hugo&lt;/a> to create this blog. Hugo is a static site generator - this means that you simply feed it with some text files and it then spits out all of the files which form a complete web page. Although Hugo is written in Go, it is published as a CLI tool - there is no Go knowledge necessary to use it.&lt;/p>
&lt;p>Setting up the website was fairly easy, I literally followed the &lt;a href="https://gohugo.io/getting-started/quick-start/">Quick Start&lt;/a> guide in the docs. There are a few things which I did differently and I want to outline these to make it easier on people who also want to start their own blog.&lt;/p>
&lt;p>Hugo is based around the concept of templates - instead of you having to create your own website from scratch, you can access other people&amp;rsquo;s page templates and make them your own. Anyone can create templates and there are literally hundreds of them for free &lt;a href="https://themes.gohugo.io/">on their website&lt;/a>. This is how I found the &lt;a href="https://github.com/Mitrichius/hugo-theme-anubis">Anubis theme&lt;/a>. As I wanted to tweak this theme and adjust it to my personal needs, I created a &lt;a href="https://github.com/rickschubert/hugo-theme-anubis">GitHub fork of the project&lt;/a>. If you have a theme you consider using, I would highly advise you to also fork it right away as it gravely simplifies the later modification process. Once the theme was forked, I had to pull it in as a git submodule: &lt;code>git submodule add https://github.com/rickschubert/hugo-theme-anubis themes/anubis&lt;/code>.&lt;/p>
&lt;p>From there on, working with Hugo is a charm: &lt;code>hugo new posts/my-new-post.md&lt;/code> will create a new markdown file in the directory &lt;code>posts&lt;/code> where all the blog posts live. In case you haven&amp;rsquo;t seen markdown before, this is literally how the start of this article looks like!&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-md" data-lang="md">&lt;span style="display:flex;">&lt;span>---
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>title: &amp;#34;How to make a blog such as this one with Hugo&amp;#34;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>date: 2021-01-01T15:29:34Z
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>draft: true
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>tags: [&amp;#34;programming&amp;#34;]
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>---
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>I am really happy with the tools I use for my blog [...]
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>I use the tool [&lt;span style="color:#f92672">Hugo&lt;/span>](&lt;span style="color:#a6e22e">https://gohugo.io/&lt;/span>) to create this blog.
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Since the articles are markdown, writing new posts becomes a breeze. I write all of my articles in Visual Studio Code with its amazing &amp;ldquo;Zen Mode&amp;rdquo; feature. Trust me, I wrote a full play with this program - it&amp;rsquo;s amazing! Since all posts are plain text, I can copy and paste them wherever necessary; I often move my texts to grammarly.com in order to perform a quick spell-check. (I am the king of typos, I know.)&lt;/p>
&lt;figure>
&lt;img src="https://rickschubert.net/blog/images/vscode_zen_mode.PNG" />
&lt;figcaption>
&lt;h4>Visual Studio Code Zen Mode in action&lt;/h4>
&lt;/figcaption>
&lt;/figure>
&lt;p>If all is done, the static website files can be generated using &lt;code>hugo --minify&lt;/code> which creates a directory &lt;code>public/&lt;/code> - easy as that!&lt;/p>
&lt;h2 id="a-namehostahow-to-host-the-website">&lt;a name="host">&lt;/a>How to host the website&lt;/h2>
&lt;p>My hosting solution is fully automated and 100% free - two attributes I am incredibly happy about. I use the &lt;a href="https://pages.github.com/">GitHub pages&lt;/a> feature which makes it possible to transform GitHub repositories into websites. I am using this feature already for my main page &lt;a href="https://rickschubert.net">https://rickschubert.net&lt;/a> which actually lives under &lt;a href="https://rickschubert.github.io">https://rickschubert.github.io&lt;/a> (as &lt;code>rickschubert&lt;/code> is my GitHub user name). I then &lt;a href="https://docs.github.com/en/free-pro-team@latest/github/working-with-github-pages/configuring-a-custom-domain-for-your-github-pages-site">updated the DNS configuration of my domain to point to the GitHub server&lt;/a>.&lt;/p>
&lt;p>If you already have one repository that is your main GitHub page, any other repository which you want to host via GitHub pages becomes accessible as path with the name of the repository - so a repository &lt;code>/blog&lt;/code> would be accessible under &lt;a href="https://rickschubert.github.io/blog">https://rickschubert.github.io/blog&lt;/a>. But since I have the DNS redirects set up over at my domain provider Google Domains, the redirects also work for the blog!&lt;/p>
&lt;p>My workflow of bringing the files to GitHub is fairly ingenious. While &lt;a href="https://github.com/rickschubert/blog">https://github.com/rickschubert/blog&lt;/a> contains the minified and uglified static website files which need to be open to the public, the actual source code of the website is stored in a private repository. That repository has GitHub actions configured in such a way that on every push to that private repository, the &lt;code>hugo --minify&lt;/code> command gets run to build the static website. Another task then moves these output files to my &lt;code>/blog&lt;/code> repository which is open to the public. See below my GitHub actions file (&lt;code>.github/workflows/hugo.yml&lt;/code>) for how I achieve this:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-yaml" data-lang="yaml">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#f92672">name&lt;/span>: &lt;span style="color:#ae81ff">Website publish&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#f92672">on&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">push&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">branches&lt;/span>: [ &lt;span style="color:#ae81ff">master ]&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#f92672">jobs&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">deploy&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">runs-on&lt;/span>: &lt;span style="color:#ae81ff">ubuntu-18.04&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">steps&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> - &lt;span style="color:#f92672">uses&lt;/span>: &lt;span style="color:#ae81ff">actions/checkout@v2&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">with&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">submodules&lt;/span>: &lt;span style="color:#66d9ef">true&lt;/span> &lt;span style="color:#75715e"># Fetch Hugo themes (true OR recursive)&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">fetch-depth&lt;/span>: &lt;span style="color:#ae81ff">0&lt;/span> &lt;span style="color:#75715e"># Fetch all history for .GitInfo and .Lastmod&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> - &lt;span style="color:#f92672">name&lt;/span>: &lt;span style="color:#ae81ff">Setup Hugo&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">uses&lt;/span>: &lt;span style="color:#ae81ff">peaceiris/actions-hugo@v2&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">with&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">hugo-version&lt;/span>: &lt;span style="color:#e6db74">&amp;#39;0.79.1&amp;#39;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> - &lt;span style="color:#f92672">name&lt;/span>: &lt;span style="color:#ae81ff">Build&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">run&lt;/span>: &lt;span style="color:#ae81ff">HUGO_ENV=production hugo --minify&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> - &lt;span style="color:#f92672">name&lt;/span>: &lt;span style="color:#ae81ff">Pushes to another repository&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">uses&lt;/span>: &lt;span style="color:#ae81ff">cpina/github-action-push-to-another-repository@master&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">env&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">API_TOKEN_GITHUB&lt;/span>: &lt;span style="color:#ae81ff">${{ secrets.API_TOKEN_GITHUB }}&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">with&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">source-directory&lt;/span>: &lt;span style="color:#e6db74">&amp;#39;public&amp;#39;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">destination-github-username&lt;/span>: &lt;span style="color:#e6db74">&amp;#39;rickschubert&amp;#39;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">destination-repository-name&lt;/span>: &lt;span style="color:#e6db74">&amp;#39;blog&amp;#39;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">user-email&lt;/span>: &lt;span style="color:#ae81ff">rickschubert@gmx.de&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">target-branch&lt;/span>: &lt;span style="color:#ae81ff">gh-pages&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;hr>
&lt;p>On the Hugo website, the tag line reads &amp;ldquo;Hugo makes building websites fun again&amp;rdquo; - I have to agree! The concept of shareable themes takes all the pain from content producers, especially for small projects such as this blog. Instead of having to spend countless hours designing and updating my website I get the chance to focus on writing articles. Thanks, Hugo!&lt;/p></description></item><item><title>Volunteering With My Programming Skills For A Non-Commercial Radio Station</title><link>https://rickschubert.net/blog/posts/volunteering-with-tech-skills-golang-music-library-reader/</link><pubDate>Wed, 30 Dec 2020 14:17:19 +0000</pubDate><guid>https://rickschubert.net/blog/posts/volunteering-with-tech-skills-golang-music-library-reader/</guid><description>&lt;p>&lt;a href="https://www.youtube.com/watch?v=Z3zD7z2vKZ8">During the Crimbo Limbo&lt;/a> period between Christmas and New Years, I volunteered with my programming skills. That&amp;rsquo;s how it came to be that I built a tool to create a browsable music library for the non-commercial radio station &lt;a href="https://greatamericansongbook.info">Great American Songbook&lt;/a>. This article discusses why and how.&lt;/p>
&lt;ol>
&lt;li>&lt;a href="#why">How I came to volunteer&lt;/a>&lt;/li>
&lt;li>&lt;a href="#tool">Building the tool&lt;/a>&lt;/li>
&lt;li>&lt;a href="#final">Final thoughts&lt;/a>&lt;/li>
&lt;/ol>
&lt;h2 id="a-namewhyahow-i-came-to-volunteer">&lt;a name="why">&lt;/a>How I came to volunteer&lt;/h2>
&lt;p>For me, programming is like an itch. If I haven&amp;rsquo;t programmed in a while I start to feel fidgety. Furthermore, I am deeply passionate about automation and eliminating &lt;a href="https://www.strike.coop/bullshit-jobs/">bullshit jobs&lt;/a>. This was actually how I started programming in the first place - back in my time when I was a linguist there had been dozens of manual tasks multiple people (including me) performed over and over. I got so annoyed by it that I taught myself how to program in order to get rid of these.&lt;/p>
&lt;p>Volunteering was something I had considered already for a long time. I have approached multiple organisations over the past year, i.e. local hospitals at the height of coronavirus pressures. Most of the time, I either never heard back or have been told there was no need for help. It is surprising through how many hoops one has to jump just to work for free!&lt;/p>
&lt;p>The Great American Songbook is a radio station I have been listening to for many years now. Swing music and dance are big passions for me. I donated some money to that radio station and that&amp;rsquo;s how I thought again about volunteering: couldn&amp;rsquo;t I donate my time and skills additionally to money? That&amp;rsquo;s how I contacted the radio station and almost instantly received an answer.&lt;/p>
&lt;p>During a zoom call, the radio producer walked me through the website&amp;rsquo;s setup and how the radio stream operates. In my personal circle, I don&amp;rsquo;t have many people of age and no one who could let alone operate a tablet. Therefore, I was incredibly impressed by the radio producer who is a pensioner and runs the radio station completely out of their own pocket. They have a setup of multiple servers, a website with SSH certificate, templates from GitHub, site updates using FTP, &amp;hellip; - and they browsed through their media library using FreeCommander like a champ!&lt;/p>
&lt;p>Our initial conversation immediately lay bare automation needs: On the website, there is a section which lists the complete music library. That way, listeners know what songs are available (and which aren&amp;rsquo;t) so that they can request songs to be played more frequently or ask for new songs to be added to the rotation. Creating this PDF was a constant long-lasting pain to the radio producer, they only did it a few times a year because it took so long. This is where my tool comes in!&lt;/p>
&lt;h2 id="a-nametoolabuilding-the-tool">&lt;a name="tool">&lt;/a>Building the tool&lt;/h2>
&lt;div class="callout">
The code for this project is open-source and &lt;a href="https://github.com/rickschubert/music-library-reader">can be found on GitHub&lt;/a>.
&lt;/div>
&lt;p>The task at hand was to crawl through the entire music collection of the radio producer and to generate a sorted list of all the tracks. This list should be sorted either by the artist or title. It should also be possible to exclude subdirectories from the generated catalogue.&lt;/p>
&lt;p>It was clear to me that I wanted to have the catalogue displayed in an interactive table using HTML so that listeners can filter and sort easily. I also decided to add CSV export functionality to my tool so that the radio producer could still convert the list into a PDF if needed.&lt;/p>
&lt;p>I wanted to build a tool which could be executed by the radio producer without any of my input - that&amp;rsquo;s what automation is about after all: empowering the user. This is why I decided to use the Go programming language. Although I&amp;rsquo;m an expert in JavaScript/TypeScript, it is not a great solution for tools that should be executed by people/machines outside your control. I wouldn&amp;rsquo;t want to give the radio producer a list of things they need to install and commands they need to execute! With Go, I can compile the tool into one single executable file which the radio producer can start with a simple double click as they would do with any other program.&lt;/p>
&lt;p>Once I knew all these requirements, I immediately started programming. I created a small command-line tool using the &lt;a href="https://github.com/Songmu/prompter">prompter&lt;/a> library. I decided against any command-line flags and arguments as the tool was supposed to be used by non-techy people as well with just a double click. Therefore, I think it is way better to use a solution like prompter which asks the user for their input. That way, you can also give the user hints and examples. Unlike with avoidable &lt;code>--help&lt;/code> messages, these prompts will definitely be seen by the user, there is no way around them. The prompter library is also incredibly easy to use:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-go" data-lang="go">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">func&lt;/span> &lt;span style="color:#a6e22e">promptForDirectory&lt;/span>() &lt;span style="color:#66d9ef">string&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">directory&lt;/span> &lt;span style="color:#f92672">:=&lt;/span> &lt;span style="color:#a6e22e">prompter&lt;/span>.&lt;span style="color:#a6e22e">Prompt&lt;/span>(
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#e6db74">&amp;#34;Please provide us with the path of the &amp;#34;&lt;/span> &lt;span style="color:#f92672">+&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#e6db74">&amp;#34;directory where all your music lies. I.e. C:\\Music&amp;#34;&lt;/span>,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#e6db74">&amp;#34;&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> )
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">if&lt;/span> &lt;span style="color:#a6e22e">directory&lt;/span> &lt;span style="color:#f92672">==&lt;/span> &lt;span style="color:#e6db74">&amp;#34;&amp;#34;&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">log&lt;/span>.&lt;span style="color:#a6e22e">Fatal&lt;/span>(&lt;span style="color:#e6db74">&amp;#34;You need to enter a valid path&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">return&lt;/span> &lt;span style="color:#a6e22e">directory&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>One of the trickiest bits for me was to recursively iterate over all directories and subdirectories. I use the &lt;code>filepath.Walk()&lt;/code> function which already works recursively out of the box! Due to my familiarity with JavaScript, I hadn&amp;rsquo;t expected this though and assumed subdirectories were ignored. This is why I initially created unnecessary duplication where I would recursively call &lt;code>filepath.Walk&lt;/code> again for every subdirectory - madness!&lt;/p>
&lt;p>Golang is a highly readable programming language if done well - it requires the user to abstract things out a bit though. Consider the below code where I I try to determine whether a file path (&lt;code>path&lt;/code>) starts with any item of the &lt;code>ignoredDirectories&lt;/code> list. The code itself is quite readable but I have it placed in the midst of another function - I should have created a helper function called &lt;code>Find()&lt;/code> instead. We live to make mistakes 🤷.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-go" data-lang="go">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">// This should be its own function, something like `Find()`
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">&lt;/span>&lt;span style="color:#66d9ef">var&lt;/span> &lt;span style="color:#a6e22e">shouldBeIgnored&lt;/span> &lt;span style="color:#66d9ef">bool&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">for&lt;/span> &lt;span style="color:#a6e22e">_&lt;/span>, &lt;span style="color:#a6e22e">ignoredDirectory&lt;/span> &lt;span style="color:#f92672">:=&lt;/span> &lt;span style="color:#66d9ef">range&lt;/span> &lt;span style="color:#a6e22e">ignoredDirectories&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">if&lt;/span> &lt;span style="color:#a6e22e">ignoredDirectory&lt;/span> &lt;span style="color:#f92672">!=&lt;/span> &lt;span style="color:#e6db74">&amp;#34;&amp;#34;&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">if&lt;/span> &lt;span style="color:#a6e22e">strings&lt;/span>.&lt;span style="color:#a6e22e">HasPrefix&lt;/span>(&lt;span style="color:#a6e22e">strings&lt;/span>.&lt;span style="color:#a6e22e">ToLower&lt;/span>(&lt;span style="color:#a6e22e">path&lt;/span>), &lt;span style="color:#a6e22e">strings&lt;/span>.&lt;span style="color:#a6e22e">ToLower&lt;/span>(&lt;span style="color:#a6e22e">ignoredDirectory&lt;/span>)) {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">shouldBeIgnored&lt;/span> = &lt;span style="color:#66d9ef">true&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">break&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Reading out artist name etc. from MP3 files was fairly straight-forward thanks to the &lt;a href="https://github.com/dhowden/tag">tag&lt;/a> library. The same goes for creating CSV files which works with the built-in library &lt;code>encoding/csv&lt;/code>.&lt;/p>
&lt;p>More interesting - and challenging - was creating the catalogue as HTML table. Some people surely wonder whether I went with some grand solution like a serverless database and API calls to fetch items - way too complicated! Remember our goals: replacing the PDF file and empowering the radio producer. We wouldn&amp;rsquo;t want to teach them about database access, requiring them to host things, or me hosting things for them which might not work any longer if I don&amp;rsquo;t maintain it. Instead, I went for a simple and proven solution: Bake all the songs as table rows into a static HTML file.&lt;/p>
&lt;p>The main idea of this tool was to have one single executable to give the radio producer. Therefore, having HTML mustache templates was out of the question as well. This is why I ended up hard-coding an actual string into my &lt;code>.go&lt;/code> file which represents the base HTML skeleton. This skeleton is then enriched with all the table rows for each song where each item is converted into a &lt;code>&amp;lt;tr&amp;gt;&lt;/code> element.&lt;/p>
&lt;p>I initially created my own basic HTML table including custom filter functions but I was shocked by the atrocious performance of my naive scripts. The Great American Songbook music library contains 16.000 items - due to that size there is a need for smart filters. This is how I stumbled over the &lt;a href="https://datatables.net/">DataTables project&lt;/a> which provides a fantastic out of the box solution for displaying tables, including pagination and filtering. Even on tables with 16.000 rows the search and sorting functionality works tremendously fast and stable.&lt;/p>
&lt;p>While the filtering is fast, I had to do some trickery in regards to page loads. Because all of the &lt;code>&amp;lt;tr&amp;gt;&lt;/code> elements are in the HTML page, the final page is 1.48MB large. This is a good decrease in size compared to the PDF (2.5MB) but it&amp;rsquo;s still quite large to load for browsers. It turns out that if there are more than 16.000 elements on the page, your browser has to do quite a lot of work to process all of these items. This is why I had a look around and used the solution from &lt;a href="https://patdavid.net/2019/02/displaying-a-big-html-table/">patdavid.net&lt;/a> where I first hide the table on page load. Instead, the site is showing a loading spinner. Once all elements are processed by the browser and in the DOM, I hide the loading spinner and display the table. I am using the jQuery event method &lt;code>$(document).ready()&lt;/code> for that.&lt;/p>
&lt;p>The end result works pretty well and the radio producer was very happy about it - what previously took hours became fully automated and finished in around 60 seconds!&lt;/p>
&lt;h3 id="implementing-it-into-the-webpage">Implementing it into the webpage&lt;/h3>
&lt;p>The radio producer told me that they would be able to implement the solution as iframe on the website - which was why I went with a separate HTML file to begin with. After a few hours, they contacted me though to ask whether I could help out integrating it. Of course I could!&lt;/p>
&lt;p>Implementing the iframe into the existing website was quite tricky. The main problem was that I had to wrap my head around the website&amp;rsquo;s framework which the radio producer purchased from another developer. While greatamericansongbook.info is a single-page application, there aren&amp;rsquo;t any frameworks being used, it&amp;rsquo;s all vanilla JavaScript: everything in jQuery, all scripts somewhat minified (highly shortened variable and function names but at least line breaks and indentation left intact). An extra pain point was that the website doesn&amp;rsquo;t use any version control - it&amp;rsquo;s just a bunch of files on an FTP server.&lt;/p>
&lt;p>Another problem presented itself in having to have the iframe only loaded when the user requires to see it. As I discussed earlier, the HTML file for the table is very large. If you have an &lt;code>iframe&lt;/code> element in your HTML file with a &lt;code>src&lt;/code> attribute pointing to another page your browser will also load that other page though - even if the iframe isn&amp;rsquo;t immediately visible to users. This is why the page loads were suddenly quite slow. I solved it by removing the &lt;code>src&lt;/code> attribute from the &lt;code>iframe&lt;/code> element in the initial HTML file. I managed to find in the source files the function which is being called whenever the URL changes. This is how I added another jQuery listener which only fires when the URL contains &lt;code>#!/page_LIBRARY_TABLE&lt;/code> - in that case, I am adding to the &lt;code>iframe&lt;/code> the &lt;code>src&lt;/code> attribute leading to our big HTML table. Thus, the browser only loads our big table once it&amp;rsquo;s necessary.&lt;/p>
&lt;p>The final product looks something like this (might not be on the website yet, development version):&lt;/p>
&lt;figure>
&lt;img src="https://rickschubert.net/blog/images/gas_table_example.png" />
&lt;figcaption>
&lt;h4>The iframe embedded into the Great American Songbook page&lt;/h4>
&lt;/figcaption>
&lt;/figure>
&lt;h2 id="a-namefinalafinal-thoughts">&lt;a name="final">&lt;/a>Final thoughts&lt;/h2>
&lt;p>This was a fun and surprisingly challenging project which was right up my alley. I love that programming allows me to empower people: the radio producer doesn&amp;rsquo;t have to spend hours anymore having to create a PDF library and the listeners can browse the entire catalogue with ease, allowing them to become more engaged.&lt;/p>
&lt;p>Volunteering was a lot of fun and is something which I will want to do again. As outlined above, it isn&amp;rsquo;t always that easy to find suitable projects. I think I got really lucky with how trusting and open the radio producer was. Even more so, they immediately grasped what I meant when I said &amp;ldquo;I do automation&amp;rdquo; and they had a fitting task for me in mind right away. Thank you very much!&lt;/p></description></item><item><title>Flushing Promises in JavaScript - what it is and why you need it in UI tests</title><link>https://rickschubert.net/blog/posts/flushing-promises/</link><pubDate>Tue, 22 Dec 2020 17:32:41 +0000</pubDate><guid>https://rickschubert.net/blog/posts/flushing-promises/</guid><description>&lt;p>&amp;ldquo;This UI test is flaky, why does it sometimes fail?&amp;rdquo; - &amp;ldquo;Could it be because promises were not flushed?&amp;rdquo; - &amp;ldquo;Huuuuh?&amp;rdquo; Chances are high that frontend developers will have had conversations like this. From my experience, many developers are still unaware of what flushing promises means and why we need it in automated tests of UI components such as with React or Angular. Let&amp;rsquo;s shed some light on this.&lt;/p>
&lt;div class="callout">
All the example code can be found &lt;a href="https://github.com/rickschubert/flushing-promises-explanation">on this GitHub repository&lt;/a>.
&lt;/div>
&lt;h2 id="why-do-i-need-to-flush-promises">Why do I need to flush promises?&lt;/h2>
&lt;p>Consider the following case: I created a new UI component. On starting, it displays the text &amp;ldquo;Empty&amp;rdquo;. When the component renders, it starts a network request to a server. The server takes 1 second to return a response. Once the component receives the response, it replaces the text &amp;ldquo;Empty&amp;rdquo; with the response it received.&lt;/p>
&lt;details>
&lt;summary>Expand to see a demo GIF of the component:&lt;/summary>
&lt;figure>
&lt;img src="https://rickschubert.net/blog/images/flushing_promises_component_demo.gif" />
&lt;figcaption>
&lt;h4>A demo of our dummy component&lt;/h4>
&lt;/figcaption>
&lt;/figure>
&lt;/details>
&lt;p>The code of this dummy component:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-jsx" data-lang="jsx">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">import&lt;/span> &lt;span style="color:#a6e22e">React&lt;/span> &lt;span style="color:#a6e22e">from&lt;/span> &lt;span style="color:#e6db74">&amp;#34;react&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">export&lt;/span> &lt;span style="color:#66d9ef">async&lt;/span> &lt;span style="color:#66d9ef">function&lt;/span> &lt;span style="color:#a6e22e">makeNetworkRequest&lt;/span>() {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">const&lt;/span> &lt;span style="color:#a6e22e">response&lt;/span> &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#66d9ef">await&lt;/span> &lt;span style="color:#a6e22e">fetch&lt;/span>(&lt;span style="color:#e6db74">&amp;#34;http://localhost:3001/what-day-is-it&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">const&lt;/span> &lt;span style="color:#a6e22e">data&lt;/span> &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#66d9ef">await&lt;/span> &lt;span style="color:#a6e22e">response&lt;/span>.&lt;span style="color:#a6e22e">json&lt;/span>()
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">return&lt;/span> &lt;span style="color:#a6e22e">JSON&lt;/span>.&lt;span style="color:#a6e22e">stringify&lt;/span>(&lt;span style="color:#a6e22e">data&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">class&lt;/span> &lt;span style="color:#a6e22e">App&lt;/span> &lt;span style="color:#66d9ef">extends&lt;/span> &lt;span style="color:#a6e22e">React&lt;/span>.&lt;span style="color:#a6e22e">Component&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">constructor&lt;/span>(&lt;span style="color:#a6e22e">props&lt;/span>) {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">super&lt;/span>(&lt;span style="color:#a6e22e">props&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">this&lt;/span>.&lt;span style="color:#a6e22e">state&lt;/span> &lt;span style="color:#f92672">=&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">webResponse&lt;/span>&lt;span style="color:#f92672">:&lt;/span> &lt;span style="color:#e6db74">&amp;#34;Empty&amp;#34;&lt;/span>,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">async&lt;/span> &lt;span style="color:#a6e22e">componentDidMount&lt;/span>() {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">const&lt;/span> &lt;span style="color:#a6e22e">webResponse&lt;/span> &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#66d9ef">await&lt;/span> &lt;span style="color:#a6e22e">makeNetworkRequest&lt;/span>()
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">this&lt;/span>.&lt;span style="color:#a6e22e">setState&lt;/span>({&lt;span style="color:#a6e22e">webResponse&lt;/span>})
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">render&lt;/span>() {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">return&lt;/span> (
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &amp;lt;&lt;span style="color:#f92672">div&lt;/span> &lt;span style="color:#a6e22e">className&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">&amp;#34;App&amp;#34;&lt;/span>&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &amp;lt;&lt;span style="color:#f92672">div&lt;/span> &lt;span style="color:#a6e22e">className&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">&amp;#34;response&amp;#34;&lt;/span>&amp;gt;{&lt;span style="color:#66d9ef">this&lt;/span>.&lt;span style="color:#a6e22e">state&lt;/span>.&lt;span style="color:#a6e22e">webResponse&lt;/span>}&amp;lt;/&lt;span style="color:#f92672">div&lt;/span>&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &amp;lt;/&lt;span style="color:#f92672">div&lt;/span>&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> )
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">export&lt;/span> &lt;span style="color:#66d9ef">default&lt;/span> &lt;span style="color:#a6e22e">App&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>This component works, fantastic! So let&amp;rsquo;s write some automated tests for this.&lt;/p>
&lt;p>First, let&amp;rsquo;s test our network request function in isolation. We will use &lt;a href="https://github.com/nock/nock">the npm module nock&lt;/a> to mock the network request that would normally be made. This allows us to run the tests without our server. It will essentially fake the network request and have it always return a fake response, the JSON string &lt;code>{&amp;quot;A&amp;quot;: &amp;quot;mocked response&amp;quot;}&lt;/code>:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-js" data-lang="js">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">import&lt;/span> &lt;span style="color:#a6e22e">App&lt;/span>, {&lt;span style="color:#a6e22e">makeNetworkRequest&lt;/span>} &lt;span style="color:#a6e22e">from&lt;/span> &lt;span style="color:#e6db74">&amp;#34;./App&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">import&lt;/span> &lt;span style="color:#a6e22e">nock&lt;/span> &lt;span style="color:#a6e22e">from&lt;/span> &lt;span style="color:#e6db74">&amp;#34;nock&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">const&lt;/span> &lt;span style="color:#a6e22e">mockedWebResponse&lt;/span> &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#a6e22e">JSON&lt;/span>.&lt;span style="color:#a6e22e">stringify&lt;/span>(&lt;span style="color:#e6db74">&amp;#39;{&amp;#34;A&amp;#34;: &amp;#34;mocked response&amp;#34;}&amp;#39;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">function&lt;/span> &lt;span style="color:#a6e22e">setUpMock&lt;/span>() {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">nock&lt;/span>(&lt;span style="color:#e6db74">/.*/&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> .&lt;span style="color:#a6e22e">persist&lt;/span>()
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> .&lt;span style="color:#a6e22e">defaultReplyHeaders&lt;/span>({
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#e6db74">&amp;#34;access-control-allow-origin&amp;#34;&lt;/span>&lt;span style="color:#f92672">:&lt;/span> &lt;span style="color:#e6db74">&amp;#34;*&amp;#34;&lt;/span>,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#e6db74">&amp;#34;access-control-allow-credentials&amp;#34;&lt;/span>&lt;span style="color:#f92672">:&lt;/span> &lt;span style="color:#e6db74">&amp;#34;true&amp;#34;&lt;/span>,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> })
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> .&lt;span style="color:#a6e22e">get&lt;/span>(&lt;span style="color:#e6db74">/what-day-is-it/&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> .&lt;span style="color:#a6e22e">reply&lt;/span>(&lt;span style="color:#ae81ff">200&lt;/span>, &lt;span style="color:#a6e22e">mockedWebResponse&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">it&lt;/span>(&lt;span style="color:#e6db74">&amp;#34;Testing our network request&amp;#34;&lt;/span>, &lt;span style="color:#66d9ef">async&lt;/span> () =&amp;gt; {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">setUpMock&lt;/span>()
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">const&lt;/span> &lt;span style="color:#a6e22e">result&lt;/span> &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#66d9ef">await&lt;/span> &lt;span style="color:#a6e22e">makeNetworkRequest&lt;/span>()
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">expect&lt;/span>(&lt;span style="color:#a6e22e">result&lt;/span>).&lt;span style="color:#a6e22e">toEqual&lt;/span>(&lt;span style="color:#a6e22e">mockedWebResponse&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>})
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>This test passes, amazing. Take note that &lt;code>makeNetworkRequest&lt;/code> is an asynchronous function. Since we are using the &lt;code>async&lt;/code> and &lt;code>await&lt;/code> keywords, nodejs will hold up with the subsequent I/O executions and only populate the &lt;code>result&lt;/code> variable with a value once &lt;code>makeNetworkRequest&lt;/code> finishes.&lt;/p>
&lt;div class="callout">
An I/O execution is any standard task which doesn't perform asynchronously, i.e. adding up 2 + 2.
&lt;/div>
&lt;p>Let&amp;rsquo;s move on to testing our component. We will render the component using the &lt;a href="https://testing-library.com/docs/react-testing-library/intro/">Testing Library npm module&lt;/a> and then check whether the text shows our fixed response:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-js" data-lang="js">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">import&lt;/span> {&lt;span style="color:#a6e22e">render&lt;/span>} &lt;span style="color:#a6e22e">from&lt;/span> &lt;span style="color:#e6db74">&amp;#34;@testing-library/react&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">it&lt;/span>(&lt;span style="color:#e6db74">&amp;#34;Test if component renders web response&amp;#34;&lt;/span>, () =&amp;gt; {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">const&lt;/span> &lt;span style="color:#a6e22e">component&lt;/span> &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#a6e22e">render&lt;/span>(&lt;span style="color:#f92672">&amp;lt;&lt;/span>&lt;span style="color:#a6e22e">App&lt;/span> &lt;span style="color:#f92672">/&amp;gt;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">const&lt;/span> &lt;span style="color:#a6e22e">response&lt;/span> &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#a6e22e">component&lt;/span>.&lt;span style="color:#a6e22e">container&lt;/span>.&lt;span style="color:#a6e22e">querySelector&lt;/span>(&lt;span style="color:#e6db74">&amp;#34;.response&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">expect&lt;/span>(&lt;span style="color:#a6e22e">response&lt;/span>.&lt;span style="color:#a6e22e">textContent&lt;/span>).&lt;span style="color:#a6e22e">toEqual&lt;/span>(&lt;span style="color:#a6e22e">mockedWebResponse&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>})
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>But this test is failing. Why? &lt;strong>Because no promises have been flushed&lt;/strong>. Alternatively, because nodejs exited too early. What jest does here is executing the function inside the &lt;code>it&lt;/code> block which includes I/O operations only. But the update of the component where the text changes from &amp;ldquo;Empty&amp;rdquo; to our mocked response is not synchronous; this update happens in fact asynchronously because of our network request but also because of how React works internally. But since the test is not using any &lt;code>await&lt;/code> keyword, nodejs exits as soon as possible. And this is the major problem with UI tests: &lt;strong>Components are &lt;em>observables&lt;/em> which don&amp;rsquo;t return promises or values. Therefore, we cannot add &lt;code>await&lt;/code> keywords anywhere at will.&lt;/strong>&lt;/p>
&lt;p>And this is why we need to flush promises. Let us first speak about how we could get the test to pass and we will then &lt;a href="#how">discuss why it works&lt;/a>.&lt;/p>
&lt;p>We can flush promises with the following function:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-js" data-lang="js">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">function&lt;/span> &lt;span style="color:#a6e22e">flushPromises&lt;/span>() {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">return&lt;/span> &lt;span style="color:#66d9ef">new&lt;/span> Promise(&lt;span style="color:#a6e22e">setImmediate&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Since it returns a promise, it needs to be called using &lt;code>await&lt;/code>:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-js" data-lang="js">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">it&lt;/span>(&lt;span style="color:#e6db74">&amp;#34;Test if component renders web response&amp;#34;&lt;/span>, &lt;span style="color:#66d9ef">async&lt;/span> () =&amp;gt; {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">const&lt;/span> &lt;span style="color:#a6e22e">component&lt;/span> &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#a6e22e">render&lt;/span>(&lt;span style="color:#f92672">&amp;lt;&lt;/span>&lt;span style="color:#a6e22e">App&lt;/span> &lt;span style="color:#f92672">/&amp;gt;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">const&lt;/span> &lt;span style="color:#a6e22e">response&lt;/span> &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#a6e22e">component&lt;/span>.&lt;span style="color:#a6e22e">container&lt;/span>.&lt;span style="color:#a6e22e">querySelector&lt;/span>(&lt;span style="color:#e6db74">&amp;#34;.response&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">for&lt;/span> (&lt;span style="color:#66d9ef">let&lt;/span> &lt;span style="color:#a6e22e">index&lt;/span> &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#ae81ff">0&lt;/span>; &lt;span style="color:#a6e22e">index&lt;/span> &lt;span style="color:#f92672">&amp;lt;&lt;/span> &lt;span style="color:#ae81ff">200&lt;/span>; &lt;span style="color:#a6e22e">index&lt;/span>&lt;span style="color:#f92672">++&lt;/span>) {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">await&lt;/span> &lt;span style="color:#a6e22e">flushPromises&lt;/span>()
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">expect&lt;/span>(&lt;span style="color:#a6e22e">response&lt;/span>.&lt;span style="color:#a6e22e">textContent&lt;/span>).&lt;span style="color:#a6e22e">toEqual&lt;/span>(&lt;span style="color:#a6e22e">mockedWebResponse&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>})
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>And there you go, the test passes now! Alternatively, we could also use the &lt;code>waitFor&lt;/code> helper exposed by the Testing Library. &lt;a href="https://github.com/testing-library/dom-testing-library/blob/e3fc7f3a0d29921a5821d361394446968d77eca5/src/wait-for.js#L89">A deep-dive into the source code&lt;/a> shows that it is essentially just a wrapper around our current solution which also uses &lt;code>setImmediate&lt;/code> - but the &lt;code>waitFor&lt;/code> helper doesn&amp;rsquo;t flush for a random 200 times but only as often as actually needed. This speeds up our test slightly.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-js" data-lang="js">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">import&lt;/span> {&lt;span style="color:#a6e22e">render&lt;/span>, &lt;span style="color:#a6e22e">waitFor&lt;/span>} &lt;span style="color:#a6e22e">from&lt;/span> &lt;span style="color:#e6db74">&amp;#34;@testing-library/react&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">it&lt;/span>(&lt;span style="color:#e6db74">&amp;#34;Test if component renders web response&amp;#34;&lt;/span>, &lt;span style="color:#66d9ef">async&lt;/span> () =&amp;gt; {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">const&lt;/span> &lt;span style="color:#a6e22e">component&lt;/span> &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#a6e22e">render&lt;/span>(&lt;span style="color:#f92672">&amp;lt;&lt;/span>&lt;span style="color:#a6e22e">App&lt;/span> &lt;span style="color:#f92672">/&amp;gt;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">const&lt;/span> &lt;span style="color:#a6e22e">response&lt;/span> &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#a6e22e">component&lt;/span>.&lt;span style="color:#a6e22e">container&lt;/span>.&lt;span style="color:#a6e22e">querySelector&lt;/span>(&lt;span style="color:#e6db74">&amp;#34;.response&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">await&lt;/span> &lt;span style="color:#a6e22e">waitFor&lt;/span>(() =&amp;gt; &lt;span style="color:#a6e22e">expect&lt;/span>(&lt;span style="color:#a6e22e">response&lt;/span>.&lt;span style="color:#a6e22e">textContent&lt;/span>).&lt;span style="color:#a6e22e">toEqual&lt;/span>(&lt;span style="color:#a6e22e">mockedWebResponse&lt;/span>))
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>})
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="a-namehowahow-does-flushing-promises-work">&lt;a name="how">&lt;/a>How does flushing promises work&lt;/h2>
&lt;p>As you saw above, both solutions involved the function &lt;code>setImmediate&lt;/code>. This function is special. In fact, the MDN Web Docs mention &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Window/setImmediate">that it should not be used in production websites&lt;/a>. But since we only deal here with nodejs and tests, this doesn&amp;rsquo;t matter to us.&lt;/p>
&lt;p>In order to know more about how &lt;code>setImmediate&lt;/code> works, we need to learn &lt;a href="https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/">what the JavaScript event loop is&lt;/a>. I will try my best to sum it up and paraphrase: JavaScript is a &lt;em>non-blocking&lt;/em> programming language. This means that if I would make 10 network requests each taking 1 second then my program would not take 10 seconds but still only 1 second. This is achieved via the event loop which is essentially a task queue. JavaScript then takes the tasks from the task queue and executes them concurrently.&lt;/p>
&lt;p>Imagine you are in school and your teacher gives you homework - you will not complete all of these tasks right the instance the teacher asked you to do so; instead, you will write down these tasks in your notebook and you will execute these tasks once it&amp;rsquo;s a convenient time to do so.&lt;/p>
&lt;p>Sticking to the homework example, let&amp;rsquo;s pretend your teacher gave you 100 tasks of homework. But there is only so much you can do in one day, you only have time for 10 exercises per day. This is your &amp;ldquo;call stack size&amp;rdquo;. The nodejs event loop looks at all scheduled tasks and tries to put as many as possible onto the call stack so that they can be executed.&lt;/p>
&lt;p>But here is where the function &lt;code>setImmediate&lt;/code> comes in. If the teacher would have given 100 tasks for the next week, nodejs would try to prioritise the tasks which the teacher gave the longest time ago in order to catch up. But functions which are scheduled using &lt;code>setImmediate&lt;/code> are special - these functions are additional tasks. This means that if your teacher gave you 100 tasks &lt;strong>and&lt;/strong> 5 tasks scheduled using &lt;code>setImmediate&lt;/code>, nodejs will first do the 10 tasks it can do per day and then &lt;strong>additionally&lt;/strong> also do all of the tasks queued up using &lt;code>setImmediate&lt;/code>. In essence, &lt;code>setImmediate&lt;/code> functions are extra tasks which take higher priority.&lt;/p>
&lt;p>How does this translate onto our &lt;code>flushPromises&lt;/code> function? The above way of writing was implicit, so let&amp;rsquo;s pick it apart and write it explicitly:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-js" data-lang="js">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">function&lt;/span> &lt;span style="color:#a6e22e">flushPromises&lt;/span>() {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">return&lt;/span> &lt;span style="color:#66d9ef">new&lt;/span> Promise((&lt;span style="color:#a6e22e">resolve&lt;/span>, &lt;span style="color:#a6e22e">reject&lt;/span>) =&amp;gt; {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">setImmediate&lt;/span>(() =&amp;gt; {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">resolve&lt;/span>()
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> })
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> })
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>What happens is that an asynchronous event (all functions returning promises are async functions) is being scheduled which contains a &lt;code>setImmediate&lt;/code> call. As we have learnt, &lt;code>setImmediate&lt;/code> is only called in the event loop &lt;strong>after&lt;/strong> tasks from the call stack have been executed. &lt;strong>Therefore the event loop is forced to execute tasks of the call stack first before it can call setImmediate&lt;/strong>. Our &lt;code>setImmediate&lt;/code> function is basically sandwiched into all of the other queued up tasks. Since we are returning a promise, we have again something to wait for using the &lt;code>await&lt;/code> keyword.&lt;/p>
&lt;p>In essence, because &lt;code>setImmediate&lt;/code> always happens &lt;em>after&lt;/em> the execution of tasks on the event loop, &amp;ldquo;flushing promises&amp;rdquo; is about &lt;strong>forcing&lt;/strong> the event loop to execute some queued up tasks. But since the call stack is only limited (remember that we can only do 10 pieces of homework per day), we need to call it multiple times in order to force multiple cycles through the event loop.&lt;/p>
&lt;hr>
&lt;p>And that&amp;rsquo;s flushing promises for you! I hope that this article both helped you understand the problem and also how to solve it.&lt;/p></description></item></channel></rss>