<?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 Alex Gogl on Medium]]></title>
        <description><![CDATA[Stories by Alex Gogl on Medium]]></description>
        <link>https://medium.com/@gogl.alex?source=rss-75ca831e3834------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*WgElUjdHtw1rZJtD</url>
            <title>Stories by Alex Gogl on Medium</title>
            <link>https://medium.com/@gogl.alex?source=rss-75ca831e3834------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 19:42:39 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@gogl.alex/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[How to deploy Shopify themes automatically]]></title>
            <link>https://medium.com/@gogl.alex/how-to-deploy-shopify-themes-automatically-1ac17ee1229c?source=rss-75ca831e3834------2</link>
            <guid isPermaLink="false">https://medium.com/p/1ac17ee1229c</guid>
            <category><![CDATA[shopify-development]]></category>
            <category><![CDATA[shopify-theme]]></category>
            <category><![CDATA[shopify]]></category>
            <dc:creator><![CDATA[Alex Gogl]]></dc:creator>
            <pubDate>Sun, 21 Jun 2020 13:43:56 GMT</pubDate>
            <atom:updated>2020-06-21T13:43:56.028Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gou94JVJbyHxEzqnvJULRQ.png" /></figure><p>If you’re a startup or a small company selling products online there’s a good chance you’re using Shopify. Most of the time you’ll buy a third party theme and customize it to your needs. But how do you deploy these changes without breaking things in production? This tutorial walks you through creating a CI/CD pipeline with separate <strong>development</strong>, <strong>staging</strong> and <strong>production</strong> environments.</p><p>Knowledge required: Liquid (basics) / What CI,CD pipelines are / some experience with GitLab</p><p>You’ll end up with this nice <strong>workflow</strong>:</p><ol><li>Develop locally on a feature branch and once you save files -&gt; your development environment gets updated instantly, you see changes immediately without a pipeline.</li><li>Once you are happy with your local changes, merge your feature branch into your master branch -&gt; your staging environment will get updated after the pipeline succeeds</li><li>If everything looks alright and your theme passes QA, create a new tag and push it -&gt; Now your production environment theme will be updated and customers can use new features.</li></ol><p>Now lets’ get to it. This will take you about 20–30 minutes and I promise it’s well worth your time. As with anything tutorial-related I write, please reach out to me immediately if you have issues or the steps outlined stop working altogether. Tutorials on the internet should just work.</p><h3>1. Creating multiple theme versions</h3><p>The first step is to create multiple versions of the same theme. One for active development where you can try and break things, one as a staging environment so you can beta-test your changes, and the last one as a production-ready environment (your actual live store).</p><p>To show you real examples I’ve created a new Shopify store in trial mode. If you already have a live theme, duplicate it two times so you have 3 versions in total and then rename them to something meaningful. This should end up looking like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jDZy0vCWBWqYGegzie85kw.png" /></figure><p>As you can see, my live theme is called “Debut Production”, my staging theme “Debut Staging” and the dev theme “Debut Development”.</p><h3>2. Set up a GitLab Repository</h3><p>The next step is to set up a git repository so you can track your changes. If you didn’t know it already, your Shopify theme is basically just a file and folder structure you can download. Even all your settings (site layout, translations, etc.) are just files! This is awesome for us because we can utilize the full power of Git.</p><p>The reason I recommend GitLab and not GitHub is “GitLab CI” — GitLab’s integrated CI tool with more than enough free quota which isn’t offered by GitHub. We will use this to update our theme whenever we push to our master branch.</p><p>For now, just head over to <a href="https://5ede6b8499bfdd00078d3a16--xenodochial-kirch-50b011.netlify.app/how-to-upgrade-shopify-themes-with-custom-code-changes/gitlab.com">gitlab.com</a> and create a new repo which we’ll need in a bit.</p><h3>3. Set up ThemeKit CLI</h3><p>I’m assuming you’re coding on MacOS, to install ThemeKit run the following commands:</p><pre>brew tap shopify/shopify<br>brew install themekit</pre><p>For detailed instructions and installation guides on other operating systems visit: <a href="https://shopify.github.io/themekit/">https://shopify.github.io/themekit/</a></p><p>Next, we need to get an API key from Shopify so we can actually use ThemeKit on our local machine. In your Shopify Dashboard go to Apps, scroll down to “manage private apps” and then click “Create new private app”.</p><p>Name it whatever you like, the only important thing is to set the correct permissions: “Products — Read access”, and “Themes — Read, Write access”. This should look like this:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.loom.com%2Fembed%2Fa481aa28190e4c0aa7040b86634adccb&amp;display_name=Loom&amp;url=https%3A%2F%2Fwww.loom.com%2Fshare%2Fa481aa28190e4c0aa7040b86634adccb&amp;image=https%3A%2F%2Fcdn.loom.com%2Fsessions%2Fthumbnails%2Fa481aa28190e4c0aa7040b86634adccb-00001.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=loom" width="1280" height="960" frameborder="0" scrolling="no"><a href="https://medium.com/media/4530e803fe10cd0af61b449e2e7aca2d/href">https://medium.com/media/4530e803fe10cd0af61b449e2e7aca2d/href</a></iframe><p><strong>Note: This private Shopify app has already been deleted and was used as a showcase, the API keys seen in this video are not available anymore so abusing them is pointless.</strong></p><p>Take note of your <strong>password</strong>, you’ll need it later.</p><h3>3. Download theme to local machine</h3><p>To make changes offline, we need to download the theme to our machine. To do that create a new folder first, run git init and add your remote git repo.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.loom.com%2Fembed%2F70eeea04f79545f59ae046b81c5ab966&amp;display_name=Loom&amp;url=https%3A%2F%2Fwww.loom.com%2Fshare%2F70eeea04f79545f59ae046b81c5ab966&amp;image=https%3A%2F%2Fcdn.loom.com%2Fsessions%2Fthumbnails%2F70eeea04f79545f59ae046b81c5ab966-00001.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=loom" width="1280" height="960" frameborder="0" scrolling="no"><a href="https://medium.com/media/03d6769565935f722abf66034848b8ea/href">https://medium.com/media/03d6769565935f722abf66034848b8ea/href</a></iframe><p>Now it’s getting a little more complicated. We need to use the ThemeKit CLI to download the live theme version of our store. While a little more complicating, it’s actually still quite straightforward:</p><pre>theme get -p<strong>=[</strong>your-password] -s<strong>=[</strong>you-store.myshopify.com] -t<strong>=live</strong></pre><p>Substitute the brackets with your actual password and store name (we’ll use environment variables in a moment)</p><p>After our theme has been downloaded successfully, you will notice that a config.yml file has been created. Right now it contains our actual shopify API password so we have to change that in the next step to keep our environment secure.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.loom.com%2Fembed%2Ff67038d35e8748cbbc60bda48816e466&amp;display_name=Loom&amp;url=https%3A%2F%2Fwww.loom.com%2Fshare%2Ff67038d35e8748cbbc60bda48816e466&amp;image=https%3A%2F%2Fcdn.loom.com%2Fsessions%2Fthumbnails%2Ff67038d35e8748cbbc60bda48816e466-00001.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=loom" width="1280" height="960" frameborder="0" scrolling="no"><a href="https://medium.com/media/3dcf3409db7437306334bdc4c17573ed/href">https://medium.com/media/3dcf3409db7437306334bdc4c17573ed/href</a></iframe><h3>4. Setting up environment variables</h3><p>Because we do not want to store the Shopify password in our git repo we need to set up environment variables.</p><p>We need to do this for:</p><ol><li>Our local machine</li><li>The GitLab repo</li></ol><h4>Local Machine</h4><p>First, we’ll do it for our local machine. Depending on your OS you need to create a “variables” file at a different location.</p><p>The ThemeKit documentation (<a href="https://shopify.github.io/themekit/configuration/">https://shopify.github.io/themekit/configuration/</a>) tells you exactly where you have to create it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/987/1*8UocJt_nqcoOpbmb2Twl8g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cvrhCW9fNX2gRQMerVnNRA.png" /></figure><p>As I’m a Mac user, I’ll need to create a variables file at /Library/Application Support/Shopify/Themekit/variables (without a file extension) and add the following content:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6c2398b42d0ee49147fb6089fc186d2b/href">https://medium.com/media/6c2398b42d0ee49147fb6089fc186d2b/href</a></iframe><p>You can already fill out your API password (the password you noted down earlier) but what about the theme IDs? We need to use the ThemeKit CLI for that again to retrieve all the available themes of our store:</p><pre>theme get --list -p=[your-api-password] -s=[your-store.myshopify.com]</pre><p>This should display all your theme IDs. As you can see, these are the ones we duplicated in the first step.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8G14DleOn54TyfdyXj7LlQ.png" /></figure><p>Now just copy these numbers to your variables file (without the brackets) and save it. Leave your terminal open for now as we need the theme IDs again for our GitLab repo.</p><h4>GitLab repo</h4><p>When we push new code changes we want GitLab to automatically run the ThemeKit command to deploy the changes to the correct environment. For that, we need some environment variables first.</p><p>Here’s how you add the environment variables to your GitLab repo:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.loom.com%2Fembed%2Fcfa1faef876740db95931805ce970020&amp;display_name=Loom&amp;url=https%3A%2F%2Fwww.loom.com%2Fshare%2Fcfa1faef876740db95931805ce970020&amp;image=https%3A%2F%2Fcdn.loom.com%2Fsessions%2Fthumbnails%2Fcfa1faef876740db95931805ce970020-00001.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=loom" width="1152" height="864" frameborder="0" scrolling="no"><a href="https://medium.com/media/9ec630af8bc5ec0f7483e5803c549afc/href">https://medium.com/media/9ec630af8bc5ec0f7483e5803c549afc/href</a></iframe><p>Note that I turned the “protected” feature off. When it’s turned on, these variables will not show in the CI logs, which adds an extra layer of security. If you want to turn them on, you need to make sure that your master branch and the tag v* wildcard is protected as well.</p><h3>5. Auto Deployments with GitLab CI</h3><p>Now comes the best part. Imagine you develop on your local machine and once you’re happy with your changes you want to test them and then update the live theme. A really bad thing would be to only have one single theme (your live theme) and do all the changes while your customers can still shop online. That would make for a very bad user experience. Instead, we want to be able to break things in our development theme, completely isolated from what customers see and once everything works as defined, we update our live theme without any service disruption. To make that happen we’re going to create a CI pipeline.</p><p>We will set it up like this:</p><ul><li>Pushes to master will update the staging theme</li><li>Creating new tags will update the production theme</li><li>The development theme will only be used locally</li></ul><h4>Creating .gitlab-ci.yml</h4><p>Go to your local theme folder and create a file called .gitlab-ci.yml at project root. Add the following content:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/704e9696c6d6edc4e0e62941a84876d9/href">https://medium.com/media/704e9696c6d6edc4e0e62941a84876d9/href</a></iframe><p>This is the configuration for our CI pipeline. It has two stages: “staging” and “production”. Both stages install the ThemeKit CLI first and then deploy the whole theme to the corresponding environment.</p><p>Note: We have to use Python2 because ThemeKit doesn’t seem to support Python3 at the moment, but it literally makes no difference because this is just the Docker image used behind the scenes to run our script.</p><h4>Updating config.yml</h4><p>ThemeKit uses a config file so you can run commands in different environments. Just create a file at project root called “config.yml”. Make sure to change your store URL but leave the password and theme id as is (these will be populated by our variables file we created in step 4.)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f61ae08b15f2c6fb11ad1dee16a44618/href">https://medium.com/media/f61ae08b15f2c6fb11ad1dee16a44618/href</a></iframe><p>Once you created these two files, your project structure should look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YIc60T76_hXispxTLgLSxg.png" /></figure><h3>6. Development flow</h3><p>Now once you run the following code on your <strong>master</strong> branch:</p><pre>git commit -am &quot;commit message&quot;<br>git push</pre><p>Your theme will get automatically deployed to your staging environment:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.loom.com%2Fembed%2Fa5db3b4c0b0a4ea590d49f75385f703a&amp;display_name=Loom&amp;url=https%3A%2F%2Fwww.loom.com%2Fshare%2Fa5db3b4c0b0a4ea590d49f75385f703a&amp;image=https%3A%2F%2Fcdn.loom.com%2Fsessions%2Fthumbnails%2Fa5db3b4c0b0a4ea590d49f75385f703a-00001.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=loom" width="1152" height="864" frameborder="0" scrolling="no"><a href="https://medium.com/media/f3794c00222f528d1c1d89ce6e749a31/href">https://medium.com/media/f3794c00222f528d1c1d89ce6e749a31/href</a></iframe><p>Isn’t that incredibly useful?</p><p>And once you create a new tag and push it, your live theme will be updated:</p><pre>git tag -a &quot;v1.0.0&quot; -m &quot;first  prod version of our customized theme&quot;<br>git push --tags</pre><p>And if you want to develop locally you can just run:</p><pre>theme watch -e=development</pre><p>Which will update your development theme instantly without the use of a pipeline. On the one hand, we want to develop fast, but once we’re happy with our results it doesn’t matter if deployment to production takes 5 seconds or 2 minutes.</p><p>Now how could a nice development flow look like?</p><ol><li>Develop locally on a feature branch and once you save files -&gt; your development theme gets updated instantly and you see changes immediately without a pipeline.</li><li>Once you are happy with your local changes, merge your feature branch into your master branch -&gt; your staging theme will get updated</li><li>If everything looks alright and your theme passes QA, create a new tag and push it -&gt; Now your live theme will be updated</li></ol><p>This video exactly depicts this workflow:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.loom.com%2Fembed%2Fbabcb551d4d7450190400cad05076cdf&amp;display_name=Loom&amp;url=https%3A%2F%2Fwww.loom.com%2Fshare%2Fbabcb551d4d7450190400cad05076cdf&amp;image=https%3A%2F%2Fcdn.loom.com%2Fsessions%2Fthumbnails%2Fbabcb551d4d7450190400cad05076cdf-00001.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=loom" width="1152" height="864" frameborder="0" scrolling="no"><a href="https://medium.com/media/4746d32935a500d1dfce279f7c4fdfd1/href">https://medium.com/media/4746d32935a500d1dfce279f7c4fdfd1/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1ac17ee1229c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to set up TypeScript/ESLint/Prettier for Next/Gatsby]]></title>
            <link>https://medium.com/@gogl.alex/how-to-set-up-typescript-eslint-prettier-for-next-gatsby-c5330b4a9b7a?source=rss-75ca831e3834------2</link>
            <guid isPermaLink="false">https://medium.com/p/c5330b4a9b7a</guid>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[eslint]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Alex Gogl]]></dc:creator>
            <pubDate>Fri, 27 Mar 2020 19:31:11 GMT</pubDate>
            <atom:updated>2021-05-08T12:10:40.621Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to set up TypeScript/ESLint/Prettier for Next.js</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*rbbBUhXLt826rOGH" /></figure><p><strong>Update: </strong>Since many devs are facing the same problems I created a nextjs starter template with everything configured (and some): <a href="https://nextjs-advanced-starter.vercel.app/">https://nextjs-advanced-starter.vercel.app/</a></p><p>This tutorial assumes you’re using Visual Studio Code (as you definitely should). Setting up TypeScript with ESLint and Prettier is always a pain, there are tons of tutorials but none of them seem to work on their own and you always have to copy and paste code from multiple sources to get it working. This article should serve as a documentation on how to do that as simple as possible. Basically what I want you to be able to do is to just copy and paste everything and it should just work</p><p><strong>If this stops working, please reach out to me immediately, I really hate tutorials that just don’t work.</strong></p><h3>1. Install dependencies</h3><pre>npm i --save-dev <a href="http://twitter.com/types/node">@types/node</a> <a href="http://twitter.com/types/react">@types/react</a> <a href="http://twitter.com/typescript">@typescript</a>-eslint/eslint-plugin <a href="http://twitter.com/typescript">@typescript</a>-eslint/parser eslint-config-airbnb eslint-config-prettier eslint-plugin-import eslint-plugin-jsx-a11y eslint-plugin-prettier eslint-plugin-react prettier typescript eslint babel-plugin-module-resolver eslint-import-resolver-babel-module eslint-plugin-react-hooks</pre><h3>2. Set up tsconfig.json</h3><p>Create a file called “tsconfig.json” at the root of your project and paste the following code</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/30805dd42dccb9103a488fbf03479247/href">https://medium.com/media/30805dd42dccb9103a488fbf03479247/href</a></iframe><h3>3. Set up .eslintrc.js</h3><p>Create a file called “.eslintrc.js” at the root of your project and paste the following code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dadb09ade2a4aaa92d24d969804ad1f7/href">https://medium.com/media/dadb09ade2a4aaa92d24d969804ad1f7/href</a></iframe><h3>4. Set up VSCode settings for auto fixes</h3><p>Create a folder called “.vscode” at the project root. Inside this folder create a file called “settings.json” with the following content:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/04fd890f18eff3d9267271b014e72505/href">https://medium.com/media/04fd890f18eff3d9267271b014e72505/href</a></iframe><h3>5. (Optional) Absolute imports</h3><p>Create a .babelrc file in the root of your project</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3dbf9327639859ebc575cd22a7e41e5a/href">https://medium.com/media/3dbf9327639859ebc575cd22a7e41e5a/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c5330b4a9b7a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Nuxt + Jest setup from scratch]]></title>
            <link>https://medium.com/@gogl.alex/nuxt-jest-setup-from-scratch-8905d3880daa?source=rss-75ca831e3834------2</link>
            <guid isPermaLink="false">https://medium.com/p/8905d3880daa</guid>
            <category><![CDATA[nuxtjs]]></category>
            <category><![CDATA[nuxt]]></category>
            <category><![CDATA[jest]]></category>
            <category><![CDATA[vuejs]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Alex Gogl]]></dc:creator>
            <pubDate>Sun, 19 May 2019 15:09:01 GMT</pubDate>
            <atom:updated>2019-11-07T10:21:52.820Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to properly set up Nuxt + Jest</h3><p>Setting up testing can be very daunting: especially if you are just starting out. When I started out with Vue and Nuxt, I didn’t really have any experience regarding setting up configurations for linting or testing. The vue-cli provides pre-made configs for several kinds of things, including a Jest setup for Unit and Snapshot testing, however, Nuxt does not offer something similar to the vue-cli at the moment. I wanted to mimic that behaviour but couldn’t find any good resources which outline clear and most of all - easy to follow steps. This article serves as a reference guide for anyone wanting to use Nuxt + Jest.</p><p>By the way, whenever I do something a little more complex, I always write down each and every step because it makes it much easier to reproduce it in new projects. Documenting these steps does not have to be very thorough (at the end it has to work though), just write down each command you used. This also makes it much easier to write articles like this one and by publishing them everyone can benefit.</p><p>If you specifically looked for a config like the one described here, you probably already know about the advantages of testing. Testing makes proper CI/CD possible, which is quite frankly, one of the coolest things in software engineering (coming from someone who never wanted to test anything). However, if you don’t, I strongly encourage you to find out more about how testing could benefit you and your project.</p><p>One last note: I really don’t like when I am trying to set up a specific thing (e.g linting) but the tutorials I find just won’t work. So please please, let me know if something with this config does not work. I really don’t want to waste anyone’s time.</p><h3>1. Installing dependencies</h3><p>To install the development dependencies issue the following command inside the root directory of your project:</p><pre>npm install <a href="http://twitter.com/babel/core">@babel/core</a> <a href="http://twitter.com/babel/preset-env">@babel/preset-env</a> babel-core@^7.0.0-bridge.0 <a href="http://twitter.com/vue/test-utils">@vue/test-utils</a> jest babel-jest jest-serializer-vue vue-jest -D</pre><p>At the time of this writing, vue-jest@3.0.4 requires a peer of babel-core@^6.25.0So we additionally have to install babel@core^7.0.0-bridge.0</p><h3>2. Jest config</h3><p>Create jest.config.js in your root folder and add the following content:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eaec689a394b390fd34da3055860568a/href">https://medium.com/media/eaec689a394b390fd34da3055860568a/href</a></iframe><p>Because Jest itself does not understand Vue’s Single File Components, we instruct it to use vue-jest as a preprocessor. In addition, even though the latest versions of Node already support most ES2015 features, you may still want to use ES modules syntax and stage-x features in your tests which is handled with babel-jest.</p><h3>3. Babel config</h3><p>As stated in the Vue documentation for testing single file components:</p><blockquote>The default Babel config disables ES modules transpilation because webpack already knows how to handle ES modules. However, we do need to enable it for our tests because Jest tests run directly in Node.</blockquote><p>[UPDATE 11/2019]: babel.config.js has been changed to utilize version 7 of babel.</p><p>Therefore we create a file called babel.config.js with the following content:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/02bca08db8facf03fe1a2c70259eacbc/href">https://medium.com/media/02bca08db8facf03fe1a2c70259eacbc/href</a></iframe><h3>4. Add test script to package.json</h3><p>What is left to do now, is to add a test-script to package.json as displayed below. In this case, the line &quot;test&quot;: &quot;jest&quot; tells your project to run the Jest test suite when issuing npm run test in the command line.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aa75c3b6bf7017523b17f799970d66b9/href">https://medium.com/media/aa75c3b6bf7017523b17f799970d66b9/href</a></iframe><h3>5. Writing tests</h3><p>Of course, the whole point of this article is to be able to test Vue components, so that’s what we’ll do here. As an example, I’m going to use the default Logo component Nuxt generates when using npx create-nuxt-app.</p><p>Jest expects the tests of a component to be in a folder called __tests__ (Note the double underscores). As a convention, the __tests__ folders are always in the same directory of your components. For example if you have a folder structure like this: components/Logo.vue, the test for the Logo component would live in components/__tests__/Logo.spec.js.</p><p>Note that the tests as illustrated below are really incomplete and only check if the component mounts and renders properly, however, it is enough to get you started.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a03b98288d196970d97215fcc2d267bb/href">https://medium.com/media/a03b98288d196970d97215fcc2d267bb/href</a></iframe><h3>6. Running tests</h3><p>Now run the tests with npm run test. It should print something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6Ikfzo6Fn6CEFWpQcVHgEA.png" /></figure><h3>Experimental setup with vue-cli</h3><p>You probably heard about the vue-cli with all its plugins and configurations you can easily install with a single command. Even though there is no mention of Nuxt being compatible with it, you can actually use it in this specific situation for setting up Jest.</p><p>Just run this command on a fresh project with no testing set up yet:</p><pre>vue add @vue/unit-jest</pre><p>It will automatically create all files needed (.babelrc and jest.config.js) and install the needed dependencies. It will also add this script to package.json:</p><pre>“test:unit”: “vue-cli-service test:unit”</pre><p>Which you will have to overwrite to:</p><pre>&quot;test&quot;: &quot;jest&quot;</pre><p>Additionally it will create a tests folder in the root of your project which you can delete. Now just do steps 5. and 6.</p><p>Final note here: even though it works I’m not sure if I would use it in production as I don’t know if it will stay compatible with Nuxt in the future.</p><h3>Git Repo</h3><p>If you would like to see this config in action I’ve set up a little git repo which you can clone. The master branch contains the manual setup and the vue-cli branch contains the experimental config.</p><p><a href="https://github.com/agcty/nuxt-jest-sample">agcty/nuxt-jest-sample</a></p><h3>Sources</h3><ul><li><a href="https://vue-test-utils.vuejs.org/guides/testing-single-file-components-with-jest.html">Testing Single-File Components with Jest | Vue Test Utils</a></li><li><a href="https://cli.vuejs.org/">Vue CLI</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8905d3880daa" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to properly set up ESLint with Prettier for Vue or Nuxt in VSCode]]></title>
            <link>https://medium.com/@gogl.alex/how-to-properly-set-up-eslint-with-prettier-for-vue-or-nuxt-in-vscode-e42532099a9c?source=rss-75ca831e3834------2</link>
            <guid isPermaLink="false">https://medium.com/p/e42532099a9c</guid>
            <category><![CDATA[nuxtjs]]></category>
            <category><![CDATA[vuejs]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Alex Gogl]]></dc:creator>
            <pubDate>Mon, 03 Dec 2018 22:28:59 GMT</pubDate>
            <atom:updated>2020-05-09T09:40:12.016Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to properly set up Nuxt with ESLint and Prettier in VSCode</h3><p><strong>Writing tech tutorials is hard because certain configurations might stop working after several months. As I’m also working on other things I don’t always have time to check if this is still working. However, if this tutorial doesn’t work for you anymore, please let me know ASAP! I really don’t want to waste your time doing something that doesn’t work anymore.</strong></p><p>With JavaScript being a dynamic and loosely-typed programming language problems and errors can potentially stay undetected for a very long time. To analyze code and to find errors, JavaScript applications are typically executed. This however, is a complete waste of processing power and hinders a fast development process.</p><p>As an alternative, static analysis tools can be used. One very common tool is “ESLint”, more specifically, it is a linting tool. Linting is defined as a type of static analysis that can be utilized to detect problematic code patterns or to enforce code style guidelines. This allows programmers to detect potential problems without executing the code. What is more, ESLint is a highly flexible and configurable in which every aspect can be adapted to fit a project’s needs. Configuring ESLint is not that hard, integrating custom rules, code formatting and properly setting up error highlighting is. This article aims to help developers who are using Nuxt and Visual Studio Code to set-up linting and code formatting.</p><p>Setting up linting and formatting requires 3 steps:</p><ol><li>Installing the needed development dependencies</li><li>Installing VSCode extensions (needed for error highlighting etc.)</li><li>Actually configuring ESLint to make it support .vue files and to integrate code formatting</li></ol><h3>1. Installing dev dependencies</h3><p>To install the development dependencies we issue the following command inside the root directory of our project:</p><pre>npm install eslint babel-eslint eslint-config-prettier eslint-plugin-prettier eslint-plugin-vue eslint-loader prettier -D</pre><p>This adds <strong>eslint</strong> (linting, error messages, etc.), <strong>babel-eslint</strong> (so we can use ES6+ features, flow types, etc.), <strong>eslint-config-prettier</strong> (special config for prettier so it will not interfere with other eslint formatting rules), <strong>eslint-plugin-prettier</strong> (runs prettier as an eslint rule so wrong formatting is seen as an error), <strong>prettier</strong> itself (to properly format code) and <strong>eslint-plugin-vue </strong>(adds vue support to eslint, e.g v-model cannot be used on a div element).</p><h3>2. Installing VSCode extensions</h3><p>The next step is to install some extensions, to enable error highlighting and automatic fixes. This step is fairly straightforward, we just need to install two extensions for VSCode:</p><p><strong>Vetur: </strong><a href="https://marketplace.visualstudio.com/items?itemName=octref.vetur">https://marketplace.visualstudio.com/items?itemName=octref.vetur</a></p><p><strong>ESLint: </strong><a href="https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint">https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint</a></p><p>By installing Vetur VSCode can now highlight the syntax of .vue files and by additionally installing the ESLint extension it is able to highlight syntax and pattern errors and errors which are defined by custom rules.</p><p><strong>UPDATE: </strong>VSCode changed the settings and it is now easier than ever to get prettier + eslint to work. The snippet below has been updated to reflect these changes.</p><p>An <strong>important</strong> note here: please do yourself a favor by adding the following snippet to your <strong>workspace</strong> settings, not your global VSCode settings. This will create an additional file in your project structure, which can be tracked with git and therefore shared between developers.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7f0e1902c1602ed54fa3f5d45a3ebe3a/href">https://medium.com/media/7f0e1902c1602ed54fa3f5d45a3ebe3a/href</a></iframe><h3>3. Configuring ESLint and Prettier</h3><p>The most important step is to actually configure eslint. I personally like to create a file called .eslintrc.js in my root folder which contains my settings as shown below. Another way would be to add the configuration to the package.json file which is a little less clean in my opinion.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7bafa64f212e7e00c7622c98db73271f/href">https://medium.com/media/7bafa64f212e7e00c7622c98db73271f/href</a></iframe><p>In the extends array ESLint is instructed to use the plugins we installed as dependencies earlier. As these four lines are probably the most important part of this article I will further elaborate:</p><ol><li>plugin:vue/recommended tells ESLint to incorporate Vue specific rules (e.g you cannot use v-model on divs)</li><li>eslint:recommended configures ESLint to use some default best-practice rules like getters always need to return a value (full list here: <a href="https://eslint.org/docs/rules/">https://eslint.org/docs/rules/</a>)</li><li>plugin:prettier/recommended turns on both eslint-plugin-prettier and eslint-config-prettierwhich tells ESLint to treat prettier errors as linting errors and disable certain rules that interfere with prettier (should prevent weird loops).</li><li>prettier/vue adds Vue specific formatting rules to prettier (e.g v-for comes before class)</li></ol><p>The vue/component-name-in-template-casing rule defines that custom components should be written in PascalCase. Setting up the “no-console” rule like I did allows us to use console.log during development but treats it as an error when the project is in production mode, which is regarded as a best practice. Last but not least, we define $nuxt as a global variable, if we did not do this, it would throw the “no-undefined variables” error when accessing $nuxt. This is useful for example when we need to retrieve a route name with $nuxt.$route.name and don’t want to use “this”).</p><h3>4. Roundup</h3><p>By now your files should be nice and tidy when hitting save and errors should be highlighted as soon as your fingers hit the keys. The system we put in place should now look somewhat like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zARpd8HgPcyqsEhe06NzYg.png" /></figure><p>We’ve got the plugins for ESLint to integrate Prettier and Vue rules. Babel-eslint serves as the parser which allows us to write ES6+ code and still get linting capabilities. And we have the ESLint VSCode extension which tells the Vetur Extension which errors to highlight and then actually highlights them.</p><p>That’s it! It took me a lot of time to properly understand how to integrate ESLint together with Prettier as the documentation of Nuxt and Vue don’t quite touch these topics in depth. Also some other ESLint tutorials which were specifically written for Nuxt or Vue seem half-baked and I feel like some of the authors didn’t even bother to properly read the documentation of each tool leading to unnecessary dependencies and weird formatting.</p><ul><li><a href="https://nuxtjs.org/guide/development-tools/">Development Tools - Nuxt.js</a></li><li><a href="https://vue-loader.vuejs.org/guide/linting.html#eslint">Linting | Vue Loader</a></li></ul><p>I hope this both helps and saves you a ton of time. Btw, did you know you can clap up to 50 times on a single article? If not you should probably try it out :)</p><p>Happy Coding!</p><p>PS: Also see my other article about how to set up Jest and Nuxt from scratch: <a href="https://medium.com/@gogl.alex/nuxt-jest-setup-from-scratch-8905d3880daa">https://medium.com/@gogl.alex/nuxt-jest-setup-from-scratch-8905d3880daa</a></p><h3>References</h3><ul><li><a href="https://vuejs.github.io/vetur/linting-error.html#linting">Linting / Error Checking | Vetur</a></li><li><a href="https://eslint.org/docs/about/">About</a></li><li><a href="https://prettier.io/docs/en/eslint.html">Integrating with ESLint · Prettier</a></li><li><a href="https://github.com/prettier/eslint-config-prettier">GitHub - prettier/eslint-config-prettier: Turns off all rules that are unnecessary or might conflict with Prettier.</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e42532099a9c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>