<?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 Paul W. on Medium]]></title>
        <description><![CDATA[Stories by Paul W. on Medium]]></description>
        <link>https://medium.com/@pwo?source=rss-6d54e2cbfc21------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*5718FPv17Y2xuVfr0IxI8Q.jpeg</url>
            <title>Stories by Paul W. on Medium</title>
            <link>https://medium.com/@pwo?source=rss-6d54e2cbfc21------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 01:42:58 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@pwo/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 use parent-child pipelines on GitLab CI]]></title>
            <link>https://medium.com/ovrsea/how-to-use-parent-child-pipelines-on-gitlab-ci-658e946ebdb6?source=rss-6d54e2cbfc21------2</link>
            <guid isPermaLink="false">https://medium.com/p/658e946ebdb6</guid>
            <category><![CDATA[gitlab-ci]]></category>
            <category><![CDATA[children]]></category>
            <category><![CDATA[ci]]></category>
            <category><![CDATA[gitlab]]></category>
            <category><![CDATA[parents]]></category>
            <dc:creator><![CDATA[Paul W.]]></dc:creator>
            <pubDate>Mon, 14 Mar 2022 08:50:17 GMT</pubDate>
            <atom:updated>2022-03-14T08:50:17.029Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0W3WSAsIjgXxHfKA_DiRzw.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@jule_42">Juliane Liebermann</a> — <a href="https://unsplash.com/photos/O-RKu3Aqnsw">Unsplash</a></figcaption></figure><p><em>The full code can be found on </em><a href="https://gitlab.com/paul84/ci-example"><em>https://gitlab.com/paul84/ci-example</em></a></p><p>At <a href="https://www.ovrsea.com/">OVRSEA</a> we are using a monorepo with a lot of different projects. Quickly our CI became bigger and bigger and our <em>.gitlab-ci.yml</em> file started to look horrible with hundreds of lines and comments everywhere…</p><p>To separate the CI configuration of our different projects, we finally opted for the <strong>parent-child pipelines</strong>, a handy GitLab CI feature.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*t5j1fSvUHsgU7UAe-dPxWw.png" /><figcaption>The result on GitLab</figcaption></figure><h3>How does this work?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/326/1*MolSqiENbukvB12Rw5OAuQ.png" /><figcaption>Each project has its own CI configuration</figcaption></figure><p>Instead of having one big configuration file at the root of your monorepo, each project has its proper configuration file. These are the child pipelines.</p><p>At the root of your repository, you still have a configuration file. Its role is only to manage child pipelines and global jobs (for example, linting all your repo with ESLint). This is the parent pipeline.</p><p>Let’s dive into this by configuring a pipeline with 3 different packages…</p><h3>Configuring the parent pipeline</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c00fcf21dc261078adb45dcbf682065c/href">https://medium.com/media/c00fcf21dc261078adb45dcbf682065c/href</a></iframe><p>It is quite simple. In your parent configuration, you just have to create a job for each package with the keyword <strong><em>trigger. </em></strong>In the example above I did this for the 3 packages.</p><p>Under the trigger, you include your child configuration file and define the strategy to use. If you set no strategy the parent pipeline will succeed immediately after the child pipeline has started. To prevent this you can set a <strong><em>depend</em></strong> strategy to make the parent wait for his child&#39;s completion.</p><h3>The child pipelines</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ef7e5be401c7f1362ffd4a0ea8b1a857/href">https://medium.com/media/ef7e5be401c7f1362ffd4a0ea8b1a857/href</a></iframe><p>There is nothing special here. Each child pipeline is individual and completely autonomous, so you can define your jobs as you would normally do.</p><p>Here is a simple echo for package A. I did the same for the two others.</p><p><strong>It is mandatory to have a least one job in a child pipeline</strong>, otherwise you will get an error when running your pipeline.</p><h3>Conclusion</h3><p>The parent-child feature allows you to split your configuration into multiple pieces where each project owns its specific configuration and is completely decoupled from the others.</p><p>This is very handy when you start to have a very large repo with a lot of different projects.</p><h3>To go further</h3><p>The side-effect of this approach is that you can quickly have a lot of duplication of configuration through your different projects.</p><p>For example, the configuration of the cache for the modules, which can be shared among all the projects of the monorepo. We don’t want to duplicate this on each project.</p><p>To mitigate this issue, at OVRSEA, we made a shared configuration package that contains all the reusable configurations. We import it each time we need it and this way we can separate our configurations while minimizing duplication.</p><p>You can find more information on how to import shared configuration in the GitLab CI documentation: <a href="https://docs.gitlab.com/ee/ci/yaml/includes.html">https://docs.gitlab.com/ee/ci/yaml/includes.html</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=658e946ebdb6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ovrsea/how-to-use-parent-child-pipelines-on-gitlab-ci-658e946ebdb6">How to use parent-child pipelines on GitLab CI</a> was originally published in <a href="https://medium.com/ovrsea">OVRSEA</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to automatically rebase all your Merge Requests on GitLab when pushing on master]]></title>
            <link>https://medium.com/ovrsea/how-to-automatically-rebase-all-your-merge-requests-on-gitlab-when-pushing-on-master-9b7c5119ac5f?source=rss-6d54e2cbfc21------2</link>
            <guid isPermaLink="false">https://medium.com/p/9b7c5119ac5f</guid>
            <category><![CDATA[gitlab]]></category>
            <category><![CDATA[gitlab-ci]]></category>
            <category><![CDATA[merge-request]]></category>
            <category><![CDATA[jq]]></category>
            <category><![CDATA[rebase]]></category>
            <dc:creator><![CDATA[Paul W.]]></dc:creator>
            <pubDate>Thu, 24 Sep 2020 12:17:40 GMT</pubDate>
            <atom:updated>2020-10-01T10:18:23.722Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*5O2fuRLQwT4qpK5O.png" /></figure><p>At OVRSEA we are used to merge our Merge Requests with a fast-forward policy. However, on GitLab, when you merge an MR on master, you must manually rebase all the other MRs afterwards. It’s time consuming and as developers we have other more important things to do.</p><p>To overcome this problem we made a very simple script to automate the rebase process of all opened MRs when a push is done on the main branch.</p><p>It runs on a GitLab CI pipeline alongside of our other pipelines.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a4645cd9d2c1cd8063a213fad5ee6c3a/href">https://medium.com/media/a4645cd9d2c1cd8063a213fad5ee6c3a/href</a></iframe><p>We use curl for fetching all opened MRs via the GitLab API first, then we parse the returned JSON for iid variable with jq.</p><p>With each MR’s iid we can send a rebase request through GitLab API.</p><p>You have to define $GITLAB_PERSONAL_TOKEN and $PROJECT_ID environment variables, in our case we run this script on a GitLab CI pipeline so they are filled in the GitLab CI settings.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b22debd618f471ff52a8cb75abb4881c/href">https://medium.com/media/b22debd618f471ff52a8cb75abb4881c/href</a></iframe><p>For the CI part we made a very simple pipeline :</p><ul><li>We use node:14-alpine image wich includes a lightweight version of Linux.</li><li>A rule is added for running this job only on master branch.</li><li>jq and curl packages are necessary for our script, we install them with the apk command</li><li>And finally, our script is run.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9b7c5119ac5f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ovrsea/how-to-automatically-rebase-all-your-merge-requests-on-gitlab-when-pushing-on-master-9b7c5119ac5f">How to automatically rebase all your Merge Requests on GitLab when pushing on master</a> was originally published in <a href="https://medium.com/ovrsea">OVRSEA</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to manually install and configure GitLab Runners on GKE (Google Kubernetes Engine)]]></title>
            <link>https://medium.com/ovrsea/how-to-manually-install-and-configure-gitlab-runners-on-gke-google-kubernetes-engine-b81c6c945313?source=rss-6d54e2cbfc21------2</link>
            <guid isPermaLink="false">https://medium.com/p/b81c6c945313</guid>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[gitlab]]></category>
            <category><![CDATA[google-cloud-platform]]></category>
            <category><![CDATA[gitlab-runner]]></category>
            <category><![CDATA[gke]]></category>
            <dc:creator><![CDATA[Paul W.]]></dc:creator>
            <pubDate>Tue, 21 May 2019 15:08:26 GMT</pubDate>
            <atom:updated>2019-05-22T06:57:45.295Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*xiO3sJmUZ2dqCOgjNI92kg.png" /></figure><p>When you use GitLab CI/CD, you have access to GitLab Shared Runners for running your jobs. You have absolutely no configuration to do, it’s ready to use.</p><p>But Shared Runners have some disadvantages. They are limited in build time (2000 minutes/month on Free Plan), you can’t configure them and they can be pretty slow…</p><p>That’s why GitLab lets us host our own runners, they can be hosted wherever you want, locally on your machine, on a server, in a Docker container…</p><p>In this tutorial, we will see how to host our runners on Kubernetes and particularly on Google Kubernetes Engine. At Ovrsea, we chose Kubernetes because of its capability to autoscale, when we have a lot of jobs running we want to increase the amount of runners available automatically and in the other hand, when we have no jobs running, we don’t wan’t to pay for it.</p><p>There is an option to automatically create a Kubernetes cluster on GKE (Google Kubernetes Engine) on the GitLab interface but this is not what this article is about because it’s somewhat limited and it can be tricky to configure afterwards.</p><h3>Cluster Creation</h3><p>First, you have to create a cluster on GKE.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*s6jbXEIbHuHZWgF0j7cdyA.png" /><figcaption>Cluster creation interface on Google Cloud Platform</figcaption></figure><p>In the Node Pools options, you have to choose your machine type, I personally chose n1-standard-2 (2 vCPUs and 7.5GB memory) and only 1 node.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/458/1*KkyqjWkte6QejC3l3Z5M3g.png" /><figcaption>Number of nodes and machine type</figcaption></figure><p>Click on “More options” to set “Autoscaling” on and choose minimum and maximum nodes you want. This allows us to scale our nodes when there are too many pods in one node (one job in our CI/CD is one pod in a Kubernetes node).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/473/1*5ku47oH7nRrZAmnwrizP0g.png" /><figcaption>I set 1 node minimum and 10 nodes maximum for autoscaling</figcaption></figure><p>If you are not comfortable with Kubernetes autoscaling, I suggest you to read this article <a href="https://dzone.com/articles/kubernetes-autoscaling-explained">https://dzone.com/articles/kubernetes-autoscaling-explained</a>.</p><p>The rest of configuration is up to you, I personally let the default settings.</p><h3>Helm and Tiller Installation</h3><p>We will install Helm which is a package manager for Kubernetes, and Tiller which is the server who will communicate between Helm and Kubernetes.</p><p>First, get a connection to our newly created cluster by clicking “Connect” in the clusters view.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/858/1*dxJsbG_UTZ2jHXDcL90G7w.png" /><figcaption>Click “Connect”</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/731/1*xRyed8BNpB3-GVsi8pWC-Q.png" /><figcaption>And click “Run in Cloud Shell”</figcaption></figure><p>You should now have a terminal view in the bottom of the page with a prefilled line, just hit “Enter”.</p><p>Download Helm:</p><pre>wget <a href="https://storage.googleapis.com/kubernetes-helm/helm-v2.6.2-linux-amd64.tar.gz">https://storage.googleapis.com/kubernetes-helm/helm-v2.6.2-linux-amd64.tar.gz</a></pre><p>Extract it:</p><pre>$ tar zxfv helm-v2.6.2-linux-amd64.tar.gz</pre><pre>$ cp linux-amd64/helm .</pre><p>Set authorizations for Tiller:</p><pre>$ kubectl create serviceaccount --namespace kube-system tiller</pre><pre>$ kubectl create clusterrolebinding tiller-cluster-rule --clusterrole=cluster-admin --serviceaccount=kube-system:tiller</pre><pre>$ kubectl patch deploy --namespace kube-system tiller-deploy -p &#39;{&quot;spec&quot;:{&quot;template&quot;:{&quot;spec&quot;:{&quot;serviceAccount&quot;:&quot;tiller&quot;}}}}&#39;</pre><p>Install Tiller:</p><pre>$ ./helm repo add gitlab <a href="https://charts.gitlab.io/">https://charts.gitlab.io</a></pre><pre>$ ./helm init</pre><h3>Configure your values.yml</h3><p>Now, create a values.yml file, it will contain all the configuration for the GitLab Runners.</p><pre>$ touch values.yml</pre><pre>$ nano values.yml</pre><p>You can see at <a href="https://gitlab.com/charts/gitlab-runner/blob/master/values.yaml">https://gitlab.com/charts/gitlab-runner/blob/master/values.yaml</a> all available variables but I will cover the most important ones.</p><pre># values.yml</pre><pre>gitlabUrl: <a href="https://gitlab.com/">https://gitlab.com/</a><br>runnerRegistrationToken: &lt;your-GitLab-Runners-Registration-Token&gt;<br>concurrent: 20<br>rbac:<br>  create: true<br>  clusterWideAccess: false<br>runners:<br>  image: ubuntu:18.04<br>  privileged: false<br>  cache:<br>    cacheType: s3<br>    cacheShared: true<br>    s3ServerAddress: s3.amazonaws.com<br>    s3BucketName: &lt;your-S3-bucket&gt;<br>    s3BucketLocation: us-central-1<br>    s3CacheInsecure: false<br>    secretName: s3access<br>  builds:<br>    memoryRequests: 4000Mi</pre><p><strong>gitlabUrl:</strong> Don’t change this, it always be <a href="https://gitlab.com/"><em>https://gitlab.com/</em></a></p><p><strong>runnerRegistrationToken: </strong>You can get this token in your CI/CD settings on GitLab, in the Runners section.</p><p><strong>concurrent: </strong>The number of concurrent jobs you can run at the same time.</p><p><strong>rbac:</strong> For RBAC support, don’t change it.</p><p><strong>runners:</strong> Configuration for deploying the runners, see below.</p><p><strong>image:</strong> Default container image.</p><p><strong>privileged:</strong> For executing docker commands in the containers, don’t change it as we don’t need it.</p><p><strong>cache:</strong> As jobs will run in different pods and nodes, if you use cache for saving dependencies between jobs, you have to save it somewhere else. You can use either GCS on Google Cloud Platform or S3 on AWS. I personally use S3. You will need a secret file filled with your credentials, you can create it with the following commands:</p><pre>For S3:<br>$ kubectl -n &lt;your-cluster-name&gt; \<br>  create secret generic s3access \<br>  --from-literal=accesskey=&quot;YourAccessKey&quot; \<br>  --from-literal=secretkey=&quot;YourSecretKey&quot;</pre><pre>For GCS:<br>$ kubectl -n &lt;your-cluster-name&gt; \<br>  create secret generic gcsaccess \<br>  --from-literal=gcs-access-id=&quot;YourAccessID&quot; \<br>  --from-literal=gcs-private-key=&quot;YourPrivateKey&quot;</pre><p><strong>builds:</strong> Configuration for the builds, you can specify here how much memory and CPU your jobs need. If a node is out of memory or CPU usage, the cluster will create another node for you, thanks to autoscaling.</p><p>You can now apply this configuration to your cluster:</p><pre>$ ./helm install --namespace &lt;your-cluster-name&gt; --name gitlab-runner -f values.yml gitlab/gitlab-runner</pre><p>Tadaa ! Your runner is now ready to use !</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/840/1*WSvVxUzKPxqdJLPEtnHV4Q.png" /><figcaption>Success message</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oUa7MaYh_oE3gS_ksmaXpg.png" /><figcaption>Now, you can see your GitLab Runner in the Workloads menu</figcaption></figure><p>If you need to change the current configuration, you just have to edit your values.yml and upgrade your runner with the following command:</p><pre>$ ./helm upgrade --namespace &lt;your-cluster-name&gt; -f values.yml gitlab-runner gitlab/gitlab-runner</pre><h3>Link GitLab with your runner</h3><p>GitLab should now be linked to your runner if you specified the good registration token in your values.yml file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/431/1*7aJW3rLjrcbDSsPpTHN3tQ.png" /><figcaption>Runners in GitLab’s CI/CD settings</figcaption></figure><p>You can now trigger your jobs and see pods in action in the GKE Workloads menu.</p><p>That’s it, you should have a working GitLab Runners environment by now. If you are facing some issues, feel free to ask me in comments.</p><p>For further reading on Kubernetes and GitLab Runners:</p><ul><li><a href="https://kubernetes.io/docs/concepts/">Concepts</a></li><li><a href="https://dzone.com/articles/kubernetes-autoscaling-explained">Kubernetes Autoscaling Explained - DZone Cloud</a></li><li><a href="https://docs.gitlab.com/runner/executors/kubernetes.html">Kubernetes executor | GitLab</a></li><li><a href="https://docs.gitlab.com/runner/install/kubernetes.html">GitLab Runner Helm Chart</a></li><li><a href="https://gitlab.com/charts/gitlab-runner/blob/master/values.yaml">values.yaml · master · charts / gitlab-runner</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b81c6c945313" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ovrsea/how-to-manually-install-and-configure-gitlab-runners-on-gke-google-kubernetes-engine-b81c6c945313">How to manually install and configure GitLab Runners on GKE (Google Kubernetes Engine)</a> was originally published in <a href="https://medium.com/ovrsea">OVRSEA</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Common Mistakes in React and How to Avoid Them]]></title>
            <link>https://medium.com/ovrsea/common-mistakes-in-react-and-how-to-avoid-them-e882b5437748?source=rss-6d54e2cbfc21------2</link>
            <guid isPermaLink="false">https://medium.com/p/e882b5437748</guid>
            <category><![CDATA[development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[developer]]></category>
            <dc:creator><![CDATA[Paul W.]]></dc:creator>
            <pubDate>Thu, 18 Oct 2018 14:08:19 GMT</pubDate>
            <atom:updated>2018-10-18T14:08:19.312Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rsMQVckkVT8uwVXk7tFFYA.jpeg" /></figure><p>At <a href="https://ovrsea.com/">Ovrsea</a>, when new trainees arrive, they struggle with small but time consuming mistakes when they start React. That’s why I’ll cover here those most common mistakes in order to avoid you possible future headaches.</p><p><strong>TL;DR</strong>: You have…</p><ul><li>…a render issue, for example an infinite loop, <strong>you may have forgotten arrows in a callback (point 4).</strong></li><li>…a function that is triggered whereas it shouldn’t, <strong>check also your callbacks (point 4).</strong></li><li>…a state variable that doesn’t change properly<strong>, your State variables may be mutated (point 2).</strong></li><li>…a list of components that doesn’t update well, <strong>you probably did not set your keys (point 1).</strong></li><li>… a component that doesn’t update after state changes, <strong>maybe you didn’t know that setState is async ? (point 3).</strong></li></ul><h3>1. You forgot ‘key’ on a Components list</h3><p>When you have an array of values and you want to render a component for each of them, you will probably do something like this:</p><pre>const arr = [&#39;yellow&#39;, &#39;red&#39;, &#39;green&#39;];</pre><pre>render() {<br>  <strong>return </strong>(<br>    &lt;ul&gt;<br>      {arr.map(color =&gt;<br>        &lt;li&gt;{color}&lt;/li&gt;)}<br>    &lt;/ul&gt;<br>  );<br>}</pre><p>This will work fine if the array never changes but if you have to make some modifications in it, for example delete or modify a value, you will probably have some render issues.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/288/1*ub1nYQbY7ybVT2Oyx74HiQ.gif" /><figcaption>Why green still here ? 🤔</figcaption></figure><p>This problem can easily be solved by adding a key to the component inside the loop. This will help React to watch what element has changed.</p><pre>&lt;p key={color}&gt;<br>  {color}<br>&lt;/p&gt;</pre><p>But be careful, <strong>keys must be unique</strong> !</p><p>You don’t want to use your array index, because it will be reassign if the array order or size changes. In this example we can only use color as key but if the same color is containing twice in the array this will cause unexpected behavior…</p><p>If you map an array of objects with IDs or any unique property then you can use it, else you may still use index but only at last resort.</p><h3>2. You mutated a State variable</h3><p>Imagine that we have an array of color in our state:</p><pre>state = {<br>  arr: [&#39;red&#39;, &#39;yellow&#39;],<br>};</pre><p>We want to change the color of the first element into green, so we write a function that modify state:</p><pre>changeColorToGreen = () =&gt; {<br>  <strong>const </strong>arr = <strong>this</strong>.state.arr;<br><br>  arr[0] = &#39;green&#39;;<br><br>  <strong>this</strong>.setState({ arr });<br>};</pre><p>This will correctly change our state but we may have some unexpected behavior by doing it this way. React is looking for object reference changes in memory when it decides to rerender or not, in that case the object is still the same in memory and only it’s content has changed. So components using the array as props may not rerender properly whenever colors change.</p><pre>changeColorToGreen = () =&gt; {<br>  <strong>const </strong>arr = <strong>this</strong>.state.arr.slice();<br>  // .slice() return a shallow copy of the array</pre><pre>  // We can also use ES6 spread operator:<br>  // <strong>const </strong>arr = [...this.state.arr]</pre><pre>  arr[0] = &#39;green&#39;;<br><br>  <strong>this</strong>.setState({ arr });<br>};</pre><p>This time our array is well reassigned and its reference in memory has changed, we keep our state immutable.</p><h3>3. You forgot that setState is asynchronous</h3><p>When you have to change a value in state, you have to use setState function as we saw in previous examples. But if we try to use setState many times in the same function, we can have some unexpected behavior.</p><pre>state = {<br>  counter: 1,<br>};<br><br>incrementCounter = () =&gt; {<br>  <strong>this</strong>.setState({ counter: <strong>this</strong>.state.counter + 1 });<br>  <strong>this</strong>.setState({ counter: <strong>this</strong>.state.counter + 1 });<br>  <strong>this</strong>.setState({ counter: <strong>this</strong>.state.counter + 1 });<br>};</pre><p>In this example, we expect counter to be 4. But as setState is async, it will have no enough time to change the value when the next call is done. So, at the third call, this.state.counter is still at 1 and the result will be only 2.</p><p>Each call to incrementCounter will increment counter by 1 instead of 3.</p><p><em>Try it out on </em><a href="https://codepen.io/pwortham/pen/ePVVKN?editors=0010"><em>CodePen</em></a><em>.</em></p><h3>4. You forgot arrows in a callback</h3><p>When beginners have to deal with arrow functions for the first times, they often misunderstanding their syntax. See belove the most common mistake they usually make:</p><pre>state = {<br>  counter: 1,<br>};<br><br>incrementCounter = (amount) =&gt; {<br>  <strong>this</strong>.setState({ counter: <strong>this</strong>.state.counter + amount });<br>};<br><br>render() {<br>  <strong>return </strong>(<br>    &lt;div&gt;<br>      &lt;p&gt;{<strong>this</strong>.state.counter}&lt;/p&gt;<br>      &lt;button onClick={<strong>this</strong>.incrementCounter(5)} /&gt;<br>    &lt;/div&gt;<br>  );<br>}</pre><p>In this example, we have a button that call incrementCounter function when onClick is triggered. Problem is that it’s the return of incrementCounter function that is assign to onClick, not the function itself. So incrementCounter will be triggered as soon as the component will render.</p><p>The correct way to assign a function at events is to use arrow function:</p><pre>onClick={<strong>() =&gt; this</strong>.incrementCounter(5)}</pre><p>When you don’t need parameter in your function or parameters are given directly by the event, you can simply write it like this:</p><pre>onClick={<strong>this</strong>.myFunction}</pre><pre>// Is the same as onClick={(e) <strong>=&gt; this</strong>.myFunction(e)}</pre><h3>For further reading</h3><ul><li>React Lists and Keys: <a href="https://reactjs.org/docs/lists-and-keys.html"><em>https://reactjs.org/docs/lists-and-keys.html</em></a></li><li>Why Immutability is important: <a href="https://reactjs.org/tutorial/tutorial.html#why-immutability-is-important"><em>https://reactjs.org/tutorial/tutorial.html#why-immutability-is-important</em></a></li><li>Why setState asynchronous: <a href="https://github.com/facebook/react/issues/11527#issuecomment-360199710"><em>https://github.com/facebook/react/issues/11527#issuecomment-360199710</em></a></li><li>Passing functions to components: <a href="https://reactjs.org/docs/faq-functions.html"><em>https://reactjs.org/docs/faq-functions.html</em></a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e882b5437748" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ovrsea/common-mistakes-in-react-and-how-to-avoid-them-e882b5437748">Common Mistakes in React and How to Avoid Them</a> was originally published in <a href="https://medium.com/ovrsea">OVRSEA</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Headless Browser Testing with Jest and Puppeteer]]></title>
            <link>https://medium.com/ovrsea/headless-browser-testing-with-jest-and-puppeteer-2cf7d04f4af5?source=rss-6d54e2cbfc21------2</link>
            <guid isPermaLink="false">https://medium.com/p/2cf7d04f4af5</guid>
            <category><![CDATA[tech]]></category>
            <category><![CDATA[jest]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[puppeteer]]></category>
            <category><![CDATA[headless]]></category>
            <dc:creator><![CDATA[Paul W.]]></dc:creator>
            <pubDate>Mon, 20 Nov 2017 15:04:44 GMT</pubDate>
            <atom:updated>2018-05-24T13:48:31.129Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/851/1*pN3IJVbx2iv97xJ6sB9jJA.png" /></figure><p>It can be tricky to do functional tests on web applications. There are a lot of frameworks on the market to do that (Selenium…) but it generally comes with a lot of configuration and unexpected behavior.</p><p>In this article, I will show you how to test very easily and without any configuration your application with Jest and Puppeteer, two strong new comers in the jungle of web frameworks.</p><p>For this article, we will use React Create App to start quickly with a new and functional application. You can do the same by installing it with the following command:</p><pre>npm install -g create-react-app</pre><p>Then, create the app:</p><pre>create-react-app headless-testing-app<br>cd headless-testing-app/</pre><p>You can now access our app by typing this command in the terminal:</p><pre>npm start</pre><h3>Jest &amp; Puppeteer</h3><p>In one hand I have <a href="https://github.com/GoogleChrome/puppeteer">Puppeteer</a>, a Node Library that can control a Headless Chrome browser by writing actions in Javascript files. In the other hand, I have <a href="https://facebook.github.io/jest/">Jest</a>, a “Delightful JavaScript Testing” framework with zero configuration. It will launch our tests written with Puppeteer and show us which tests succeed or fail.</p><p>Install Jest with the following command:</p><pre>npm install --save-dev jest jest-cli</pre><p>And Puppeteer:</p><pre>npm install --save-dev puppeteer</pre><h3>Our first test</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/625/1*fZmGQvmyxA-kijXpcs2j3A.png" /><figcaption>What our app looks like</figcaption></figure><p>For this first test, we will start with an easy one. We will check if the application is really online by looking if “Welcome to React” is present.</p><p>We can see in the inspector that this title is contained in a <em>h1</em> tag with a class named “App-title”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/567/1*6VfFRTOD8MearNDHJRnF8g.png" /><figcaption>We are looking for the text in the App-title class</figcaption></figure><h3>Now, let’s start !</h3><p>First, we create a <em>__tests__</em> folder and a file named <em>first_test.js</em> in it. Your files should now look like that:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/231/1*T6T-Yb_qSk0XsLn7fKhbCQ.png" /></figure><p>Delete the <em>App.test.js</em> is the <em>src </em>folder. Jest is designed to search all files containing the word “test” in their name, and we don’t want it to launch this one.</p><p>Next, let’s write our first test:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/552/1*BK8l8MaHZaNJqOITCgMMCg.png" /><figcaption>first_test.js</figcaption></figure><h4>Let’s explain this script…</h4><p>We start by importing Puppeteer, then we write the test function. This function is declared by Jest and tells him to start a test.</p><p>We declare a browser and a page variable that tell to Puppeteer to launch the browser and get a new page. The “headless: false” option is indicated so that we can see the browser while testing.</p><p>After that, we tell to the page to navigate to our application hosted at <a href="http://localhost:3000/">http://localhost:3000/</a> and wait for the selector (remember, the <em>h1</em> tag with the class named “App-title”) to appear.</p><p>Once the selector is display, we capture the content of the selector in a “html” variable and tell to Jest we want the content to be “Welcome to React”.</p><p>Finally, the browser is closed and the test ends.</p><h3>Launch the test</h3><p>We can now start the test ! Just write this command in your terminal:</p><pre>jest</pre><p>And the magic begins…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/531/1*QnqqCVGRXxxGsGGZODTZcw.png" /><figcaption>Yay!</figcaption></figure><p>You can also edit your <em>package.json</em> as follows (add a <em>test </em>script):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/307/1*dMp9DNkQcdWpbajL06xJcQ.png" /><figcaption>package.json</figcaption></figure><p>and launch the tests in your terminal:</p><pre>npm test</pre><h3>Conclusion</h3><p>We saw how easy it is to write headless tests for any web application with zero configuration and two simple frameworks.</p><p>This example was very simple but you can do very advanced tests with this tools. E. g., you can simulate a user navigating on your website and check that everything corresponds to the expected behavior. We will see how to implement such advanced tests with puppeteer in another incoming post.</p><p>In between, I strongly encourage you to look at their very well detailed documentation:</p><p>Jest: <a href="https://facebook.github.io/jest/">https://facebook.github.io/jest/</a></p><p>Puppeteer: <a href="https://github.com/GoogleChrome/puppeteer">https://github.com/GoogleChrome/puppeteer</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2cf7d04f4af5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ovrsea/headless-browser-testing-with-jest-and-puppeteer-2cf7d04f4af5">Headless Browser Testing with Jest and Puppeteer</a> was originally published in <a href="https://medium.com/ovrsea">OVRSEA</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>