<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Michael Rhema</title>
    <description>The latest articles on Forem by Michael Rhema (@k0d3d).</description>
    <link>https://forem.com/k0d3d</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F213271%2Ff655d35a-0972-4f65-83d5-953a31fd8757.jpeg</url>
      <title>Forem: Michael Rhema</title>
      <link>https://forem.com/k0d3d</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/k0d3d"/>
    <language>en</language>
    <item>
      <title>Unit Testing for Gatsby using Jest, Typescript and React Testing Library</title>
      <dc:creator>Michael Rhema</dc:creator>
      <pubDate>Tue, 07 Jan 2020 00:14:15 +0000</pubDate>
      <link>https://forem.com/k0d3d/unit-testing-for-gatsby-using-jest-typescript-and-react-testing-library-i7p</link>
      <guid>https://forem.com/k0d3d/unit-testing-for-gatsby-using-jest-typescript-and-react-testing-library-i7p</guid>
      <description>&lt;p&gt;My first experience working TDD using React Testing Library with Jest for a Gatsby website in Typescript.&lt;/p&gt;

&lt;p&gt;Setting up Jest and React Testing Library for TDD with Gatsby is quite straight forward. It does get a bit tricky because I plan on using Typescript in my tests. &lt;br&gt;
First, I install &lt;code&gt;jest&lt;/code&gt;,  &lt;code&gt;babel-jest&lt;/code&gt; and &lt;code&gt;babel-preset-gatsby&lt;/code&gt; to ensure that the babel preset(s) that are used match what are used internally for a Gatsby site.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save-dev jest babel-jest  babel-preset-gatsby identity-obj-proxy tslint-react @types/jest
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Configuring Jest to work with Gatsby
&lt;/h2&gt;

&lt;p&gt;Because Gatsby handles its own Babel configuration, I will need to manually tell Jest to use &lt;code&gt;babel-jest&lt;/code&gt;. The gatsby website recommends creating a &lt;code&gt;jest.config.js&lt;/code&gt; file. The code below works for me and should work nicely for you.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;jest.config.js&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const path = require("path")

module.exports = {
  setupFilesAfterEnv: [
    path.resolve(__dirname, "./jest-configs/setup-test-env.js")
  ],
  transform: {
    // "^.+\\.(tsx?|jsx?)$": "ts-jest",
    "\\.svg": "&amp;lt;rootDir&amp;gt;/jest-configs/__mocks__/svgTransform.js" ,
    "^.+\\.(tsx?|jsx?)$": `&amp;lt;rootDir&amp;gt;/jest-configs/jest-preprocess.js`,
  },
  moduleNameMapper: {
    // "\\.svg": `./jest-configs/__mocks__/file-mocks.js`,
    "\\.svg": `&amp;lt;rootDir&amp;gt;/jest-configs/__mocks__/svgTransform.js`,
    "typeface-*": "identity-obj-proxy",
    ".+\\.(css|styl|less|sass|scss)$": `identity-obj-proxy`,
    ".+\\.(jpg|jpeg|png|gif|eot|otf|webp|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": `&amp;lt;rootDir&amp;gt;/jest-configs/__mocks__/file-mocks.js`,
  },
  testPathIgnorePatterns: [`node_modules`, `.cache`, `public`],
  transformIgnorePatterns: [`node_modules/(?!(gatsby)/)`, `\\.svg`],
  globals: {
    __PATH_PREFIX__: ``,
  },
  testRegex: "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx)$",
  moduleFileExtensions: [
    "ts",
    "tsx",
    "js"
  ],
  collectCoverage: false,
  coverageReporters: [
    "lcov",
    "text",
    "html"
  ]
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;em&gt;svgTransform.js&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = {
  process() {
    return 'module.exports = {};';
  },
  getCacheKey() {
    // The output is always the same.
    return 'svgTransform';
  },
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;transform:&lt;/code&gt; option tells Jest that all ts, tsx, js or jsx files need to be transformed using a &lt;code&gt;jest-preprocess.js&lt;/code&gt; file. I have created this file in a &lt;code&gt;jest-configs&lt;/code&gt; folder at the root of my project.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;jest-configs/jest-preprocess.js&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const babelOptions = {
  presets: [
    '@babel/preset-react', 
    'babel-preset-gatsby', 
    "@babel/preset-typescript"
  ],
};

module.exports = require("babel-jest").createTransformer(babelOptions)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I also need to put some code in &lt;code&gt;setup-test-env.js&lt;/code&gt; . &lt;br&gt;
The &lt;a href="https://jestjs.io/docs/en/configuration#setupfilesafterenv-array"&gt;Jest Configuration&lt;/a&gt; docs explains the &lt;code&gt;setupFilesAfterEnv: ....&lt;/code&gt; configuration option if you need to understand what it means along with other available configuration options.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A list of paths to modules that run some code to configure or set up the testing framework before each test.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;jest-configs/setup-test-env.js&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import "@testing-library/jest-dom/extend-expect"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That should have Jest properly configured. Now, I'll install the testing library and jest-dom as dev-dependencies with npm.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save-dev @testing-library/react @testing-library/jest-dom
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;After this I could run &lt;code&gt;npx jest&lt;/code&gt; ( Using npx because jest isn't installed globally ) and I got good.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Zjo1cRzx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/sRhdLKd/Screenshot-2020-01-07-at-12-36-15-AM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Zjo1cRzx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/sRhdLKd/Screenshot-2020-01-07-at-12-36-15-AM.png" alt="cli"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Ok GREAT WORK.
&lt;/h2&gt;

&lt;p&gt;Fingers crossed, I am about to write my first test and run it.&lt;br&gt;
One major reason to love TDD is to fail faster or break things faster. This means, write tests before your write code. Also test should initially fail, always. &lt;a href="https://hackernoon.com/introduction-to-test-driven-development-tdd-61a13bc92d92"&gt;Read this up&lt;/a&gt; from Hackernoon.&lt;/p&gt;

&lt;p&gt;I'll create a &lt;code&gt;__tests__&lt;/code&gt; folder on my project root folder. I'll then create a file called &lt;code&gt;test.spec.tsx&lt;/code&gt; and paste this code in it. I usually prefer my tests along side my components eg `src/components/-componentName-/-componentName.spec.ts-.&lt;br&gt;
Right now, I am not sure which convention is better accepted.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;tests&lt;/strong&gt;/test.spec.tsx&lt;/em&gt;&lt;/p&gt;


&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&lt;br&gt;
import React from "react"&lt;br&gt;
import { render } from "@testing-library/react"

&lt;p&gt;// You have to write data-testid&lt;br&gt;
const Title = () =&amp;gt; &amp;lt;h1 data-testid="hero-title"&amp;gt;Gatsby is awesome!&amp;lt;/h1&amp;gt;&lt;/p&gt;

&lt;p&gt;test("Displays the correct title", () =&amp;gt; {&lt;br&gt;
  const { getByTestId } = render(&amp;lt;Title /&amp;gt;)&lt;br&gt;
  // Assertion&lt;br&gt;
  expect(getByTestId("hero-title")).toHaveTextContent("Gatsby is awesome!")&lt;br&gt;
  // --&amp;gt; Test will pass&lt;br&gt;
})&lt;/p&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h4&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  &lt;em&gt;Run Yarn or npm install if you get errors like...&lt;/em&gt;&lt;br&gt;
&lt;/h4&gt;
&lt;br&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Cannot find module 'react' from 'test.spec.tsx'&lt;br&gt;
    &amp;gt; 1 | import React from "react"&lt;br&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h1&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  HURRAY SUCCESSFUL UNIT TESTING WITH TYPESCRIPT AND GATSBY AND JEST AND REACT TESTING LIBRARY&lt;br&gt;
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lvWDXHh9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/tKSRxLK/Screenshot-2020-01-07-at-1-02-50-AM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lvWDXHh9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/tKSRxLK/Screenshot-2020-01-07-at-1-02-50-AM.png" alt="test-passes"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I feel really good with this. I am just starting out Typescript with React so this was a lot of fun for me. I'll put up another post about writing real code using TDD for a FAQ website I am building using Gatsby.&lt;/p&gt;

&lt;p&gt;Thank you for reading this.. &lt;/p&gt;

</description>
      <category>testing</category>
      <category>react</category>
      <category>tdd</category>
      <category>gatsby</category>
    </item>
    <item>
      <title>Composition in Angular the React JS way</title>
      <dc:creator>Michael Rhema</dc:creator>
      <pubDate>Wed, 13 Nov 2019 17:53:29 +0000</pubDate>
      <link>https://forem.com/k0d3d/composition-in-angular-the-react-js-way-51ai</link>
      <guid>https://forem.com/k0d3d/composition-in-angular-the-react-js-way-51ai</guid>
      <description>&lt;p&gt;What ever it is Dev.to has here as an editor, should get binned. I know we trying to get real geeky here, but for people who wanna write coming from a medium world, this editor right here is are turn-off. &lt;br&gt;
But pre-rambling, I was actually think of ..... ohh shit I forgot... ehm .. {countdown to brain restart and ... yes thats it. &lt;br&gt;
Im working on this project in Angular 6. For some reason I have the strong desire to use composition instead of inheritance for sharing just logic and using composition to build complex UIs in this Angular project as I have tried in my other React projects because React encourages composition over Inheritance..&lt;/p&gt;

&lt;p&gt;In my experience, React developers preference of composition over inheritance for building complex user interaction driven application is really good reason use React a lot more. &lt;br&gt;
But I am thinking, I can use the same principle in Angular 6 and build components and interactions using composition &lt;/p&gt;

</description>
      <category>chrome</category>
      <category>webdev</category>
      <category>tenx</category>
    </item>
  </channel>
</rss>
