<?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: Eamonn Walsh</title>
    <description>The latest articles on DEV Community by Eamonn Walsh (@eamonnprwalsh).</description>
    <link>https://dev.to/eamonnprwalsh</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%2F122673%2Fcd6512d0-f6ef-4999-a6bd-c2127b658007.jpeg</url>
      <title>DEV Community: Eamonn Walsh</title>
      <link>https://dev.to/eamonnprwalsh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/eamonnprwalsh"/>
    <language>en</language>
    <item>
      <title>React Application: Build Once, Deploy Anywhere Solution</title>
      <dc:creator>Eamonn Walsh</dc:creator>
      <pubDate>Fri, 23 Jun 2023 18:06:52 +0000</pubDate>
      <link>https://dev.to/eamonnprwalsh/react-application-build-once-deploy-anywhere-solution-507m</link>
      <guid>https://dev.to/eamonnprwalsh/react-application-build-once-deploy-anywhere-solution-507m</guid>
      <description>&lt;p&gt;In this blog post, we will explore a solution for the build once deploy many issue faced by projects working with web application libraries such as React. &lt;/p&gt;

&lt;p&gt;In React environment-specific configuration is baked in at build time, meaning you most likely need to create individual build artifacts for dev, test, uat, live etc. This can turn into a significant time sink if the build process is even remotely lengthy.&lt;/p&gt;

&lt;p&gt;The goal is to decouple environment-specific configuration from the build process, allowing the same build artifact to be deployed to multiple environments with different configurations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Basic knowledge of React and Vite&lt;/li&gt;
&lt;li&gt;Familiarity with Docker and Kubernetes concepts&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Steps
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Placeholder-based Configuration
&lt;/h3&gt;

&lt;p&gt;To make the application deployable to different environments, we need to remove all hard-coded references to environment variables and replace them with placeholders. During the build process, Vite will generate a deployable asset containing these placeholders instead of the actual environment variable values.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Move Environment Variables to Kubernetes
&lt;/h3&gt;

&lt;p&gt;Store your environment-specific configuration in Kubernetes, preferably using a ConfigMap. This allows you to set the configuration on a per-environment basis.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Create a Shared Environment File
&lt;/h3&gt;

&lt;p&gt;Create a single environment file, e.g., &lt;code&gt;.env.all&lt;/code&gt;, that will be used for all builds. Set your variables to align with the placeholder names. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;VITE_VARIABLE_1=%VITE_VARIABLE_1%
VITE_VARIABLE_2=%VITE_VARIABLE_2%
VITE_VARIABLE_3=%VITE_VARIABLE_3%
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. Create a Configuration Replacement Script
&lt;/h3&gt;

&lt;p&gt;In your React application codebase, create a JavaScript script that can be executed inside your Docker container. This script will replace the placeholders with the actual environment variable values at deploy time, pulling the values from the ConfigMap in Kubernetes. It's recommended to create this script in a folder outside the &lt;code&gt;/src&lt;/code&gt; directory, such as &lt;code&gt;/static&lt;/code&gt;, to avoid it being included in the JavaScript asset during build time.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Copy the Replacement Script
&lt;/h3&gt;

&lt;p&gt;Use Vite's static copy plugin to move the replacement script into the build output folder (by default, &lt;code&gt;dist&lt;/code&gt;). Add the following configuration to your Vite project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;react&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@vitejs/plugin-react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;viteStaticCopy&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;vite-plugin-static-copy&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Other Vite configuration options...&lt;/span&gt;
  &lt;span class="na"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nx"&gt;react&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="nx"&gt;viteStaticCopy&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;targets&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="na"&gt;src&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./static/replacePlaceHolders.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="na"&gt;dest&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./static/replacePlaceHolders.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="p"&gt;}),&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  6. Run the Build Command
&lt;/h3&gt;

&lt;p&gt;Run your build command as usual, but make sure to point to your single &lt;code&gt;.env.all&lt;/code&gt; file using the &lt;code&gt;mode&lt;/code&gt; flag. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;tsc &lt;span class="nt"&gt;-p&lt;/span&gt; tsconfig.build.json &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; vite build &lt;span class="nt"&gt;--mode&lt;/span&gt; all
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  7. Verify the Build Output
&lt;/h3&gt;

&lt;p&gt;Verify that the built asset contains the variable placeholders instead of the actual values. Also, ensure that the &lt;code&gt;replacePlaceHolders.js&lt;/code&gt; script exists in the &lt;code&gt;dist&lt;/code&gt; folder.&lt;/p&gt;

&lt;h3&gt;
  
  
  8. Update the Dockerfile
&lt;/h3&gt;

&lt;p&gt;Update your Dockerfile to include Node.js as part of the image so that it can run the &lt;code&gt;replacePlaceHolders.js&lt;/code&gt; script during the image preparation phase. &lt;/p&gt;

&lt;p&gt;The majority of Docker images used for deploying React applications do not typically necessitate the presence of Node. However, we specifically require Node to be running in order to access and retrieve environment-specific configuration set in your ConfigMap via the process.env object in Node.&lt;/p&gt;

&lt;p&gt;Add the following command to the Dockerfile:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;CMD&lt;/span&gt;&lt;span class="s"&gt; ["node", "./replacePlaceHolders.js"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will execute the JavaScript script in Node.js, providing access to the variables set in the ConfigMap. You can replace placeholders in files using the following code snippet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const fs = require('fs');
const filePath = '&amp;lt;path-to-file&amp;gt;';

let fileContent = fs.readFileSync(filePath, 'utf-8');
        let fileContent = await fs.readFile(&amp;lt;path-to-file&amp;gt;, 'utf-8');

        fileContent = fileContent.replace('%VITE_VARIABLE_1%', process.env.VITE_VARIABLE_1);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;There are a few other approaches to do this but they all need to add what I would consider as configuration logic to the app itself and don't run at deploy time. There is no perfect solution &lt;/p&gt;

</description>
    </item>
    <item>
      <title>Migrating from Jest to Vitest for React Testing</title>
      <dc:creator>Eamonn Walsh</dc:creator>
      <pubDate>Sat, 17 Jun 2023 16:42:30 +0000</pubDate>
      <link>https://dev.to/eamonnprwalsh/migrating-from-jest-to-vitest-for-react-testing-ljn</link>
      <guid>https://dev.to/eamonnprwalsh/migrating-from-jest-to-vitest-for-react-testing-ljn</guid>
      <description>&lt;h2&gt;
  
  
  Migrating from Jest to Vitest for React Testing
&lt;/h2&gt;

&lt;p&gt;When it comes to testing React applications, Jest has been a popular choice for many developers. However, if you're looking for an alternative testing framework, Vitest could be a great option to explore. In this guide, we'll walk through the process of migrating from Jest to Vitest, step by step.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: Uninstall Jest
&lt;/h3&gt;

&lt;p&gt;To begin the migration process, you'll need to uninstall Jest from your project. You can do this by running the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm uninstall jest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In many projects there may be additional dependencies to remove - some to watch out for&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@types/jest
jest-css-modules
jest-environment-jsdom
ts-jest 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Step 2: Install Vitest
&lt;/h3&gt;

&lt;p&gt;Next, install Vitest as your new testing framework. Use the following command to install Vitest:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm i &lt;span class="nt"&gt;-D&lt;/span&gt; vitest 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 3: Install Dependencies for Vitest
&lt;/h3&gt;

&lt;p&gt;Vitest may require additional dependencies based on your testing needs. Make sure to install any necessary third-party dependencies for Vitest. At a minimum I would recommend for coverage to install :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm i &lt;span class="nt"&gt;-D&lt;/span&gt; @vitest/coverage-v8
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Feel free to add any other testing-related dependencies required for your project.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 4: Remove Jest Configuration Files
&lt;/h3&gt;

&lt;p&gt;Since you're migrating away from Jest, you can remove any Jest-specific configuration files, such as &lt;code&gt;jest.config.js&lt;/code&gt; or &lt;code&gt;jest.setup.js&lt;/code&gt;, from your project.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 5: Create &lt;code&gt;setUpTests.ts&lt;/code&gt; File
&lt;/h3&gt;

&lt;p&gt;Create a new file named &lt;code&gt;setUpTests.ts&lt;/code&gt; in your project's source directory (e.g., &lt;code&gt;src/setUpTests.ts&lt;/code&gt;). This file will serve as the setup file for Vitest. Here's an example content for &lt;code&gt;setUpTests.ts&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// add Vitest functions here globally&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;afterEach&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;vitest&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;cleanup&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@testing-library/react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;matchers&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@testing-library/jest-dom/matchers&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Extend Vitest's expect method with methods from react-testing-library&lt;/span&gt;
&lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;extend&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;matchers&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Run cleanup after each test case (e.g., clearing jsdom)&lt;/span&gt;
&lt;span class="nx"&gt;afterEach&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;cleanup&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Feel free to customize the &lt;code&gt;setUpTests.ts&lt;/code&gt; file based on your specific testing requirements.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 6: Modify &lt;code&gt;vite.config.ts&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Open your &lt;code&gt;vite.config.ts&lt;/code&gt; file (or create one if it doesn't exist) and make the following modifications:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;/// &amp;lt;reference types="vitest" /&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;defineConfig&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;vite&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;react&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@vitejs/plugin-react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;viteTsconfigPaths&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;vite-tsconfig-paths&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;defineConfig&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;react&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nx"&gt;viteTsconfigPaths&lt;/span&gt;&lt;span class="p"&gt;()],&lt;/span&gt;
  &lt;span class="na"&gt;test&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;globals&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;jsdom&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;setupFiles&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./src/setUpTests.ts&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ensure that you have the necessary imports for the plugins used in the &lt;code&gt;vite.config.ts&lt;/code&gt; file.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 7: Update Unit Test Code
&lt;/h3&gt;

&lt;p&gt;In Vitest, the API is similar to Jest, so updating your unit test code should be straightforward. Replace references to Jest functions with the corresponding Vitest functions. For example, replace &lt;code&gt;jest.fn()&lt;/code&gt; with &lt;code&gt;vi.fn()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Make the necessary changes to your unit test&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Migrating from Jest to Vitest for your React testing needs is a straightforward process that allows you to explore a different testing framework with its own set of features and advantages. By following the steps outlined in this guide, you can smoothly transition your tests to Vitest and take advantage of its capabilities.&lt;/p&gt;

&lt;p&gt;It's important to note that Vitest, unlike Jest, does not provide built-in type checking for your test code. Therefore, it's crucial to ensure proper type annotations and type checking in your codebase separately. Be diligent in maintaining the correctness of your tests by ensuring that they align with the expected types of the data being tested.&lt;/p&gt;

&lt;p&gt;One thing to be aware of when using Vitest is that tests may still pass even when incorrect or mismatched data types are used. Vitest does not perform strict type checks by default, so it's essential to exercise caution and validate your test inputs and assertions manually.&lt;/p&gt;

&lt;p&gt;Overall, migrating from Jest to Vitest opens up new possibilities and testing approaches for your React applications. Be sure to explore the Vitest documentation and community resources to fully leverage its capabilities and take your testing to the next level.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Vite TypeScript &amp; Jest with Absolute Paths</title>
      <dc:creator>Eamonn Walsh</dc:creator>
      <pubDate>Sat, 17 Jun 2023 08:59:53 +0000</pubDate>
      <link>https://dev.to/eamonnprwalsh/vite-typescript-jest-with-absolute-paths-5bh3</link>
      <guid>https://dev.to/eamonnprwalsh/vite-typescript-jest-with-absolute-paths-5bh3</guid>
      <description>&lt;p&gt;&lt;b&gt;Introduction:&lt;/b&gt;&lt;br&gt;
When working with Vite, TypeScript, and Jest, setting up absolute paths for module imports can enhance code organization and improve developer productivity. However, configuring absolute paths in this setup can sometimes be challenging. In this blog post, we will explore the steps to enable absolute path imports in a Vite project that uses TypeScript and Jest. We'll cover the necessary configurations and potential solutions to address common issues that may arise during the process.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Table of Contents:&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;&lt;b&gt;1.&lt;/b&gt;The Challenge Faced&lt;br&gt;
&lt;b&gt;2.&lt;/b&gt;Setting up Absolute Paths in Vite&lt;br&gt;
 &lt;b&gt;2.1.&lt;/b&gt; Install vite-tsconfig-paths&lt;br&gt;
 &lt;b&gt;2.2.&lt;/b&gt; Configuring vite.config.js&lt;br&gt;
 &lt;b&gt;2.3.&lt;/b&gt; Verifying TypeScript Configuration &lt;br&gt;
&lt;b&gt;3.&lt;/b&gt;Testing with Jest&lt;br&gt;
 &lt;b&gt;3.1.&lt;/b&gt; Configuring Jest for Absolute Paths&lt;/p&gt;
&lt;h2&gt;
  
  
  1. The challenge faced
&lt;/h2&gt;

&lt;p&gt;When working with a Vite project using TypeScript, absolute paths allow us to import modules without worrying about the relative path from the current file. This can improve readability, simplify imports, and make code more maintainable. However, configuring absolute paths in the Vite environment, especially when also using Jest for testing, can present challenges due to different module resolution mechanisms.&lt;/p&gt;
&lt;h2&gt;
  
  
  2. Setting up Absolute Paths in Vite
&lt;/h2&gt;

&lt;p&gt;To enable absolute path imports in Vite, we need to configure the project appropriately. Here are the steps to follow:&lt;/p&gt;
&lt;h3&gt;
  
  
  2.1. Install &lt;code&gt;vite-tsconfig-paths&lt;/code&gt;
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--save-dev&lt;/span&gt; vite-tsconfig-paths
&lt;span class="c"&gt;# or&lt;/span&gt;
yarn add &lt;span class="nt"&gt;--dev&lt;/span&gt; vite-tsconfig-paths
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  2.2. Configuring &lt;code&gt;vite.config.js&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;In the Vite project's root directory, locate the &lt;code&gt;vite.config.js&lt;/code&gt; file (create one if it doesn't exist). Update the file as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// vite.config.js&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;defineConfig&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;vite&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;react&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@vitejs/plugin-react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;viteTsconfigPaths&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;vite-tsconfig-paths&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// https://vitejs.dev/config/&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;defineConfig&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;react&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nx"&gt;viteTsconfigPaths&lt;/span&gt;&lt;span class="p"&gt;()],&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  2.3. Verifying TypeScript Configuration
&lt;/h3&gt;

&lt;p&gt;Make sure your TypeScript configuration (&lt;code&gt;tsconfig.json&lt;/code&gt;) includes the necessary options for absolute path resolution:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"compilerOptions"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"baseUrl"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./src"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ensure the values correspond to your project's directory structure.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Testing with Jest
&lt;/h2&gt;

&lt;p&gt;To ensure Jest recognizes and resolves absolute paths, follow these steps:&lt;/p&gt;

&lt;h3&gt;
  
  
  3.1. Configuring Jest for Absolute Paths
&lt;/h3&gt;

&lt;p&gt;In your Jest configuration file (&lt;code&gt;jest.config.js&lt;/code&gt; or &lt;code&gt;jest.config.ts&lt;/code&gt;), make the following updates:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// jest.config.js&lt;/span&gt;
&lt;span class="cm"&gt;/** @type {import('ts-jest').JestConfigWithTsJest} */&lt;/span&gt;
&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;preset&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ts-jest&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;testEnvironment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;jsdom&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;setupFilesAfterEnv&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@testing-library/jest-dom/extend-expect&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;moduleDirectories&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;node_modules&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;src&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let me know in the comments if this works for you &lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
