<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Yonas Stephen on Medium]]></title>
        <description><![CDATA[Stories by Yonas Stephen on Medium]]></description>
        <link>https://medium.com/@yonasstephen?source=rss-b915f323457c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*qX6ZE1bJo7qe-FrK6e5JxQ.jpeg</url>
            <title>Stories by Yonas Stephen on Medium</title>
            <link>https://medium.com/@yonasstephen?source=rss-b915f323457c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 14:39:47 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@yonasstephen/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[The Untold Story of Golang testing]]></title>
            <link>https://medium.com/gojekengineering/the-untold-story-of-golang-testing-29832bfe0e19?source=rss-b915f323457c------2</link>
            <guid isPermaLink="false">https://medium.com/p/29832bfe0e19</guid>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[how-to]]></category>
            <category><![CDATA[tech]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[testing]]></category>
            <dc:creator><![CDATA[Yonas Stephen]]></dc:creator>
            <pubDate>Tue, 19 Feb 2019 03:25:18 GMT</pubDate>
            <atom:updated>2019-06-07T07:07:48.986Z</atom:updated>
            <content:encoded><![CDATA[<h3>The Untold Story of Golang Testing</h3><h4>Table tests are well and good, but have you heard of the golden file?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v3e_iqSI4uc_XOyFUoh3BQ.jpeg" /><figcaption><a href="http://bit.ly/2Iw18eu">gojek.jobs</a></figcaption></figure><p>If you’re new to Golang, you may not have heard of the ‘golden file’. I found out about it after two years of coding in Go, when I stumbled upon Mitchell Hashimoto’s talk about <a href="https://www.youtube.com/watch?v=8hQG7QlcLBk">Advanced Testing in Go</a>. There was no official documentation around it, not many blogs mention it, and nobody knows why it’s called golden file. All we know is that the standard package has it: <a href="https://github.com/golang/go/tree/master/src/cmd/gofmt/testdata">gofmt/testdata.</a></p><p>This post explains how the golden file is a useful alternative to traditional table tests in Golang for specific scenarios.</p><h3>What is the Golden File?</h3><p>Conceptually, it’s nothing new. If you come from a Javascript background and have used Jest testing library before, this is basically what Jest calls <a href="https://jestjs.io/docs/en/snapshot-testing">snapshot testing</a>. It saves the output of a test to a file and uses it as the expected output.</p><blockquote>That’s a testing hack, why would someone do that?</blockquote><p>This is best explained using a real use case I experienced in the Fraud team at GOJEK as an example. Imagine the simplified diagram of a microservice we are building below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MbJn-xjdgM_A4UkYmjoSBQ.png" /></figure><p>The microservice acts like a proxy service that accepts an HTTP request, does some business logic, and triggers API calls to another microservice. More specifically, it is intended to send communication messages to the appropriate API based on the request received, i.e. push notification, SMS, or in-app nudge card. To ensure everything works as expected, we wrote a table-driven test:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7a5b683b58db5c9f1f20e194faabcd4d/href">https://medium.com/media/7a5b683b58db5c9f1f20e194faabcd4d/href</a></iframe><p>Few commits later, this is what our table test looked like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f6120d7315b6a0d6f46ea88c9406878f/href">https://medium.com/media/f6120d7315b6a0d6f46ea88c9406878f/href</a></iframe><p>There are three observations I want to highlight here:</p><ol><li>First of all, the table test is superb. The time spent to implement a template and loop that iterates through the table is worth the overhead cost of adding various test scenarios. I highly recommend using this pattern.</li><li>As our test cases and the number of assertions grew, our table became too cluttered. It became harder for developers to understand and modify the test cases.</li><li>One of the assertions we want to do is to ensure that we send the correct HTTP header and body to the other microservice. This is the part that primarily bloats our table to a non-human-friendly situation 🤖</li></ol><h3>Here is our golden opportunity</h3><p>Fortunately for us, we have the golden file. This method is most useful when one wants to assert a huge chunk of bytes — such as image base 64 string, JSON file, or (in our case) HTTP body. Instead of writing the expected JSON body in the test file, we can save it totestdata/TestAPI_Comms_Success.golden. Next, we can manually create the golden file, though a better way is to use an update flag. This is to let the test know if we intend to update the golden file or assert the test output against the golden file. Let’s modify our assertion:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/771dae0c9574fb36185cb0adca80b4b9/href">https://medium.com/media/771dae0c9574fb36185cb0adca80b4b9/href</a></iframe><p>If you haven’t figured out yet, the additional code in TestAPI_Comms_Success function essentially dumps the logs table into TestAPI_Comms_Success/&lt;test-case-name&gt;.golden in a beautified JSON format. So, there are now two modes to run the test:</p><ol><li><strong>Update</strong></li></ol><pre>go test ./... -update</pre><p>When the update flag is passed, whatever the logs table contains will be saved into the golden file. For example:</p><pre>[{<br>  &quot;id&quot;: 1,<br>  &quot;http_method&quot;: <br>  &quot;entity_id&quot;: 1, <br>  &quot;entity_type&quot;: &quot;driver&quot;, <br>  &quot;action&quot;: &quot;comm1&quot;, <br>  &quot;api_name&quot;: &quot;service-A&quot;, <br>  &quot;http_method&quot;: &quot;POST&quot;,<br>  &quot;http_body&quot;: {<br>    &quot;type&quot;: &quot;comm-service&quot;, <br>    &quot;action&quot;: &quot;send-notification&quot;, <br>    &quot;priority&quot;: &quot;medium&quot;, <br>    &quot;when&quot;: &quot;now&quot;<br>  },<br>  &quot;http_response_status_code&quot;: 200<br>}]</pre><p><strong>2. Assert</strong></p><pre>go test ./...</pre><p>Without the update flag, the code will run the query that retrieves logs from database. It then compares it against what was written into the golden file earlier.</p><h4>What’s the catch?</h4><p>In golden file testing, the most crucial step is manually eyeballing the golden file to ensure the output saved matches our expectation. That’s why we printed out the JSON in beautified format, so that this task becomes easier.</p><h3>More advanced scenarios</h3><p>If the above setup is sufficient for your golden file testing, that’s great. Unfortunately for us, there are three issues that needed to be tackled:</p><h4>1. Asynchronous architecture</h4><p>Our microservice uses an asynchronous architecture where there are message queues (RabbitMQ) and workers that pick up the jobs. In this case, when we want to generate the golden file, we don’t know exactly when to read the logs from database. The log could be written by the worker 50ms later, 100ms later, or 300ms later.</p><p>The solution is adding a time.Sleep() with the amount of time by when you’re confident the worker will finish the job. Alternatively, you can use <a href="https://onsi.github.io/gomega/#eventually">G</a>omega’s <a href="https://onsi.github.io/gomega/#eventually">Eventually</a>. It can poll your asynchronous assertion at your desired frequency and timeout.</p><h4>2. Time/UUID field</h4><p>Our logs table contains fields such as log_id, created_at, updated_at,which always change whenever golden file is generated. One way to solve this is by excluding these fields when querying the database for the golden file. If you have many tables to be asserted in golden file (like we did), <a href="https://github.com/bouk/monkey">monkeypatch</a> is a decent solution.</p><pre>// Patch time.Now() to static time so that we can compare golden files correctly<br>timezone, err := time.LoadLocation(&quot;Asia/Jakarta&quot;)<br>timeNowPatch := monkey.Patch(time.Now, func() time.Time {<br>  return time.Date(2012, time.December, 5, 15, 0, 0, 0, timezone)<br>})</pre><p>The only catch here is when you write the log into the database, you must pass time.Now() as argument to created_at field, instead of using your Postgres function now().</p><p>Now for UUID, we use <a href="https://github.com/google/uuid">https://github.com/google/uuid</a>. What we can do is to feed a static random number reader, so that the generated UUIDs are always the same:</p><pre>reader := bytes.NewReader([]byte(&quot;1111111111111111&quot;))<br>uuid.SetRand(reader)<br>uuid.SetClockSequence(1)</pre><h4>3. A Better Golden File Diff</h4><p>Writing an output such as There is a mismatch in TestAPI_Comms_Success.golden is not helpful information when your golden file is 200 lines long. One must eyeball field by field and compare which one is different. To make our life easier, we use <a href="https://onsi.github.io/gomega/#matchjsonjson-interface">gomega.MatchJSON</a> which highlights which JSON key has a mismatch.</p><h3>Is the golden file for me?</h3><p>This method is not for everyone. If all test cases fit just fine in a table test, there is no need to write golden file. In such scenarios, the benefits are not worth the time spent and possibility of human error while analysing golden file changes. However, when used for the right use cases, the golden file makes life much better.</p><p>GOJEK runs one of the largest Go clusters in Asia. We’ve scaled over 6600x since we launched our app in 2015, and expanded to Vietnam, Singapore and Thailand in 2018. We’re still growing, and always learning. Head over to <a href="http://bit.ly/2Iw18eu">gojek.jobs</a>, and be a part of our journey to redefine transport, payments and logistics in Southeast Asia 🙌</p><figure><a href="http://bit.ly/2Iw18eu"><img alt="" src="https://cdn-images-1.medium.com/max/892/1*PdKKvtpvxTQcbMlDvIlFzQ.png" /></a><figcaption>gojek.jobs</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=29832bfe0e19" width="1" height="1" alt=""><hr><p><a href="https://medium.com/gojekengineering/the-untold-story-of-golang-testing-29832bfe0e19">The Untold Story of Golang testing</a> was originally published in <a href="https://medium.com/gojekengineering">Gojek Product + Tech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ESLint + Prettier For a Consistent React Codebase]]></title>
            <link>https://medium.com/gojekengineering/eslint-prettier-for-a-consistent-react-codebase-eaa673debb1d?source=rss-b915f323457c------2</link>
            <guid isPermaLink="false">https://medium.com/p/eaa673debb1d</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[startup]]></category>
            <category><![CDATA[tech]]></category>
            <dc:creator><![CDATA[Yonas Stephen]]></dc:creator>
            <pubDate>Wed, 28 Mar 2018 04:23:57 GMT</pubDate>
            <atom:updated>2018-05-30T16:58:38.613Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LCYrOpZBR5aBsUTwqFx6xQ.jpeg" /></figure><p>Different programmers have different styles of coding. One would put curly braces at the same line as function names, while others would write curly braces on a new line. Some tend to name variables in camelCase, others in PascalCase. There is no right or wrong. It’s only a matter of preference.</p><p>At GO-JEK, we always have at least two people for code pairing &amp; reviewing a project. The moment two or more programmers put their hands on a project, you’ll start to notice inconsistency across your codes. This inconsistency bothers my engineering conscience.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*MeSw3PDyjLARfsVH6xX_og.gif" /><figcaption>Obsessive Compulsive Disorder (OCD)</figcaption></figure><p><em>P.S. In this post I’m going to assume we are building a React app with </em><a href="https://github.com/facebook/create-react-app"><em>create-react-app</em></a><em>, although most of the concepts here are applicable to any Javascript development.</em></p><h3>ESLint to the rescue</h3><p>Luckily we are not alone and that’s why <a href="https://twitter.com/slicknet">Nicholas C. Zakas</a> built <a href="https://eslint.org/">ESLint</a>.</p><blockquote>Its goal is to provide a pluggable linting utility for JavaScript.</blockquote><p>You can either install ESLint globally or vendor as project’s dependency (preferred). Let’s install it as a project’s development dependency:</p><pre>npm install --save-dev eslint</pre><p>Then you can run an interactive initialization command:</p><pre>./node_modules/.bin/eslint — init</pre><p>You’ll be asked to pick any base rules you wish to extend and config file format. In our case, we pick <a href="https://github.com/airbnb/javascript/tree/master/packages/eslint-config-airbnb">airbnb-eslint-config</a> as base rules and JSON as config format. Voila! just like in Unix, a runcom fossil is generated as .eslintrc file in project’s root directory. Here’s how it looks,</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5faac9de9be913768256951cea6b6a9c/href">https://medium.com/media/5faac9de9be913768256951cea6b6a9c/href</a></iframe><p>Let’s try to decipher .eslintrc relics,</p><ol><li><strong>env</strong> — By default, Airbnb config uses <a href="https://eslint.org/docs/rules/no-undef">no-undef</a> rule. If you use browser globals like localStorage or window in your code, these rules will cause linter to complain that those variables being never defined. Setting “browser” env key to true essentially tells linter, “Hey chill, I’m pretty sure these localStorage and window variables are defined by the browser. Don’t worry about it.” You can refer <a href="https://eslint.org/docs/user-guide/configuring#specifying-environments">here for the list of supported env values.</a></li><li><strong>extends</strong> — These are the base rules you wish to extend.</li><li><strong>parser</strong> — This allows you to tune JavaScript transpiler and ES language features you like support. e.g. If you’re using ES6’s module systemimport React from &#39;react&#39;, you might bump into the keyword &#39;import&#39; is reserved error. This is because ESLint doesn’t know about ES6 features yet. To fix this, you need to install <a href="https://github.com/babel/babel-eslint">babel-eslint</a> npm i --save babel-eslint and set it as the parser.</li><li><strong>rules</strong> — I don’t really agree with all Airbnb’s base rules. If you feel the same way, you can override them here. For example, Airbnb base rule by default pick react/jsx-filename-extension rule and only allows JSX tag to appear in .jsx files. To us, it makes sense to have JSX tag in .js files, so we added .js to the allowed extension.</li></ol><p>Once you have fine-tuned the rules as desired, you could run:</p><pre>./node_modules/.bin/eslint --fix &lt;your_directory&gt;</pre><p>TADAAA! Your code is all fixed now! Except… Maybe not. --fix turns out to only support few rules. As you can see from <a href="https://eslint.org/docs/rules/">this page</a>, there are not many wrench (fixable) icons.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/664/1*fA65R1umkZBVJjyBTTy6Xw.png" /><figcaption>Name a more iconic duo. ESLint + Prettier. I’ll wait.</figcaption></figure><h3>Make it Prettier</h3><p>Prettier is a code formatter that works differently from --fix under the hood.</p><blockquote>Prettier takes your code and reprints it from scratch by taking the line length into account.</blockquote><p>One example of <strong>what Prettier can do that ESLint’s </strong>--fix<strong> can’t do is</strong>:</p><pre>// bad<br>foo(reallyLongArg(), omgSoManyParameters(), IShouldRefactorThis(), isThereSeriouslyAnotherOne(), noWayYouGottaBeKiddingMe());</pre><p>If you set <a href="https://eslint.org/docs/rules/max-len">max-le</a>n rule to 80, ESLint will just give you a warning that it’s too wide. If you use Prettier, it will format your code into this:</p><pre>// good<br>foo(<br>  reallyLongArg(),<br>  omgSoManyParameters(),<br>  IShouldRefactorThis(),<br>  isThereSeriouslyAnotherOne(),<br>  noWayYouGottaBeKiddingMe()<br>);</pre><p>Hopefully, you’re now convinced of using Prettier. It has few lint rules it supports on its own. But there are not many. Therefore, our goal is to now <strong>have Prettier do the code formatting based on the ESLint rules we define</strong>. Let’s start by installing these 3 libraries:</p><pre>npm install --save-dev prettier eslint-plugin-prettier eslint-config-prettier</pre><ol><li><a href="https://github.com/prettier/prettier"><strong>prettier</strong></a> — the core prettier library. <em>Duh</em>.</li><li><a href="https://github.com/prettier/eslint-plugin-prettier"><strong>eslint-plugin-prettier</strong></a> — this plugin allows you to format code using Prettier when you run --fix . <em>Say whaaaat</em>.</li><li><a href="https://github.com/prettier/eslint-config-prettier"><strong>eslint-config-prettier</strong></a> — what happens if my ESLint rule says I need a comma at the end of every line, but my Prettier rule says that should never happen? This library solves just that. It turns off conflicting rules. ESLint’s rules, not Prettier’s. <em>Obviously</em>.</li></ol><p>All you have to add plugin:prettier/recommended to the “extends” key in .eslintrc as such:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/95af3c8a0924b59046a2abfc38dffc64/href">https://medium.com/media/95af3c8a0924b59046a2abfc38dffc64/href</a></iframe><p><a href="https://github.com/prettier/eslint-plugin-prettier#recommended-configuration">That addition does 3 things</a>:</p><ol><li>Enables eslint-plugin-prettier.</li><li>Sets the prettier/prettier rule to &quot;error&quot;.</li><li>Extends the eslint-config-prettier configuration.</li></ol><p>Now run this command again:</p><pre>./node_modules/.bin/eslint --fix &lt;your_directory&gt;</pre><p>and you’ll have Prettier do the formatting for your ESLint rules 🚀</p><h3>Taking it further</h3><p>Asking programmers to run a command on every edit is not the most efficient way to enforce consistency. Here ate 3 alternatives you can follow to automate this:</p><p><strong>1. Format on save with your favourite editor</strong></p><p>Prettier has <a href="https://prettier.io/docs/en/editors.html">support for most popular editors such as Atom, VSCode, Sublime, WebStorm, and even Vim</a>. You just need to get them installed, enable “format on save”, and enable ESLint integration.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*c9jPeEGyGxQYYYw1HuEFEA.png" /></figure><p><em>But that’s super manual and time consuming to get it working with an existing dev setup.</em></p><p><strong>2. Add lint stage in project’s CI/CD</strong></p><p>You can add a script in your package.json as follows:</p><pre>{<br>  &quot;scripts&quot;: {<br>    &quot;lint&quot;: &quot;eslint src&quot;<br>  }<br>}</pre><p>Note: we don’t need ./node_modules/.bin when running using npm script because it will default to the project library, not the globally installed eslint (if any). Then in your CI config, add a lint stage:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/765fdea7bcf601fdc739cf6b1611e628/href">https://medium.com/media/765fdea7bcf601fdc739cf6b1611e628/href</a></iframe><p>This will cause project’s pipeline to fail, if the committed code does not pass lint rules.</p><p><em>Aha! now CI can report linting issues. No more, it works on my machine.</em></p><p><strong>3. Pre-commit hook</strong></p><p>You can use Prettier with a pre-commit tool. This can re-format your files that are marked as “staged” via git addbefore you commit. Prettier has the full list of options to achieve that; we are going with <a href="https://github.com/okonet/lint-staged">lint-staged</a> and <a href="https://github.com/typicode/husky">husky</a></p><pre>npm install --save-dev lint-staged husky</pre><p>Then add these scripts into package.json</p><pre>{<br>  &quot;scripts&quot;: {<br>    &quot;precommit&quot;: &quot;lint-staged&quot;<br>  },<br>  &quot;lint-staged&quot;: {<br>    &quot;src/**/*.{js,jsx,json,css,md}&quot;: [&quot;eslint --fix src&quot;, &quot;git add&quot;]<br>  }<br>}</pre><p>Now your lint --fix will be run every time you execute git commit and you can be assured that your codebase is consistent and well-formatted. <em>That’s more like it.</em></p><blockquote>don’t let 💩 slip into your codebase. — <a href="https://github.com/okonet/lint-staged">lint-staged</a></blockquote><h3><strong>Final words</strong></h3><p>Linting can be one aspect people overlook in a codebase because more often than not, a project starts with a single person. Adding linter not only increases consistency, but helps detect bugs early and build maintainable code. The earlier we adopt, the better.</p><blockquote>“Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live” ― <strong>John Woods</strong></blockquote><p>Any recommendations, tips? Would love to hear more.</p><p>If you’re interested in building beautiful, consistent, maintainable codebases, we are hiring Software Engineers! Check out <a href="http://gojek.jobs/">gojek.jobs</a> for more information.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eaa673debb1d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/gojekengineering/eslint-prettier-for-a-consistent-react-codebase-eaa673debb1d">ESLint + Prettier For a Consistent React Codebase</a> was originally published in <a href="https://medium.com/gojekengineering">Gojek Product + Tech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>