<?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 Lynn Jepsen on Medium]]></title>
        <description><![CDATA[Stories by Lynn Jepsen on Medium]]></description>
        <link>https://medium.com/@lynn.jepsen?source=rss-7bd39861b461------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*xneLL_MANoCuVZvoBYFbYA.jpeg</url>
            <title>Stories by Lynn Jepsen on Medium</title>
            <link>https://medium.com/@lynn.jepsen?source=rss-7bd39861b461------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 13:24:20 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@lynn.jepsen/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[Be Fearless with Screenshot Testing with routine-design]]></title>
            <link>https://medium.com/front-end-weekly/be-fearless-with-screenshot-testing-with-routine-design-3ae61d186174?source=rss-7bd39861b461------2</link>
            <guid isPermaLink="false">https://medium.com/p/3ae61d186174</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[screenshots]]></category>
            <category><![CDATA[components]]></category>
            <category><![CDATA[sass]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Lynn Jepsen]]></dc:creator>
            <pubDate>Wed, 02 Oct 2019 17:12:56 GMT</pubDate>
            <atom:updated>2019-10-02T17:12:56.418Z</atom:updated>
            <content:encoded><![CDATA[<h3>Be Fearless with Screenshot Testing and routine-design</h3><p>Are you afraid the next Pull Request in your React repository, will be the one that causes a serious visual regression? You’ve got plenty of PRs being sent to your GitHub repository every day. You try and review them all closely, but you’ve never felt confident reviewing changes to HTML and CSS. You maintain a high development velocity…but just <em>hope</em> you don’t accidentally break your website.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2Ff%2F788447%3Fas_embed%3Dtrue&amp;dntp=1&amp;display_name=Upscribe&amp;url=https%3A%2F%2Fupscri.be%2F788447%2F&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/18214d8333adbb5668f31791a3202b60/href">https://medium.com/media/18214d8333adbb5668f31791a3202b60/href</a></iframe><p>With <a href="http://bit.ly/2AP9IxB">routine-design</a>, you can create a safety net against visual regressions. Routine-design is a simple free tool you can immediately start using in your React repository. Routine-design works best on components that have been decoupled from other components. This allows you to test the visual result of HTML/CSS in isolation. Read on to see how to start screenshot testing your components!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RqFbszJ1V85on41EjYWz7w.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@brookelark?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Brooke Lark</a> on <a href="https://unsplash.com/s/photos/camer?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><h3>Decoupled Components</h3><p>Say you have a React component. It is basically a JSX file, paired with a CSS file. The JSX file has logic in its render method. The CSS file has the logic of selecting elements with CSS selectors and applying styling. If there is <em>lots</em> of logic in the render method, and/or the CSS file, then the component has <a href="http://bit.ly/2mnkO9t">low cohesion</a>.</p><p>If your component had <em>high</em> cohesion, you’d have the following benefits</p><ul><li>Component is simpler, having fewer operations</li><li>The overall component system is more maintainable, because you can change the code in one component without changing the code in other components</li><li>Developers will find the component easier to re-use</li></ul><p>In order to achieve high cohesion, you need to decouple your components. <a href="http://bit.ly/2kXeB3O">Low coupling</a> is often associated with high cohesion. To decouple a component, just extract some of the JSX/CSS logic into new files, creating a new React component.</p><p>Smaller, highly cohesive components are easier to screenshot test. The more screenshot testing coverage you have over your component system, the better protected you will be against visual regressions.</p><h3>routine-design</h3><p>First things first, install <a href="http://bit.ly/2AP9IxB">routine-design</a> into your React repository as a developer dependency.</p><pre>npm install --save-dev routine-design</pre><p>Next, you’re going to create a “render directory”. This is a <em>new</em> directory of React components. This is not the same as your src directory. This is a new directory of components, where each component wraps a single component from the src directory. Each of these component will be captured in a screenshot image. This way, you can take multiple screenshots of the same component, with each screenshot representing a different state. I often create this directory under ./test/render. Here’s an example screenshot-able React component that is testing the source code for the Logo component, which I would place in the ./test/render/logo/index.js file.</p><pre>import React, { Component } from &#39;react&#39;;</pre><pre>import Logo from &#39;../../../src/logo&#39;;</pre><pre>class LogoTest extends Component {</pre><pre>  render() {</pre><pre>    return (&lt;Logo/&gt;);</pre><pre>  }</pre><pre>}</pre><pre>export default LogoTest;</pre><p>Fill out your render directory with as many screenshot-able React components as you want. Next, test that all the components in the render directory actually…render. Add this command to your package.json.</p><pre>&quot;scripts&quot;: {</pre><pre>  &quot;render&quot;: &quot;routine-design render ./test/render&quot;,</pre><pre>  // other scripts like</pre><pre>  // &quot;start&quot;: &quot;react-scripts start&quot;</pre><pre>}</pre><p>Then run npm run render. This starts a server on localhost:8080. The ./test/render/logo/index.js will render on the localhost:8080/#/logo URL. Go to the URL associated with each screenshot-able component, and test that everything looks good.</p><p>Before we move on to capturing screenshots of these components, let me let you in on a little secret: <strong>do not save screenshot images in a repository</strong>. Git does not merge images well. Instead, store images on <a href="http://bit.ly/2BLsA2q">Google Cloud</a>, and keep track of their information in a JSON file. Images hosted on the cloud are easy to look at, and JSON files are easy to merge.</p><p>Create a new Google Cloud project if you don’t have one already. You’re going to store the screenshot images in a <a href="http://bit.ly/2kXeqFG">Google Cloud Storage Bucket</a>. Create a new storage bucket. You only need to fill out the “Name your bucket” step.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WjOW1bZ8c2jdBIXum8i1kg.png" /><figcaption>UI for creating a Google Cloud Storage bucket with name my-app-screenshots</figcaption></figure><p>In order to make screenshot image review as smooth as possible, I recommend making it possible for anyone to view these screenshot images. Change the permissions for the storage bucket so that allUsers can view objects in the storage bucket.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*enFnMW-vj9Lv6lvQw3GzXQ.png" /><figcaption>UI for adding allUsers as a Storage Object Viewer</figcaption></figure><p>I recommend only allowing an authorized service account to upload screenshot images to the storage bucket. <a href="http://bit.ly/2mBKxLt">Create a new service account</a>. Name the service account, and skip the step for “Grant this service account access to project”. You will specify the service account’s access from the Google Cloud Storage UI in a later step.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yzqTxc1rXojHn1TI-YENMQ.png" /><figcaption>UI for creating a Google Cloud service account called my-app-screenshots</figcaption></figure><p>And download a JSON key for the service account.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XuQcv4zTFbMXdRyMk-fA0A.png" /><figcaption>UI for downloading a JSON key for the GCP service account</figcaption></figure><p>Move the JSON file out of your Downloads directory. I recommend you move it to the same directory as your React repository, and add it to your .gitignore file. Set your GOOGLE_APPLICATION_CREDENTIALS environment variable to point to that JSON file.</p><pre>export GOOGLE_APPLICATION_CREDENTIALS=&quot;/home/user/my-app-repo/auth.json&quot;</pre><p>Then go <em>back</em> to your storage bucket and change so that the service account can create objects in the storage bucket.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ghLX7fdj6VZTwfuRsVHVew.png" /><figcaption>UI for adding service account as a Storage Object Creator</figcaption></figure><p>That is all you need to connect your developer environment to Google Cloud! But before we can test that you can actually upload screenshot images to Google Cloud…you need to set up one more service: <a href="https://dockr.ly/2GPkUin">Docker</a>. Docker is a way to run containers, built from images. I’ve created a <a href="https://dockr.ly/2BFIXgj">routine-design Docker image</a>, which packages up code needed to capture a screenshot with routine-design. This containerized software will always run the same, regardless of the infrastructure. <strong>Screenshot images captured on a local Mac will be the same as screenshot images captured on a remote Linux environment, like TravisCI.</strong></p><p>If you haven’t already, install <a href="https://dockr.ly/2SEL5PP">Docker Desktop</a> on you local developer environment. Make sure you actually start the application. This will make the docker command available in the terminal. You will be compiling Sass code on this docker image, so build a compatible binding for <a href="http://bit.ly/2tyFcoJ">node-sass</a>. There’s a handy routine-design command for that. Update your package.json with this new postinstall command</p><pre>&quot;scripts&quot;: {</pre><pre>  &quot;postinstall&quot;: &quot;docker pull lynnmercier/routine-design &amp;&amp; routine-design docker build-node-sass&quot;,</pre><pre>}</pre><p>And then run npm install to download the Docker image. You also need to set up one last environment variable. The ROUTINE_DESIGN_GOOGLE_CREDS variable will work for both your developer and CI environments.</p><pre>export ROUTINE_DESIGN_GOOGLE_CREDS=$(cat $GOOGLE_APPLICATION_CREDENTIALS)</pre><p>Finally, test that you can actually upload screenshot images to Google Cloud. Add this command to your package.json (note that you will need to specify the GCP project ID, and the name of the storage bucket)</p><pre>&quot;scripts&quot;: {</pre><pre>  &quot;capture&quot;: &quot;routine-design capture ./test/render gcp-project-id my-app-screenshots&quot;,</pre><pre>}</pre><p>And run npm run capture. This process can take more than a minute, so be patient. Once it is finished, run git status to see which files have changed. You should see some new ./test/render/**/image.json files. Open the image URLs in the image.json files to see the screenshot images. It’s actually <em>very important</em> that you open these image URLs and look at the images. You want to build up the habit of checking what the screenshots actually look like. Congratulations, you’ve captured containerized versions of screenshot images! Now with a one-line command you can capture screenshot images of all components in ./test/render.</p><p>Capturing screenshots is great, but we also want to be able to test that existing code <em>matches</em> the screenshots. There’s a handy routine-design command for that. Update your package.json with this new test:screenshots command (note that you will need to specify the GCP project ID, and the name of the storage bucket)</p><pre>&quot;scripts&quot;: {</pre><pre>  &quot;test:screenshots&quot;: &quot;routine-design test ./test/render gcp-project-id my-app-screenshots&quot;,</pre><pre>}</pre><p>Now run npm run test:screenshots. Again, this process can take more than a minute, so be patient. You should see a series of passing tests, one for each directory in the render directory. Congratulations! You now have a test suite for asserting local React/Sass code matches the images captured in image.json! With a one-line command you can test screenshot images of all components in ./test/render.</p><p>If you’re using <a href="http://bit.ly/2GuMRgb">TravisCI</a> as your CI, I suggest you update your .travis.yml file to have two separate jobs: one for unit tests and one for screenshot tests. You’ll also need to specify Docker as a service. Note that this code goes below the langauge: node_js section of your .travis.yml</p><pre>matrix:</pre><pre>  include:</pre><pre>    - script:</pre><pre>      - npm run test</pre><pre>    - services:</pre><pre>      - docker</pre><pre>      script:</pre><pre>      - npm run test:screenshots</pre><p>And finally…add these two to your .gitignore</p><pre>routine-design-output</pre><pre>auth.json</pre><p>Before you push all these changes as a PR to GitHub, update TravisCI with the ROUTINE_DESIGN_GOOGLE_CREDS environment variable. You can use this command echo $ROUTINE_DESIGN_GOOGLE_CREDS to print out the value. Copy that value into the TravisCI form, quoting it with single quotes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TCN-NFB2zs9__nWFV4Z0Ew.png" /><figcaption>TravisCI UI for adding environment variables</figcaption></figure><p>Now you can push all these changes as a PR to GitHub. TravisCI should kick off a build including screenshot testing. And the tests should pass! If you have any problems with routine-design, be sure to file bugs in the <a href="http://bit.ly/2Pi5lQS">GitHub repository</a>.</p><h3>Pull Requests</h3><p>Before you merge your routine-design PR into your code base, you’ll want to instruct your fellow developers how to use the screenshot system. First make sure developers know how to download the auth.json file: the service account JSON key.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*C1BNiO_LmzfLQtBWAkMLig.png" /><figcaption>UI for creating a key for a GCP service account</figcaption></figure><p>And make sure they set up their environment variables: GOOGLE_APPLICATION_CREDENTIALS and ROUTINE_DESIGN_GOOGLE_CREDS. I recommend including all of this in a README in your routine-design PR.</p><p>Developers on your team are responsible for both creating and reviewing PRs. Before they create a PR, they should make sure all tests pass. Make sure developers know to run npm run test:screenshots, and check that all tests pass, <em>before</em> creating a PR. If there is a failing test, routine-design will give you a convenient Google Cloud URL to help you debug the failure.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n45JoZO5W-W59zKEUvKIpA.png" /><figcaption>Console output of a failing screenshot test, including a Google Cloud URL</figcaption></figure><p>When you open the Google Cloud URL, you’ll see subdirectories for each screenshot image that failed within the directory. Each subdirectory has a new.png and a diff.png. New.png is the screenshot of the React/Sass code in the PR. Diff.png is the diff between new.png and the image saved in image.json. Include this information about npm run test:screenshot in your README.</p><p>Of course, your CI will flag any PRs that have failing tests, in case a developer forgets to run npm run test:screenshots. I <em>highly</em> recommend you set up your GitHub repository such that the CI <em>must</em> pass before a PR can be merged.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7sHwOe0L-HPvkQhesJV7zw.png" /><figcaption>UI for updating GitHub to require TravisCI to pass before merging a PR</figcaption></figure><p>With this safety net, you can be truly fearless about merging PRs without causing a visual regression.</p><h3>Review Visual Changes</h3><p>Sometimes developers actually want to change how a component looks. This is a visual change, not a visual regression. Say a developer writes some new code, runs npm run test:screenshot, a test fails, and they open the debug GCP URL. The developer sees that the new.png image looks good, and the diff.png makes sense with the new code. At this point, they need to update the golden screenshot image saved in image.json. This is what the npm run capture command is for. If the developer runs npm run capture, routine-design will upload new screenshot images for each changed component, and update the image.json files.</p><p>When the developer creates a PR, they will include any updated image.json files. If they don’t…the screenshot tests won’t pass and the CI won’t let them merge the PR. The developer reviewing the PR looks at the changes to image.json as part of the review process. Reviewers can open the image URLs in the image.json files to see the screenshot images. The reviewer can see both the old and new image URLs in GitHub’s review UI.</p><p>Including image.json files in a PR is basically a way for the PR author to signal to the reviewer that they had to run npm run capture. It’s clear that the reviewer needs to review visual changes caused by the new code. If the reviewer agrees with the visual changes, then they approve the PR, including the new image.json files. Once the PR is merged, the new image.json files become the “gold standard” for future PRs. In this way, the image.json files are the source of truth for how a component should look visually.</p><p>Include this information about npm run capture in the README in your routine-design PR. I’ve created a <a href="http://bit.ly/2ktPLZ1">simple example PR</a> you can copy. Once you’re happy with the routine-design PR, merge it! Now everyone on your developer team can start benefiting from screenshot testing!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3ae61d186174" width="1" height="1" alt=""><hr><p><a href="https://medium.com/front-end-weekly/be-fearless-with-screenshot-testing-with-routine-design-3ae61d186174">Be Fearless with Screenshot Testing with routine-design</a> was originally published in <a href="https://medium.com/front-end-weekly">Frontend Weekly</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[4 Steps to Visual Regression Testing]]></title>
            <link>https://medium.com/front-end-weekly/4-steps-to-visual-regression-testing-fe656d668ad9?source=rss-7bd39861b461------2</link>
            <guid isPermaLink="false">https://medium.com/p/fe656d668ad9</guid>
            <category><![CDATA[travis-ci]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[sass]]></category>
            <category><![CDATA[continuous-integration]]></category>
            <dc:creator><![CDATA[Lynn Jepsen]]></dc:creator>
            <pubDate>Sun, 17 Feb 2019 05:58:33 GMT</pubDate>
            <atom:updated>2019-11-16T17:50:31.021Z</atom:updated>
            <cc:license>http://creativecommons.org/licenses/by/4.0/</cc:license>
            <content:encoded><![CDATA[<p><strong><em>Update</em>: I wrote a new and improved version of this blog post here: </strong><a href="https://medium.com/front-end-weekly/be-fearless-with-screenshot-testing-with-routine-design-3ae61d186174"><strong>Be Fearless with Screenshot Testing and routine-design</strong></a></p><p>Do you review pull requests (PR) with HTML/JSX/React and CSS/Sass code? Do you find yourself relying on the PR author to test that the HTML/CSS <em>actually</em> works? Is it stressful when the PR author makes a mistake?</p><p>What if you had a safety net, so it was impossible to submit HTML/CSS code that does <strong>not</strong> work?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZvErxDHn8Utr9Ms5yXddyg.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/rWHp452M4yU?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Alberto Bigoni</a> on <a href="https://unsplash.com/search/photos/acrobatic?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>You can set up visual regression testing of your HTML/CSS in four easy steps. First, I’ll show you how to capture screenshot images on a <a href="https://dockr.ly/2GPkUin">Docker</a> image. This makes it easier to compare screenshots from a local developer environment to a Continuous Integration (CI) environment. Next, I’ll show you how to write <a href="http://bit.ly/2TSJkLY">Mocha</a> tests, which verify HTML/CSS code matches your captured screenshot images. Next, I’ll show you how to set up <a href="http://bit.ly/2GuMRgb">TravisCI</a>, which is the safety net for all PRs. And finally, I’ll show you how to review changes to screenshots, so your Web app can keep evolving.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2Ff%2F788447%3Fas_embed%3Dtrue&amp;dntp=1&amp;display_name=Upscribe&amp;url=https%3A%2F%2Fupscri.be%2F788447%2F&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/18214d8333adbb5668f31791a3202b60/href">https://medium.com/media/18214d8333adbb5668f31791a3202b60/href</a></iframe><h3>Capture on Docker</h3><p>Docker is a way to run containers, built from images. I’ve created a <a href="https://dockr.ly/2BFIXgj">routine-design Docker image</a>, which packages up code needed to capture a screenshot with <a href="http://bit.ly/2AP9IxB">routine-design</a>. This containerized software will always run the same, regardless of the infrastructure. Screenshot images captured on a local Mac will be the same as screenshot images captured on a remote Linux environment, like TravisCI.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bOrphmm1BtsoUc9rND7lmw.png" /><figcaption>diagram showing Docker’s containerized applications</figcaption></figure><p>But first, Read and follow the instructions from my two previous blog posts:</p><ul><li><a href="http://bit.ly/2E3xJ7B">Debug a React Component Faster with routine-design</a></li><li><a href="http://bit.ly/2A1QAgm">Capture React screenshots with routine-design</a></li></ul><p>By the end you will have an automated way of taking a screenshot of individual components.</p><p>Next, install <a href="https://dockr.ly/2SEL5PP">Docker Desktop</a> on you local developer environment. You will be compiling Sass code on this docker image, so build a compatible binding for <a href="http://bit.ly/2tyFcoJ">node-sass</a>. There’s a handy routine-design command for that. Update your package.json with this new postinstall command</p><pre>&quot;scripts&quot;: {</pre><pre>   &quot;postinstall&quot;: &quot;docker pull lynnmercier/routine-design &amp;&amp; routine-design docker build-node-sass&quot;,</pre><pre>  // other scripts like</pre><pre>  // &quot;start&quot;: &quot;react-scripts start&quot;</pre><pre>},</pre><p>Run npm i to pull the docker image and build node-sass. That one-line command is all you need to use routine-design in both your developer and CI environments.</p><p>Routine-design provides APIs for managing routine-design docker containers. It also provides an API, <a href="http://bit.ly/2XgAhq2">ComponentTree</a>, to easily scrape over test/render directory. So create a new capture.js file and import routine-design</p><pre>const RoutineDesign = require(&#39;routine-design&#39;);</pre><pre>class Capturer {<br>  <br>  async run() {</pre><pre>    const routineDesign = new RoutineDesign();</pre><pre>    //TODO fill me in with code<br>  <br>  }</pre><pre>}</pre><pre>new Capturer().run();</pre><p>Using <a href="http://bit.ly/2EcXSQI">RoutineDesignContainer</a>, start a Web server that renders individual components in the test/render directory. You’re basically running npm run render on the docker container. This command runs in detached mode, meaning you can send other commands to RoutineDesignContainer while the Web server is running. Next, for each directory, tell RoutineDesignContainer to capture a screenshot. You can do this with a single routine-design CLI command.</p><pre>const routineDesignContainer = routineDesign.getLocalStorage().createRoutineDesignContainer();</pre><pre>await routineDesignContainer.start();</pre><pre>await routineDesignContainer.run(&#39;routine-design render ./test/render&#39;, true);</pre><pre>const directories = routineDesign.createRoutineDesignTree(&#39;./test/render&#39;).getComponentTree().getDirectories();</pre><pre>for (let entry of directories) {<br>  <br>  const componentDirectoryId = entry[0];<br>  <br>  await routineDesignContainer.run(&#39;routine-design directory capture project-id my-app-screenshots ./test/render --component-directory=&#39;+componentDirectoryId);</pre><pre>}</pre><pre>await routineDesignContainer.cleanup();</pre><p>Update your package.json with a new capture command</p><pre>&quot;scripts&quot;: {</pre><pre>  &quot;capture&quot;: &quot;node ./capture.js&quot;,</pre><pre>  // other scripts</pre><pre>},</pre><p>Before you can run capture.js, you need to set up your <a href="https://cloud.google.com/">Google Cloud</a> authentication environment variables. The ROUTINE_DESIGN_GOOGLE_CREDS variable will work for both your developer and CI environments. Assuming you already have GOOGLE_APPLICATION_CREDENTIALS set, just run this command</p><pre>export ROUTINE_DESIGN_GOOGLE_CREDS=$(cat $GOOGLE_APPLICATION_CREDENTIALS)</pre><p>Now run npm run capture. This process can take 45 seconds, so be patient. Once it is finished, run git status to see which files have changed. You should see modifications to test/render/image.json and test/render/logo/image.json. The docker container actually mounts your local directory, so when it runs routine-design directory capture any changes to image.json are also modifications to your local directory.</p><p>Open the image URLs in the image.json files to see the new versions of the screenshot images. Congratulations, you’ve captured containerized versions of screenshot images! Now with a one-line command you can capture screenshot images of <strong>all</strong> components in test/render.</p><p>If you have any problems with routine-design, be sure to file bugs in the <a href="http://bit.ly/2Pi5lQS">GitHub repository</a>.</p><h3>Testing Screenshots</h3><p>You can use the Mocha test framework to write individual tests for each component directory. Install mocha as a developer dependency</p><pre>npm install --save-dev mocha</pre><p>Create a new test/render.test.js file, where you will write your Mocha tests.</p><pre>const RoutineDesign = require(&#39;routine-design&#39;);</pre><pre>const assert = require(&#39;assert&#39;);</pre><pre>describe(&#39;render&#39;, function() {<br>  <br>  const routineDesign = new RoutineDesign();<br>  <br>  //TODO fill me in with code</pre><pre>});</pre><p>Mocha lets you run specify code to run <a href="http://bit.ly/2N9bcbS">before</a> all tests have run. Tell Mocha that you want create a RoutineDesignContainer. Also tell RoutineDesignContainer to start a Web server that renders individual components in the test/render directory. This way, the Web server will already be running on the docker container before you take any screenshots.</p><pre>let routineDesignContainer;</pre><pre>before(async function() {</pre><pre>  const localStorage = routineDesign.getLocalStorage();</pre><pre>  routineDesignContainer = await localStorage.createRoutineDesignContainer();</pre><pre>  await routineDesignContainer.start();</pre><pre>  await routineDesignContainer.run(&#39;routine-design render ./test/render&#39;, true);</pre><pre>});</pre><p>Mocha also lets you specify code to run after all tests have run, so make sure you clean up the docker container.</p><pre>after(async function() {<br>  <br>  await routineDesignContainer.cleanup();</pre><pre>});</pre><p>Use the ComponentTree API to easily scrape over test/render directory. For each directory, tell RoutineDesignContainer to pixel validate the directory. This is a routine-design CLI command for diffing local React/Sass code against screenshots captured in image.json. The CLI command returns a JSON object, with allPass and debugId. AllPass is true when all the local screenshots match their image in image.json. DebugId represents a Google Cloud directory with debug images. Assert that allPass, and include an error message with a URL to the Google Cloud directory.</p><pre>const directories = routineDesign.createRoutineDesignTree(&#39;./test/render&#39;).getComponentTree().getDirectories();</pre><pre>for (let entry of directories) {<br>  <br>  const componentDirectoryId = entry[0];<br>  <br>  it(componentDirectoryId, async function() {<br>    <br>    const dockerCommand = &#39;routine-design directory pixel-validate project-id my-app-screenshots ./test/render --component-directory=&#39;+componentDirectoryId;<br>    <br>    const jsonStr = await routineDesignContainer.run(dockerCommand);<br>    <br>    const json = JSON.parse(jsonStr);<br>    <br>    let gcpUrl = &quot;https://console.cloud.google.com/storage/browser/my-app-screenshots/&quot;;<br>    <br>    if (componentDirectoryId != &#39;&#39;) {<br>      <br>      gcpUrl += componentDirectoryId+&quot;/&quot;;<br>      <br>    }<br>    <br>    gcpUrl += json.debugId+&quot;/?project=project-id&quot;;<br>    <br>    assert(json.allPass, gcpUrl);<br>  <br>  });</pre><pre>}</pre><p>Update your package.json with a new test command</p><pre>&quot;scripts&quot;: {</pre><pre>  &quot;test&quot;: &quot;mocha --timeout 60000 test/render.test.js&quot;,</pre><pre>  // other scripts</pre><pre>},</pre><p>Now run npm test. You should see the Mocha logging to the console that all your tests passed. Congratulations! You’ve just written a test suite for asserting local React/Sass code matches the images captured in image.json! Now with a one-line command you can test screenshot images of <strong>all</strong> components in test/render.</p><h3>Continuous Integration</h3><p>Take a moment to push your code to a <a href="http://bit.ly/2N8D2os">GitHub</a> repository. Next you’re going to be working with TravisCI, which is a Continuous Integration service that easily syncs with GitHub. TravisCI can run docker. Sign up for TravisCI (it is free). Go to <a href="https://travis-ci.org/account/repositories">https://travis-ci.org/account/repositories</a> and make sure your GitHub repository is enabled in TravisCI.</p><p>Go to your repository in Travis CI. Click More options &gt; Settings. Add a ROUTINE_DESIGN_GOOGLE_CREDS Environment Variable. You can use this command echo $ROUTINE_DESIGN_GOOGLE_CREDS to print out the value. Copy that value into the TravisCI form, quoting it with single quotes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zbAqMuTH599SdrNCZk0RQQ.png" /><figcaption>TraviCI Settings page, Environment Variables section</figcaption></figure><p>Return to your developer environment and make sure you have the master branch checked out. Do a git pull to pull the latest code. Create a new branch called infra/travis-ci</p><pre>git checkout -b infra/travis-ci</pre><p>You only need to create a single file to set up TravisCI. Create a new .travis.yml file that specifies your project is a NodeJs project. It also enable the <a href="http://bit.ly/2T20ngW">docker service</a></p><pre>language: node_js<br>node_js:<br>  - &quot;11&quot;<br>services:<br>  - docker</pre><p>git add . then</p><pre>git commit -m “infra: set up TravisCI”</pre><pre>git push --set-upstream origin infra/travis-ci</pre><p>This will push a new <a href="http://bit.ly/2NaXhSL">Git branch</a> to GitHub. From GitHub, you should see a notification to create a new PR from the infra/travis-ci branch. Create that PR. From TravisCI, you should see a build kicked off for the infra/travis-ci commit. Follow along in the logs to see TravisCI run the same npm install and npm test commands you ran locally. You should see the Mocha tests successfully pass, along with the TravisCI job.</p><p>In GitHub, navigate to the Branches page, under Settings, for your repository. Add a branch protection rule for the master branch. The rule should require that the branch is up to date and status checks pass. Check the continuous-integration/travis-ci status check.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ukcpbGaMWbNCE-3YhQyojA.png" /><figcaption>GitHub branch settings page will status checks enabled</figcaption></figure><p>Back in the PR you created, you should see that all checks have passed!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gMn65sXQ-yrb87WgyxPJww.png" /><figcaption>GitHub showing all checks have passed</figcaption></figure><p>Now you can confidently “Squash and merge” this PR into the master branch. Now all future PRs will make sure npm test passes, before giving the PR reviewer the green light.</p><p>Lets test that CI can actually <em>fail</em>. Return to your developer environment and checkout the latest version of master. Create a new branch called experimental/black-logo</p><pre>git checkout -b experimental/black-logo</pre><p>Open src/logo/logo.svg and change the #61DAFB color to #000000. If you run npm start you will see a black logo. If you run npm run render and open localhost:8080/#/logo you will also see a black logo.</p><p>git add . and then</p><pre>git commit -m “experimental: black logo”</pre><pre>git push --set-upstream origin experimental/black-logo</pre><p>Again, from GitHub, you should see a notification to create a new PR from the experimental/black-logo branch. Create that PR. Inside the PR you will find a notification of the running TravisCI job. Click on “Details”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uA3KoW-EiEmcHT9XQGf9xA.png" /><figcaption>TravisCI pending job notification</figcaption></figure><p>Follow along in the logs of that job. You should see the tests <strong>fail</strong>. The tests fail because the local React/Sass code does not match the images stored in image.json.</p><p>Mocha logs error messages for every failed test. The error messages should contain a URL to a Google Cloud directory.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*32gKqQtwRqd83yFnYdIMNw.png" /><figcaption>Console log of Mocha test</figcaption></figure><p>Open the Google Cloud URL. You should see subdirectories for each screenshot image that failed within the directory. Each subdirectory has a new.png and a diff.png. New.png is the screenshot of the React/Sass code in the PR. Diff.png is the diff between new.png and the image saved in image.json. Congratulations! You’ve collected debug information from a failed CI job. There’s one convenient Google Cloud URL for each failed test.</p><p>If you return back to the GitHub PR, you should see the status checks fail. So more congratulations! You’ve given your PR reviewer a safety net against visual regressions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*e3VBFU6F_mJfm1SiOCQjZA.png" /><figcaption>GitHub showing all checks have failed</figcaption></figure><h3>Screenshot Review</h3><p>Lets say we actually want to merge this black logo PR. Return to your developer environment, on the experimental/black-logo branch. Run npm run capture. Remember, this process can take 45 seconds, so be patient.</p><p>Once it is finished, run git status to see which files have changed. You should see modifications to test/render/image.json and test/render/logo/image.json. Remember you can open the image URLs in the image.json files to see the new versions of the screenshot images. You should see images with black logos.</p><p>git add . and then</p><pre>git commit -m “capture screenshot images”</pre><p>Then git push. From GitHub PR you should see a link to a new running TravisCI job. Follow along in the logs of that job. You should see the tests <strong>pass</strong>!</p><p>If you return back to the GitHub PR, you should see the status checks pass. Before you merge this PR, lets review it. Click on the “Files changed” tab. This tab shows diffs between the files on the master branch, and the files on the experimental/black-logo branch. You should see the diffs for both test/render/image.json and test/render/logo/image.json. Image.json has URLs you can easily open to look at the differences between master and experimental/black-logo.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KDpgSgQPZboQK6FFjtDedw.png" /><figcaption>GitHub showing diff in image.json</figcaption></figure><p>Look at you! You’re reviewing visual diffs before approving a PR. If the images on experimental/black-logo branch look good compared to the images on the master branch, then merge the PR!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fe656d668ad9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/front-end-weekly/4-steps-to-visual-regression-testing-fe656d668ad9">4 Steps to Visual Regression Testing</a> was originally published in <a href="https://medium.com/front-end-weekly">Frontend Weekly</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Capture React Screenshots with routine-design]]></title>
            <link>https://medium.com/front-end-weekly/capture-react-screenshots-with-routine-design-741bd14dd3f?source=rss-7bd39861b461------2</link>
            <guid isPermaLink="false">https://medium.com/p/741bd14dd3f</guid>
            <category><![CDATA[google-cloud-platform]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[sass]]></category>
            <category><![CDATA[chrome]]></category>
            <dc:creator><![CDATA[Lynn Jepsen]]></dc:creator>
            <pubDate>Sat, 15 Dec 2018 22:03:13 GMT</pubDate>
            <atom:updated>2019-02-16T22:33:35.127Z</atom:updated>
            <cc:license>http://creativecommons.org/licenses/by/4.0/</cc:license>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Y-K4bpH1IXEua6hcshfESw.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/hpjSkU2UYSU?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Carlos Muza</a> on <a href="https://unsplash.com/?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>Are you comfortable using screenshots in your development workflow?</p><p>If you’re working in a large React code base, with lots of components…you probably want screenshots of individual components.</p><p>I created <a href="http://bit.ly/2AP9IxB">routine-design</a> so developers can capture screenshots of their React components. Routine-design is easy to install in your Node.js repository. Once you set up a <a href="http://bit.ly/2BLsA2q">Google Cloud</a> project, you can capture screenshots of individual React components and store them on the cloud.</p><p>(This story is building off <a href="http://bit.ly/2RBx154">4 Secrets to Comfortably Working With Screenshots</a>. I basically automated the code parts.)</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2Ff%2F788447%3Fas_embed%3Dtrue&amp;dntp=1&amp;display_name=Upscribe&amp;url=https%3A%2F%2Fupscri.be%2F788447%2F&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/18214d8333adbb5668f31791a3202b60/href">https://medium.com/media/18214d8333adbb5668f31791a3202b60/href</a></iframe><h3>Google Cloud</h3><p>Do not save screenshot images in a repository. Git does not merge images well. Instead, store images on <a href="http://bit.ly/2BLsA2q">Google Cloud</a>, and keep track of their information in a JSON file. Images hosted on the cloud are easy to look at, and JSON files are easy to merge.</p><p>Sign into <a href="http://bit.ly/2BLsA2q">Google Cloud Platform</a> using a Google account. Click the “Try Free” button and follow the instructions to create a new Project. Take note of the project ID. You’ll need it later.</p><p>Once you’ve created a project, Go to IAM &amp; admin&gt; Service accounts</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/892/1*-50VBj3656uN7TcsqshJ2w.png" /><figcaption>Side navigation of Google Cloud Platform, showing IAM &amp; admin</figcaption></figure><p>Create a service account. Name your service account by combining the word “screenshots” with the name of your repository. You do not need specify a role fro the service account. Create a JSON key.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CLVTe51F0BeQI4d51Boepw.png" /></figure><p>The key is downloaded as a JSON file onto your computer. I recommend moving the JSON file <em>out</em> of your Downloads directory, and into a more permanent location. Now set your environment variable to point to that JSON file.</p><pre>export GOOGLE_APPLICATION_CREDENTIALS=&quot;/home/user/auth.json”</pre><p>Once you’ve created a service account, navigate to Google Cloud Storage.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/0*TN6ooiD5qceM-Evs" /><figcaption>Side navigation of Google Cloud Platform, showing Storage</figcaption></figure><p>Click the button for “Create Bucket”. Name your new bucket the same as the service account, by combining the word “screenshots” with the name of your repository.</p><p>Now click on the Permissions tab. Click “Add members” and add “allUsers” as the “Storage Object Viewer”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*RZus1iwgllHQyYTg" /><figcaption>Click “Add members” and add “allUsers” as the “Storage Object Viewer”</figcaption></figure><p>Now anyone can see the images hosted on the cloud.</p><p>But not <em>everyone</em> can upload images to the cloud, only authorized users. So click “Add members” again and add the service account as the “Storage Object Creator”</p><p>That is all you need to connect your developer environment to Google Cloud. Unfortunately, you won’t know if you did it correctly until you go through the next section, which will actually upload images to Google Cloud.</p><h3>Capture</h3><p>First, read and follow the instructions from my previous blog post: <a href="http://bit.ly/2E3xJ7B">Debug a React Component Faster with routine-design</a>. By the end you will have a Web server which renders individual components. The rest of this blog post will explain how to automate taking screenshots of <em>that</em> Web server.</p><p>Screenshots work best when they are deterministic. Which is why I suggest <em>not</em> taking screenshots of components in the middle of an animation. My blog post about creating a RenderServer, builds on top of <a href="http://bit.ly/2FBQcuj">Create React App</a> (CRA). But CRA has a constantly spinning logo. In order to capture a deterministic screenshot of the logo component, I suggest you <em>remove</em> the animation property from src/logo/index.scss.</p><p>Update your package.json with a new script that captures ./test/render/logo. The script will save the screenshot images on Google Cloud, using the project ID and storage bucket name</p><pre>&quot;scripts&quot;: {</pre><pre>  &quot;capture:logo&quot;: &quot;routine-design directory capture project-id my-app-screenshots ./test/render --component-directory=logo&quot;,</pre><pre>  // other scripts like</pre><pre>  // &quot;start&quot;: &quot;react-scripts start&quot;</pre><pre>},</pre><p>In one tab, run npm run render. You need this RenderServer running. Otherwise there will be no screenshots to capture. In <em>another</em> tab, first remember to export GOOGLE_APPLICATION_CREDENTIALS, then run</p><pre>npm run capture:logo</pre><p>Now open the test/render/logo/image.json. You should see an image URL. Open this URL, and you should see a screenshot of the Logo component. Congratulations! You’ve captured a screenshot of an individual React component.</p><p>If you have any problems with routine-design, be sure to file bugs in the <a href="http://bit.ly/2Pi5lQS">GitHub repository</a>.</p><h3>Image.json</h3><p>The image.json file only updates when the rendering changes. Go ahead and commit your changes, include the new test/render/logo/image.json file. Now run npm run capture:logo again. If you run git status you’ll see that the image.json file has <em>not</em> changed.</p><p>Make a habit of updating image.json for every git commit. Add more individual components to ./test/render as you add more features. These two habits increase coverage of your user visible output. When you go to refactor React and Sass code, use this coverage to check that you did <em>not</em> make any user visible changes. Just run npm run capture:foo and then git status. If the image.json does not change, you have not made a user visible change.</p><p>But if the rendering does change, then so will image.json. Update src/logo/index.scss to</p><pre>.App-logo {</pre><pre>  height: 50vmin;</pre><pre>}</pre><p>Run npm run capture:logo. Then open test/render/logo/image.json and open the image URL. The logo in the screenshot should be bigger. And if you run git status again, you’ll see that image.json has changed.</p><p>If you make a habit of updating image.json for every git commit, you’ll build an accurate git history for image.json. You can look at the history of image.json to see all related Pull Requests (PRs). And remember, image.json has URLs for each image. So you can look at the exact screenshot of that component in that PR.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=741bd14dd3f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/front-end-weekly/capture-react-screenshots-with-routine-design-741bd14dd3f">Capture React Screenshots with routine-design</a> was originally published in <a href="https://medium.com/front-end-weekly">Frontend Weekly</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[4 Secrets to Comfortably Working With Screenshots]]></title>
            <link>https://medium.com/front-end-weekly/4-secrets-to-comfortably-working-with-screenshots-a299846e6fce?source=rss-7bd39861b461------2</link>
            <guid isPermaLink="false">https://medium.com/p/a299846e6fce</guid>
            <category><![CDATA[sass]]></category>
            <category><![CDATA[chrome]]></category>
            <category><![CDATA[google-cloud-platform]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Lynn Jepsen]]></dc:creator>
            <pubDate>Wed, 05 Dec 2018 15:41:54 GMT</pubDate>
            <atom:updated>2019-02-16T23:44:25.880Z</atom:updated>
            <cc:license>https://creativecommons.org/publicdomain/mark/1.0/</cc:license>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aJtI71FFcAJonicZj7G_Bw.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/i5iIhHSAtp4?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Daria Nepriakhina</a> on <a href="https://unsplash.com/search/photos/developer-comfortable?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>Are you comfortable using screenshots in your development workflow?</p><p>If you’re working in a large React code base, with lots of components…you probably want screenshots of <em>individual</em> components.</p><p>We all want screenshots to help us code review HTML and CSS.</p><p>This blog post gives you a lightweight way to integrate screenshots into your React development. I’ll give you my secret recipe for good screenshot workflow. And I’ll teach you the secret to managing images in a repository.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2Ff%2F788447%3Fas_embed%3Dtrue&amp;dntp=1&amp;display_name=Upscribe&amp;url=https%3A%2F%2Fupscri.be%2F788447%2F&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/18214d8333adbb5668f31791a3202b60/href">https://medium.com/media/18214d8333adbb5668f31791a3202b60/href</a></iframe><h3>Screenshot Workflow</h3><p>There are two secrets to good screenshot workflow</p><ul><li>Automation</li><li>Screenshot individual components</li></ul><p>Automate taking screenshots, and you are more likely to use screenshots on a day-to-day basis. Take screenshots of individual components, and your images will be easier to navigate.</p><p>Create a Web server, which renders individual components, by reading <a href="http://bit.ly/2E3xJ7B">Debug a React Component Faster with routine-design</a>. The rest of this blog post will explain how to automate taking screenshots of <em>that</em> Web server, which I call the RenderServer.</p><p>Screenshots work best when they are deterministic. Which is why I suggest <em>not</em> taking screenshots of components in the middle of an animation. My blog post about creating a RenderServer, builds on top of <a href="http://bit.ly/2FBQcuj">Create React App</a> (CRA). But CRA has a constantly spinning logo. In order to capture a deterministic screenshot of the logo component, I suggest you <em>remove</em> the animation property from src/logo/index.scss.</p><p><a href="http://bit.ly/2EbH4uM">Puppeteer</a> is a headless version of Chrome. It automates capturing screenshots. Install it in your repository.</p><pre>npm install --save-dev puppeteer</pre><p>By combining <a href="http://bit.ly/2AP9IxB">routine-design</a> with Puppeteer, you are more likely to look at screenshots of individual components on a day-to-day basis. Constant review will naturally increase the quality of your React code. It is hard to ignore visual bugs once you have captured them in an image.</p><h3>Capture.js</h3><p>Assuming you’ve just read <a href="http://bit.ly/2E3xJ7B">Debug a React Component Faster with routine-design</a>, then you’ve created a new test/render directory on top of CRA. From now on, I’m going to call this directory the “render directory”, because it is the directory that contains all the components we want to render.</p><p>Create a new capture.js file. This is where you will write code to automate taking screenshots.</p><pre>const puppeteer = require(‘puppeteer’);</pre><pre>const RoutineDesign = require(‘routine-design’);</pre><pre>async function run(renderDirectory) {</pre><pre>  //TODO, replace me with code</pre><pre>}</pre><pre>run(‘./test/render’);</pre><p>Note that the run function is asynchronous. Inside the run function, create a <a href="http://bit.ly/2GsuV5V">RoutineDesignTree</a> with your render directory. Then launch a new Puppeteer browser. Use the browser and WebPage to check if the server has finished compiling JavaScript/CSS. Once everything is ready, iterate over all the directories within the render directory. At the end of the run function, exit the process. This will turn off the server and close the browser.</p><pre>const routineDesign = new RoutineDesign();</pre><pre>const routineDesignTree = routineDesign.createRoutineDesignTree(renderDirectory);</pre><pre>routineDesignTree.render();</pre><pre>const browser = await puppeteer.launch();</pre><pre>const webPage = routineDesign.createWebPage(browser, 8080, &#39;/#/app&#39;);</pre><pre>await webPage.waitForResolution(10);</pre><pre>const directories = routineDesignTree.getComponentTree().getDirectories();</pre><pre>for (let entry of directories) {</pre><pre>  const componentDirectory = entry[1];</pre><pre>  //TODO, replace me with code</pre><pre>}</pre><pre>process.exit(0);</pre><p>Within the for-loop, iterate over each component file within the directory. Take a screenshot of each component, using routine-design’s <a href="http://bit.ly/2tphJWP">LocalStorage</a>.</p><pre>const componentFiles = componentDirectory.getFiles();</pre><pre>for (let i=0; i&lt;componentFiles.length; i++) {</pre><pre>  const componentFile = componentFiles[i];</pre><pre>  const page = await browser.newPage();</pre><pre>  await page.goto(‘http://localhost:8080/#/&#39; + componentFile.getPath());</pre><pre>  const localImage = routineDesign.getLocalStorage().createLocalImage()</pre><pre>  await localImage.prepareForWriting();</pre><pre>  await page.screenshot({path: localImage.getPath()});</pre><pre>}</pre><p>Now run</p><pre>node ./capture.js</pre><p>If you open routine-design-output/images you should see two images, one for src/logo/index.js and one for src/App.js. Congratulations! You’ve automated taking screenshots of individual components.</p><p>If you have any problems with routine-design, be sure to file bugs in the <a href="http://bit.ly/2Pi5lQS">GitHub repository</a>.</p><h3>Images in Developer Environment</h3><p>The secret to managing images in a repository is: <em>do not save images in a repository.</em> Git does not merge images well. Instead, store images on the cloud, and keep track of their information in a JSON file. Images hosted on the cloud are easy to look at, and JSON files are easy to merge.</p><p><a href="http://bit.ly/2BLsA2q">Google Cloud</a> is a simple way to store images on the cloud. The newest feature in routine-design — <a href="http://bit.ly/2GqlSlK">ImageStorage</a> — uploads images to Google Cloud Storage. ImageStorage keeps track of image information by reading from and writing to a JSON file.</p><p>Sign into <a href="http://bit.ly/2BLsA2q">Google Cloud Platform</a> using a Google account. Click the “Try Free” button and follow the instructions to create a new Project. Take note of the project ID. You’ll need it later.</p><p>Once you’ve created a project, Go to IAM &amp; admin&gt; Service accounts</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/892/1*-50VBj3656uN7TcsqshJ2w.png" /><figcaption>Side navigation of Google Cloud Platform, showing IAM &amp; admin</figcaption></figure><p>Create a service account. Name your service account by combining the word “screenshots” with the name of your repository. You do not need specify a role fro the service account. Create a JSON key.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CLVTe51F0BeQI4d51Boepw.png" /></figure><p>The key is downloaded as a JSON file onto your computer. I recommend moving the JSON file <em>out</em> of your Downloads directory, and into a more permanent location. Now set your environment variable to point to that JSON file.</p><pre>export GOOGLE_APPLICATION_CREDENTIALS=&quot;/home/user/auth.json”</pre><p>Once you’ve created a service account, navigate to Google Cloud Storage.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/0*TN6ooiD5qceM-Evs" /><figcaption>Side navigation of Google Cloud Platform, showing Storage</figcaption></figure><p>Click the button for “Create Bucket”. Name your new bucket the same as the service account, by combining the word “screenshots” with the name of your repository.</p><p>Now click on the Permissions tab. Click “Add members” and add “allUsers” as the “Storage Object Viewer”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*RZus1iwgllHQyYTg" /><figcaption>Click “Add members” and add “allUsers” as the “Storage Object Viewer”</figcaption></figure><p>Now anyone can see the images hosted on the cloud.</p><p>But not <em>everyone</em> can upload images to the cloud, only authorized users. So click “Add members” again and add the service account as the “Storage Object Creator”</p><p>That is all you need to connect your developer environment to Google Cloud. Unfortunately, you won’t know if you did it correctly until you go through the next section, which will actually upload images to Google Cloud.</p><h3>Store Images in the Cloud</h3><p>Update your capture.js file within the componentDirectories for-loop to initialize an ImageStorage variable with the Google Cloud project ID and storage bucket name. Each componentFile in the directory automatically matches a single componentImage object. At the end of the for-loop, you’ll need to ImageStorage.save, which writes the information to an image.json file within the componentDirectory.</p><pre>const componentFiles = componentDirectory.getFiles();<br><br>const gcpImageBucket = routineDesign.createGcpImageBucket(&#39;project-id&#39;, &#39;my-app-screentshots&#39;);</pre><pre>const routineDesignDirectory = gcpImageBucket.createRoutineDesignDirectory(componentDirectory);</pre><pre>const imageStorage = routineDesignDirectory.getImageStorage();</pre><pre>const componentImages = await imageStorage.getImages();</pre><pre>for (let i=0; i&lt;componentFiles.length; i++) {</pre><pre>  const componentFile = componentFiles[i];</pre><pre>  const componentImage = componentImages[i];</pre><pre>  ...</pre><pre>}</pre><pre>await imageStorage.save();</pre><p>Update the internal componentFiles for-loop to save the localImage to the componentImage. You no longer need to keep the localImage, so call LocalImage.delete to clean it up. Every time you save a new localImage, you update the image.json file in the componentDirectory. If the screenshot doesn’t change, there is no need to update the image.json file. The following logic will <em>only</em> save a localImage if the screenshot has changed.</p><pre>const localImagePng = await localImage.getPng();</pre><pre>const imageId = componentImage.getId();</pre><pre>if (imageId) {</pre><pre>  const goldenPng = await componentImage.createGcpImage().download();</pre><pre>if (!Buffer.compare(localImagePng.data, goldenPng.data) === 0) {</pre><pre>    await componentImage.saveImage(localImagePng);</pre><pre>  }</pre><pre>} else {</pre><pre>  await componentImage.saveImage(localImagePng);</pre><pre>}</pre><pre>await localImage.delete();</pre><p>Run node ./capture.js to run this new code. If you open test/render/logo/image.json you should an image URL. Open this URL, and you should see a screenshot of the Logo component. Congratulations! You’ve <em>really</em> automated taking screenshots of individual components. The images are stored on Google Cloud Platform, and image.json tracks the changes.</p><p>Remember, if you have any problems with routine-design, be sure to file bugs in the <a href="http://bit.ly/2Pi5lQS">GitHub repository</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a299846e6fce" width="1" height="1" alt=""><hr><p><a href="https://medium.com/front-end-weekly/4-secrets-to-comfortably-working-with-screenshots-a299846e6fce">4 Secrets to Comfortably Working With Screenshots</a> was originally published in <a href="https://medium.com/front-end-weekly">Frontend Weekly</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Debug a React Component Faster with routine-design]]></title>
            <link>https://medium.com/front-end-weekly/debug-a-react-component-faster-with-routine-design-e896a89982a0?source=rss-7bd39861b461------2</link>
            <guid isPermaLink="false">https://medium.com/p/e896a89982a0</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[design-routine]]></category>
            <category><![CDATA[sass]]></category>
            <category><![CDATA[css]]></category>
            <dc:creator><![CDATA[Lynn Jepsen]]></dc:creator>
            <pubDate>Mon, 19 Nov 2018 16:14:41 GMT</pubDate>
            <atom:updated>2019-02-16T23:43:58.054Z</atom:updated>
            <cc:license>http://creativecommons.org/licenses/by/4.0/</cc:license>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pv0Fj08KJTc2dPnBBGg07g.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/zHNS-vrAVGU?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">rawpixel</a> on <a href="https://unsplash.com/search/photos/website?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>When you create a React component, you put HTML in the render method. How do you know that HTML will render the way you want it to render?</p><p>And what about the Sass file you bundled with the React component. Does it style the HTML in the way you intended?</p><p>I created <a href="https://www.npmjs.com/package/routine-design">routine-design</a> so developers can check how browsers render their React components. Routine-design is easy to install in your Node.js repository. Once it is set up, it is easy to debug individual React components.</p><p>(This story is building off <a href="https://medium.com/@lynn.mercier/3-techniques-for-fast-debugging-of-a-react-component-63cb135371ce">Three Techniques for Quickly Rendering a Component</a>. I basically automated all the code parts.)</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2Ff%2F788447%3Fas_embed%3Dtrue&amp;dntp=1&amp;display_name=Upscribe&amp;url=https%3A%2F%2Fupscri.be%2F788447%2F&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/18214d8333adbb5668f31791a3202b60/href">https://medium.com/media/18214d8333adbb5668f31791a3202b60/href</a></iframe><h3>High Quality HTML/CSS</h3><p><a href="https://github.com/facebook/create-react-app">Create React App</a> (CRA) creates a very simple React application you run on localhost:3000. It only has one component: App.js. But if you look at App.js and App.scss source code, you’ll see that the logo can be a component in and of itself.</p><p><strong>Proposed</strong> logo/index.js:</p><pre>import React, { Component } from &#39;react&#39;;</pre><pre>import logo from &#39;./logo.svg&#39;;</pre><pre>import &#39;./index.scss&#39;;</pre><pre>class Logo extends Component {</pre><pre>render() {</pre><pre>  return (&lt;img src={logo} className=&quot;App-logo&quot; alt=&quot;logo&quot; /&gt;);</pre><pre>}}</pre><pre>export default Logo;</pre><p><strong>Proposed</strong> logo/index.scss:</p><pre>.App-logo {</pre><pre>animation: App-logo-spin infinite 20s linear;</pre><pre>height: 40vmin;</pre><pre>}</pre><pre>@keyframes App-logo-spin {</pre><pre>from { transform: rotate(0deg); }</pre><pre>to { transform: rotate(360deg); }</pre><pre>}</pre><p>You need to check that logo/index.js and logo/index.scss render properly in a <strong>real browser</strong>, because there are plenty of places in the code you could have written something unintentional:</p><ul><li>Maybe the className in logo/index.js doesn’t match the CSS selector in logo/index.scss.</li><li>Maybe you mistyped the import path to logo.svg.</li><li>Maybe you mistyped the keyframe name, App-logo-spin</li><li>Maybe 40vmin is way too big. Or way too small!</li></ul><p>Now browsers only care about HTML and CSS. Browsers don’t care about the React code that <em>made</em> the HTML. Or the Sass code that <em>made</em> the CSS. So you need to check the <strong>full</strong> pipeline:</p><ul><li>First: React+Sass becomes HTML+CSS code</li><li>Then: the browser renders HTML+CSS into pixels</li></ul><p>Routine-design sets up this full pipeline.</p><h3>Getting Started with routine-design</h3><p>Create a test/render directory. I recommend keeping your test/render directory as a “mirror” of the src directory. In the CRA example, src directory has</p><ul><li>src/App.js</li><li>src/logo/index.js</li></ul><p>So create</p><ul><li>test/render/App.js</li><li>test/render/logo/index.js</li></ul><p><strong>Proposed</strong> test/render/App.js:</p><pre>import React, { Component } from &#39;react&#39;;</pre><pre>import App from &#39;../../src/App.js&#39;;</pre><pre>class AppTest extends Component {</pre><pre>render() {return (&lt;App/&gt;);}</pre><pre>}</pre><pre>export default AppTest;</pre><p><strong>Proposed</strong> test/render/logo/index.js:</p><pre>import React, { Component } from &#39;react&#39;;</pre><pre>import Logo from &#39;../../../src/logo/index.js&#39;;</pre><pre>class LogoTest extends Component {</pre><pre>render() {return (&lt;Logo/&gt;);}</pre><pre>}</pre><pre>export default LogoTest;</pre><p>Now we are going to use routine-design to scrape the test/render directory, and render each component in the browser. First, install routine-design through NPM</p><pre>npm install --save-dev routine-design</pre><p>Update your package.json with a new script that points to the test/render directory</p><pre>&quot;scripts&quot;: {</pre><pre>  &quot;render&quot;: &quot;routine-design render ./test/render&quot;,</pre><pre>  // other scripts like</pre><pre>  // &quot;start&quot;: &quot;react-scripts start&quot;</pre><pre>},</pre><p>Now run npm run render. In the browser, open localhost:8080/#/logo. Congratulations, you’ve made a server for rendering individual components! This URL renders the Logo component, from src/logo/index.js, including the styling in src/logo/index.scss.</p><p>Leave the server running and edit either src/logo/index.js or src/logo/index.scss. Your changes will be reflected immediately in the browser (unless you break something and it can’t compile, then you’ll see an error in the terminal).</p><p>Routine-design writes files to the routine-design-output directory. I recommend you add routine-design-output to your .gitignore.</p><p>If you have any problems with routine-design, be sure to file bugs in the <a href="https://github.com/lynn-mercier/routine-design">GitHub repository</a>.</p><h3>Source of Truth</h3><p>Routine-design generates URLs based on the directory structure of test/render. If you open localhost:8080/#/app you’ll see the App component.</p><p>However, the App component you see on localhost:8080/#/app is not <em>exactly</em> the same as the App component you see on localhost:3000. This is because CRA doesn’t put 100% of the App component styling in App.scss. CRA puts some global styles, like font-family, in index.css. Since App.js never imports index.css, the styles do <em>not</em> show up in routine-design’s render server.</p><p>Routine-design’s render server is a source of truth for what <em>is</em> and <em>is not</em> included in a given component. This kind of guarantee is useful when you want to share components, especially across multiple React applications.</p><p>So if you are trying to build a component library, routine-design can help keep you honest, and ensure high quality React+Sass code.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e896a89982a0" width="1" height="1" alt=""><hr><p><a href="https://medium.com/front-end-weekly/debug-a-react-component-faster-with-routine-design-e896a89982a0">Debug a React Component Faster with routine-design</a> was originally published in <a href="https://medium.com/front-end-weekly">Frontend Weekly</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[3 Techniques for Fast Debugging of a React Component]]></title>
            <link>https://medium.com/@lynn.jepsen/3-techniques-for-fast-debugging-of-a-react-component-63cb135371ce?source=rss-7bd39861b461------2</link>
            <guid isPermaLink="false">https://medium.com/p/63cb135371ce</guid>
            <category><![CDATA[webpack]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[css]]></category>
            <category><![CDATA[html]]></category>
            <dc:creator><![CDATA[Lynn Jepsen]]></dc:creator>
            <pubDate>Tue, 30 Oct 2018 15:51:33 GMT</pubDate>
            <atom:updated>2018-10-31T21:44:07.452Z</atom:updated>
            <cc:license>https://creativecommons.org/publicdomain/mark/1.0/</cc:license>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cyq39_Fi_qPwiAJqgeCsCg.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/kwzWjTnDPLk?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">NESA by Makers</a> on <a href="https://unsplash.com/search/photos/developer-woman?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>Do you wish you could debug a React component in isolation? If you’re working in a large React code base, with lots of components…sometimes you want to debug/experiment/play with just one component.</p><p>We want component sandboxes because they make pull requests easier to finish. We want to <strong>confidently write React code</strong>, test that it works, and merge the pull request.</p><p>I propose creating a separate server (aka, <strong>not</strong> the one npm start starts), where you register component code to unique URLs. Each URL becomes it’s own sandbox. This server is backed by a minimal <a href="https://webpack.js.org/">Webpack</a> configuration. You can mirror your existing src directory structure into this server.</p><p>Combine these techniques to create sandboxes for your components. Lets get those those pull requests finished faster!</p><h3>Render on a Separate Server</h3><p>Most React servers have an App.js component, which renders the entire application. If you used <a href="https://github.com/facebook/create-react-app">Create React App</a> (CRA), then it created App.js for you. CRA also populates the npm start logic in package.json. Running npm start opens localhost:3000, where it renders the App.js component</p><p>I’m proposing you create an <strong>additional</strong> separate server which renders individual components. This server starts with npm run render-server. The idea is, if you open localhost:8080/#/foo it renders the foo/index.js component.</p><p>CRA purposely creates a very simple React server. It only has one component: App.js. If you look at App.js and App.scss* source code, you’ll see that the logo can be a component in and of itself.</p><p><strong>Proposed</strong> logo/index.js:</p><pre>import React, { Component } from &#39;react&#39;;</pre><pre>import logo from &#39;./logo.svg&#39;;</pre><pre>import &#39;./index.scss&#39;;</pre><pre>class Logo extends Component {</pre><pre>  render() {</pre><pre>    return (&lt;img src={logo} className=&quot;App-logo&quot; alt=&quot;logo&quot; /&gt;);</pre><pre>}}</pre><pre>export default Logo;</pre><p><strong>Proposed</strong> logo/index.scss:</p><pre>.App-logo {</pre><pre>  animation: App-logo-spin infinite 20s linear;</pre><pre>  height: 40vmin;</pre><pre>}</pre><pre>@keyframes App-logo-spin {</pre><pre>  from { transform: rotate(0deg); }</pre><pre>  to { transform: rotate(360deg); }</pre><pre>}</pre><p>The new server will help you <strong>check</strong> this code actually works. This server will start localhost:8080/#/logo to render this logo/index.js component.</p><h3>Minimal Webpack Server</h3><p>Your component rendering server is just a matter of running <a href="https://webpack.js.org/configuration/dev-server/">Webpack Dev Server</a> with a Webpack config and a content base directory. Install <a href="https://webpack.js.org/api/cli/">webpack-cli</a> and <a href="https://github.com/webpack-contrib/file-loader">file-loader</a> (which you’ll need for the Webpack configuration):</p><pre>npm install --save-dev webpack-cli file-loader</pre><p>I propose you create your Webpack base directory under test/render. This is separate from the src directory. Within the test/render directory, create webpack.config.js</p><pre>const webpack = require(&#39;webpack&#39;);</pre><pre>const MiniCssExtractPlugin = require(&#39;mini-css-extract-plugin&#39;);</pre><pre>module.exports = {</pre><pre>  entry: [`./test/render/index.js`],</pre><pre>  output: {filename: &#39;bundle.js&#39;, path: __dirname},</pre><pre>  module: {</pre><pre>    rules: [{</pre><pre>      test: /\.js$/,</pre><pre>      exclude: /(node_modules)/,</pre><pre>      use: {</pre><pre>        loader: &#39;babel-loader&#39;,</pre><pre>        options: {plugins: [&#39;@babel/plugin-transform-react-jsx&#39;]}</pre><pre>      }</pre><pre>    },{</pre><pre>      test: /\.scss$/,</pre><pre>      use: [MiniCssExtractPlugin.loader,&#39;css-loader&#39;,&#39;sass-loader&#39;,]</pre><pre>    },{</pre><pre>      test: /\.(png|svg|jpg|gif)$/,</pre><pre>      use: [&#39;file-loader&#39;]</pre><pre>    }],</pre><pre>  },</pre><pre>  plugins: [new MiniCssExtractPlugin({filename: &#39;bundle.css&#39;})],</pre><pre>};</pre><p>Also under your test/render directory, create index.html</p><pre>&lt;!DOCTYPE html&gt;</pre><pre>&lt;html&gt;</pre><pre>  &lt;head&gt;</pre><pre>    &lt;meta charset=&quot;utf-8&quot;&gt;</pre><pre>    &lt;meta name=&quot;viewport&quot;</pre><pre>     content=&quot;width=device-width, initial-scale=1&quot;&gt;</pre><pre>    &lt;link rel=&quot;stylesheet&quot; href=&quot;./bundle.css&quot;&gt;</pre><pre>  &lt;/head&gt;</pre><pre>&lt;body&gt;</pre><pre>  &lt;div id=&#39;root&#39;&gt;&lt;/div&gt;</pre><pre>&lt;/body&gt;</pre><pre>&lt;script src=&#39;./bundle.js&#39;&gt;&lt;/script&gt;</pre><pre>&lt;/html&gt;</pre><p>The index.html imports the bundle.css and bundle.js specified in the webpack.config.js. Finally, still under the test/render directory, create index.js</p><pre>import React from &#39;react&#39;;</pre><pre>import ReactDOM from &#39;react-dom&#39;;</pre><pre>ReactDOM.render(</pre><pre>  &lt;div&gt;Hi there. You will replace me.&lt;/div&gt;,</pre><pre>  document.getElementById(&#39;root&#39;));</pre><p>The index.js references the root element created in index.html. Now, in the package.json file, add a new script called render-server. It should have the following script value:</p><pre>webpack-dev-server</pre><pre>  --config test/render/webpack.config.js</pre><pre>  --content-base test/render</pre><p>Now run npm run render-server and open localhost:8080. You’ll see the message you wrote in index.js. Congratulations, you made a Webpack server! But you’re not rendering any components from the src directory…not yet.</p><h3>Mirrored Directory Structure</h3><p>You’re going to mirror your src directory into your test/render directory. Start with src/App.js by creating a test/render/App.test.js</p><pre>import React, { Component } from &#39;react&#39;;</pre><pre>import App from &#39;../../src/App.js&#39;;</pre><pre>class AppTest extends Component {</pre><pre>  render() {return (&lt;App/&gt;);}</pre><pre>}</pre><pre>export default AppTest;</pre><p>Similarly, you can mirror src/logo/index.js by creating a test/render/logo/index.test.js</p><pre>import React, { Component } from &#39;react&#39;;</pre><pre>import Logo from &#39;../../src/logo/index.js&#39;;</pre><pre>class LogoTest extends Component {</pre><pre>  render() {return (&lt;Logo/&gt;);}</pre><pre>}</pre><pre>export default LogoTest;</pre><p>We need to read all the *.test.js files under test/render and turn them into URLs on our Webpack server. <a href="https://reacttraining.com/react-router/">React Router</a> makes URL management easy. Install <a href="https://www.npmjs.com/package/react-router-dom">react-router-dom</a>:</p><pre>npm install --save-dev react-router-dom</pre><p>Update test/render/index.js to use HashRouter</p><pre>import {HashRouter} from &quot;react-router-dom&quot;;</pre><pre>import Routes from &#39;./routes.js&#39;;</pre><pre>ReactDOM.render(</pre><pre>  &lt;HashRouter&gt;&lt;Routes/&gt;&lt;/HashRouter&gt;,</pre><pre>  document.getElementById(&#39;root&#39;));</pre><p>Routes.js is a JavaScript file we’re going to generate. It should match the directory structure under test/render. So under test/render create a build-routes.js</p><pre>const glob = require(&#39;glob&#39;);</pre><pre>const path = require(&#39;path&#39;);</pre><pre>const fs = require(&#39;fs&#39;);</pre><pre>const routes = [];</pre><pre>glob.sync(__dirname + &#39;/**/*.test.js&#39;).forEach(function(file) {</pre><pre>  const route = {</pre><pre>    path: path.relative(__dirname, path.dirname(file)),</pre><pre>    filename: path.basename(file)};</pre><pre>  routes.push(route);</pre><pre>});</pre><pre>let fileContent = &quot;import React from &#39;react&#39;;\n&quot;;</pre><pre>fileContent += &quot;import {Route} from &#39;react-router-dom&#39;;\n&quot;;</pre><pre>fileContent += &quot;class Routes extends React.Component {\nrender() {return (&lt;div&gt;\n&quot;;</pre><pre>routes.forEach((route) =&gt; {</pre><pre>  const importPath = path.join(route.path, route.filename);</pre><pre>  fileContent += &quot;&lt;Route exact path=&#39;/&quot;+route.path+&quot;&#39; component={require(&#39;./&quot;+importPath+&quot;&#39;).default} /&gt;\n&quot;;</pre><pre>});</pre><pre>fileContent += &quot;&lt;/div&gt;);}\n}\nexport default Routes\n&quot;;</pre><pre>fs.writeFile(&quot;test/render/routes.js&quot;, fileContent, function(err) {});</pre><p>This is the source code for generating routes.js by reading all the *.test.js files under test/render. The final touch is updating render-server in package.json</p><pre>node test/render/build-routes.js &amp;&amp;</pre><pre>webpack-dev-server</pre><pre>  --config test/render/webpack.config.js</pre><pre>  --content-base test/render</pre><p>This will build the routes.js that test/render/index.js needs to render, <strong>before</strong> using Webpack to bundle test/render/index.js. I recommend adding test/render/routes.js to your .gitignore file.</p><p>Now, run npm run render-server and open localhost:8080/#/ and localhost:8080/#/logo. The first URL should show the full src/App.js. The second URL should just show src/logo/index.js.</p><p>Congratulations, you’ve made a server for rendering individual components! Next time you want to render just one component, simply create a new *.test.js file under test/render.</p><p>*I renamed App.css to App.scss and installed <a href="https://github.com/sass/node-sass">node-sass</a>, because I love Sass!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=63cb135371ce" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>