<?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>DEV Community: Roej</title>
    <description>The latest articles on DEV Community by Roej (@riolio).</description>
    <link>https://dev.to/riolio</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%2F1223293%2Fe0b7a146-df34-4b4c-ba57-d0a5671c68e5.png</url>
      <title>DEV Community: Roej</title>
      <link>https://dev.to/riolio</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/riolio"/>
    <language>en</language>
    <item>
      <title>Minimal setup for Vite, React and Jest Integration</title>
      <dc:creator>Roej</dc:creator>
      <pubDate>Fri, 01 Dec 2023 06:50:09 +0000</pubDate>
      <link>https://dev.to/riolio/minimal-setup-for-vite-react-and-jest-integration-2ib6</link>
      <guid>https://dev.to/riolio/minimal-setup-for-vite-react-and-jest-integration-2ib6</guid>
      <description>&lt;p&gt;Recently, I encountered a project where I needed to add test cases, and surprisingly, configuring the project with Vite presented challenges, despite my experience with webpack. Upon completing the project, I decided to create a simple scaffold project with minimal setup to guide others on integrating unit testing in Vite.&lt;/p&gt;

&lt;p&gt;Let's start by creating the project. If you already have a project ready then jump to Step 2&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;Step 1: Create vite project&lt;br&gt;
*&lt;/em&gt;&lt;br&gt;
&lt;code&gt;1. npm create vite@latest&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Choose your desired configuration. I choose following: -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;React&lt;/li&gt;
&lt;li&gt;TypeScript + SWC&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Then install the dependencies by npm i&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;Step 2: Add dependencies in project&lt;br&gt;
*&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i @testing-library/react
npm i jest
npm i @babel/preset-react
npm i @testing-library/jest-dom
npm i -D @babel/preset-env
npm i -D @babel/preset-typescript
npm i -D @types/jest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;*&lt;em&gt;Step 3: Configure package.JSON to add test script&lt;br&gt;
*&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; "test": "jest"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;in scripts&lt;/p&gt;

&lt;p&gt;Add a Jest object at the root&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  "jest": {
    "testEnvironment": "jsdom"
  },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 4: Add Babel configuration babel.config.cjs with following presets&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = {
  presets: [
    "@babel/preset-env",
    "@babel/preset-typescript",
    ["@babel/preset-react", {
      "runtime": "automatic"
    }]
  ],
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 5: Add unit test file. This file can be kept with the component itself or in separate test folder. In this unit test, we're just verifying the title of a component.'testing-library' have user interaction actions such as userEvent which comes under "@testing-library/user-event". Depending on use case, you can use it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { render, screen } from "@testing-library/react";
import "@testing-library/jest-dom";
import App from "./App.tsx";

describe("Test library check- Should always Pass", () =&amp;gt; {
    it("should pass", function () {
        expect(true).toBe(true);
    });
});

describe("Render App component", () =&amp;gt; {
    it("component should have title", async function () {
        render(&amp;lt;App /&amp;gt;);
        expect(screen.getByText("Vite n Jest"));
    });
});

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;*&lt;em&gt;Step 6: Making your test work with Styling such as csss|sass *&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Configure package.json and locate jest object that we just added in previous step. Add a module mapper for styling&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"moduleNameMapper": {
      "\\.(css|less|sass|scss)$": "&amp;lt;rootDir&amp;gt;/__mocks__/styleMock.js"
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a styleMock.js file which is just a mock file as the name implies. Exporting a empty object is sufficient.&lt;br&gt;
&lt;code&gt;module.exports = {};&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;Step 7: Are you using alias for importing in Vite? In that case, you need to add that alias in Jest too  *&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Open package.json file and add alias in jest&amp;gt; moduleNameMapper&lt;br&gt;
&lt;code&gt;"^@/(.*)$": "&amp;lt;rootDir&amp;gt;/src/$1"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Finally the package.json should look similar to following&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  "scripts": {
    "dev": "vite",
    "build": "tsc &amp;amp;&amp;amp; vite build",
    "lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
    "preview": "vite preview",
    "test": "jest"
  },
  "jest": {
    "testEnvironment": "jsdom",
    "moduleNameMapper": {
      "\\.(css|less|sass|scss)$": "&amp;lt;rootDir&amp;gt;/__mocks__/styleMock.js",
      "^@/(.*)$": "&amp;lt;rootDir&amp;gt;/src/$1"

    }
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hopefully, your tests will start running. If you want to review the source code please visit &lt;a href="https://github.com/rohitjaryal/vite-n-jest"&gt;https://github.com/rohitjaryal/vite-n-jest&lt;/a&gt;&lt;br&gt;
By reviewing the code commit, you can follow through step by step.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>react</category>
      <category>vite</category>
    </item>
  </channel>
</rss>
