<?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 Andy Greco on Medium]]></title>
        <description><![CDATA[Stories by Andy Greco on Medium]]></description>
        <link>https://medium.com/@andygreco?source=rss-943c74193427------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*8ICt4Wulp6sZSzbC0ywJlg.jpeg</url>
            <title>Stories by Andy Greco on Medium</title>
            <link>https://medium.com/@andygreco?source=rss-943c74193427------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 11:13:49 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@andygreco/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 implement  JestJS on existing website]]></title>
            <link>https://andygreco.medium.com/how-to-implement-jestjs-on-existing-website-ac6f3cd8a430?source=rss-943c74193427------2</link>
            <guid isPermaLink="false">https://medium.com/p/ac6f3cd8a430</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[jest]]></category>
            <category><![CDATA[testing-tools]]></category>
            <category><![CDATA[tdd]]></category>
            <dc:creator><![CDATA[Andy Greco]]></dc:creator>
            <pubDate>Tue, 29 Dec 2020 11:52:08 GMT</pubDate>
            <atom:updated>2021-01-14T17:58:45.313Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to implement JestJS on existing website</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gYd6rEdvpLG_ih8JXdEMFg@2x.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@scottgummerson26">Scott Gummerson</a> on <a href="https://unsplash.com/photos/WwAUvVTb2Cw">Unsplash</a></figcaption></figure><p>I am a big fan of the TDD approach for many reasons and I want to use it as much as possible.</p><p>Recently, I have worked on a task to focus on adding JestJS to a project that uses JS Object Method, in short a syntax like this:</p><pre>let o = {<br>    property: function (parameters) {}<br>}</pre><p>Unfortunately, out of the box, the standard Jest installation does not function when the code is written in JS Object Method.</p><p>It took me a while to find a way to be able to implement it because of the lack of information I have found.</p><p>Therefore I would like to share my experience in this article. Hopefully it will help someone like me, or someone else will show me a better way to cover this scenario.</p><p>In order to make the explanation as clear as possible we are going to test a simple function with input and output. We don’t want an external library like jQuery or DOM testing.</p><p><strong>Prerequisite<br></strong>For this example I am going to use JestJS, and NodeJS, so you need to have a rough idea of what they are, and how to install and use them.</p><p>Ok, let’s start then.</p><p>This is the scenario, imagine you want to add a JestJS as a unit test on your WordPress theme and this theme uses JavaScript Object Method syntax as described before.</p><p>This is the JS files directory</p><pre>js/<br>|—- main.js<br>|—- validation.js<br>... other JS files</pre><p>The multiple JS files will all be merged into one file by some task runner like Webpack, Gulp, Rollup, etc.</p><p>And you would test <strong>checkEmail</strong> function into <strong>validations.js</strong></p><pre>ACC.validation = {<br>    checkEmail: function(email) {<br>        var isValid = false;<br>        var pattern = new RegExp(/^[^@]+@[^@]+\.[^@]+$/i);<br>        isValid = pattern.test(email) ? true : false;<br>        return isValid;<br>    }<br>}</pre><p>As I mentioned before, if you’re following the Jest official examples and tutorials you will discover that you are not able to test this function because the syntax we have is not covered by default from Jest tasks.</p><p>I am going to give a quick overview of my solution, but don’t worry you can find all files in a <a href="https://github.com/andygi/jestjs-jsom">related repository</a>, so you play with and check the code by yourself.</p><p><strong>First</strong>, we need to <strong>install JestJS</strong> in the project, and add an empty directory called <strong>__tests__ </strong>as<strong> </strong>tests files container.</p><pre>project/<br>|—- js/<br>|   |—- __tests__/<br>|   |—- main.js<br>|   `—- validation.js<br>|—- jest.config.json<br>`—- package.json</pre><p>Now we need to work on these topics:</p><ul><li>ACC <strong>object</strong> is <strong>not declared</strong> and this generates and error</li><li>It is <strong>not possible</strong> to <strong>test </strong>JS Object Method syntax with the standard Jest process</li></ul><p>Let’s work on the first one. What we need to do is declare the object like this into validation.js:</p><pre>ACC = {};</pre><p>This would be an easy solution, however it creates a number of complications which I will not go into in this article.</p><p>Because of this, we need to create a temporary file with all declarations we need to run the test successfully, and the file we want to test.</p><p>That means making a file with the declaration called <strong>_test.config.js</strong>, with the JS file we want to test (validation.js in this case), and the new JS dir structure which becomes:</p><pre>js/<br>|—- _test.config.js<br>|—- main.js<br>`—- validation.js</pre><p>The _test.config.js contain the ACC declaration</p><pre>ACC = {};</pre><p>As I said before, this is not enough; in fact, we need to merge this file with the file we want to test dynamically.</p><p>To do that we use NodeJS as a framework and then a file with all the code we need.</p><p>In particular, I use <a href="https://www.npmjs.com/package/merge-files"><strong>merge-files</strong></a> library.</p><p>This is what the structure looks like so far</p><pre>project/<br>|—- js/<br>|   |—- __tests__/<br>|   |—- _test.config.js<br>|   |—- main.js<br>|   `—- validation.js<br>|—- jest.config.json<br>|—- package.json<br>`—- setup_test.js</pre><p>And this is <strong>setup_test.js</strong> in detail:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3b9a4a180f79162e58dbcb54d490848b/href">https://medium.com/media/3b9a4a180f79162e58dbcb54d490848b/href</a></iframe><p>As a result, there are two validation.js files, the file in _temp_test is the testable file made with _test.config.js + validation.js.</p><pre>project/<br>|—- js/<br>|   |—- __tests__/<br>|   |—- _temp_test/<br>|   |     `—- validation.js<br>|   |—- _test.config.js<br>|   |—- main.js<br>|   `—- validation.js<br>|—- jest.config.json<br>|—- package.json<br>`—- setup_test.js</pre><p>In order to run this function we need to add the command in package.json script section like this:</p><pre>&quot;scripts&quot;: {<br>    &quot;test&quot;: &quot;node setup_test.js &amp; jest&quot;<br>}</pre><p>This is the final project’s tree</p><pre>project/<br>|—- js/<br>|   |—- __tests__/<br>|   |     `—- validation.test.js<br>|   |—- _temp_test/<br>|   |     `—- validation.js<br>|   |—- _test.config.js<br>|   |—- main.js<br>|   `—- validation.js<br>|—- jest.config.json<br>|—- package.json<br>`—- setup_test.js</pre><p>Now we have a function that is able to create a testable JS file. What we need is to set JestJS up to test it.</p><p>To do that we can use a library called <a href="https://www.npmjs.com/package/rewire"><strong>rewire</strong></a>, in that way you can declare the method you want to test in the object.</p><p>Now we can write the test.</p><p>In this example I am going to test the validation email function, and we are going to declare which file we want to test (js/_temp_test/validation.js), and which function we want tested in the validation.js file (checkEmail):</p><pre>var fileToTest = &quot;validation&quot;;<br>jest.dontMock(&quot;../&quot; + fileToTest);<br>var rewire = require(&quot;rewire&quot;);<br>var component = rewire(&quot;../_temp_test/&quot; + fileToTest);<br>var checkEmail = component.__get__(&quot;ACC.validation.checkEmail&quot;);</pre><p>And this is the final test file with two tests: one in case you test a wrong email, and another in case the email is formally correct</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/42a3da1605d9cda68660c3c8e48b9462/href">https://medium.com/media/42a3da1605d9cda68660c3c8e48b9462/href</a></iframe><p>In order to run the test we use the command</p><pre>npm test</pre><p>and now Jest works as expected.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/519/1*TfHQBK_NjjT_9XFpDEkS5Q@2x.jpeg" /></figure><p>Cheers</p><p>Andy Greco</p><p><strong>References</strong></p><ul><li><a href="https://jestjs.io/">Jest</a></li><li><a href="https://www.npmjs.com/package/rewire">Rewire</a></li><li><a href="https://www.npmjs.com/package/merge-files">Merge-files</a></li></ul><p><a href="https://github.com/andygi/jestjs-jsom">andygi/jestjs-jsom</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ac6f3cd8a430" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What Nucleus Style Guide is, and How to set it up]]></title>
            <link>https://andygreco.medium.com/what-nucleus-style-guide-is-and-how-to-set-up-it-16db9d3e2cf8?source=rss-943c74193427------2</link>
            <guid isPermaLink="false">https://medium.com/p/16db9d3e2cf8</guid>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[style-guides]]></category>
            <category><![CDATA[nucleus]]></category>
            <dc:creator><![CDATA[Andy Greco]]></dc:creator>
            <pubDate>Fri, 20 Jul 2018 17:51:08 GMT</pubDate>
            <atom:updated>2020-11-29T10:38:02.501Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BtKXKbj15HfpSCm7XXMVJg.jpeg" /></figure><p><strong>Why we need Style Guide</strong></p><p>As web developer every time you start to refactor or create a new part of the web app, you need information: which code color use, which HTML chunk you have to use, if there are some mixin functions ready to do and in general all you need to know to be ready to start.</p><p>This is the time where Continuing Integration Web apps are routine, developed by developers teams. Sometimes developed by a huge team with a lot of developer changing.</p><p>In this scenario, the Style Guide Documentation is an important tool to maintain code and design consistently.</p><p>The Style Guide is a Continuing Integration Project itself, constantly updated to be trusted, and because of that, we need a tool easy to maintain with less effort as possible.</p><p><strong>Nucleus</strong></p><p>I want to share my experience on this, in particular, I have started to use Nucleus to create a Style Guide tool.</p><p>I have found its logic approach coherent and it helps me to make an order for the project. It’d be a valid help to create a reliable Style Guide with the capability to update it easily.</p><p><a href="https://holidaypirates.github.io/nucleus/demo/index.html">Here you can find a demo</a></p><p>I have found this project very useful and well done with a consistent structure</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/861/1*xHWVwiXJqkLd_u76phfbDw.png" /><figcaption>Ideal Nucleus Structure</figcaption></figure><p>This is my experience on how set up it.</p><p><strong>How to set up it</strong></p><p>First of all, as prerequisite, you need able to use the terminal, in order to install the libraries we need it.</p><p>I use Mac, and will be the same for Linux, regarding Win I know there are some good terminal emulator or simply google it to find the correct procedure (sorry for that in advance).</p><p>Note: Nucleus working only with Sass files as css precompiled file, and we use that file to write in the style-guide annotations.</p><p><strong>Prerequisite</strong></p><p>I assume there is a project with:</p><ul><li>NodeJs</li><li>Grunt/Gulp as build system</li><li>Sass files</li></ul><p>Then, by terminal, go to the same directory of Grunt/Gulp and install the library:</p><pre>npm install nucleus-styleguide --save-dev</pre><p>Now the tricky coming, on this point I didn’t find a documentation, basically you need to add a nucleus config file called: <strong>config.nucleus.json</strong></p><p>Because of that I have created a Dummy Project that you can find here: <a href="https://github.com/andygi/styleguide-nucleus">https://github.com/andygi/styleguide-nucleus</a></p><p>This is an example with Bootstrap/Jquery-UI Css/Js as external libraries</p><p><a href="https://github.com/andygi/styleguide-nucleus/blob/master/config.nucleus.json">andygi/styleguide-nucleus</a></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b66f565b4a74c7934c60ebb846c69db0/href">https://medium.com/media/b66f565b4a74c7934c60ebb846c69db0/href</a></iframe><p>After checkout the repo you can read the README.md file for more information how to install it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/250/1*chjCPILnbLmWgdwDMvidHg.png" /></figure><p>You can find the Styleguide html files into styleguide directory and openindex.html file.</p><p>From this project you can find some code annotation as example, like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WVgjFaYI5ljaEuqR3W57Pw.png" /></figure><p>Here the official documentations: <a href="https://holidaypirates.github.io/nucleus/">https://holidaypirates.github.io/nucleus/</a></p><p>Cheers</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=16db9d3e2cf8" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>