<?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: Manpreet Bhikhe</title>
    <description>The latest articles on DEV Community by Manpreet Bhikhe (@msbhikhe).</description>
    <link>https://dev.to/msbhikhe</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%2F363507%2Ffba020fe-724a-4dc8-9240-b5c9f5a213c4.png</url>
      <title>DEV Community: Manpreet Bhikhe</title>
      <link>https://dev.to/msbhikhe</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/msbhikhe"/>
    <language>en</language>
    <item>
      <title>Path aliases in Cypress</title>
      <dc:creator>Manpreet Bhikhe</dc:creator>
      <pubDate>Wed, 10 Jul 2024 11:35:07 +0000</pubDate>
      <link>https://dev.to/msbhikhe/path-aliases-in-cypress-47ao</link>
      <guid>https://dev.to/msbhikhe/path-aliases-in-cypress-47ao</guid>
      <description>&lt;p&gt;I came across &lt;strong&gt;Path Aliases&lt;/strong&gt; some time ago. Articles mentioning how developers made imports in their React and node projects look &lt;em&gt;nice&lt;/em&gt;. I wondered if I could use this feature in my Cypress automation.&lt;/p&gt;

&lt;p&gt;Let me give you some context. So typically imports in my project look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import ProductsPage from '../../pages/ProductsPage';
import Navigation from '../../pages/Navigation';
import testData from '../../../../fixtures/product-listing.json'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But with path aliases, the imports look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import ProductsPage from '@pages/ProductsPage';
import Navigation from '@pages/Navigation';
import testData from '@fixtures/product-listing.json'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Cleaner, right ?&lt;br&gt;
Follow along to setup path aliases in your cypress project.&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 1: Webpack preprocessor
&lt;/h2&gt;

&lt;p&gt;Install webpack preprocessor plugin in your project. This will ultimately help to resolve path aliases.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save-dev @cypress/webpack-preprocessor
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 2: Webpack Options
&lt;/h2&gt;

&lt;p&gt;Create webpack options which will contain reference of paths to the aliases. You can keep this object in &lt;code&gt;cypress.config.js&lt;/code&gt; or separate file as per your preference.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const wpOptions = {
  webpackOptions: {
    resolve: {
      alias: {
        '@pages': path.resolve(__dirname, './cypress/e2e/pages'),
        '@fixtures': path.resolve(__dirname, './cypress/fixtures'),
        '@': __dirname,
      },
    },
  },
  watchOptions: {},
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 3: Configure preprocessor plugin
&lt;/h2&gt;

&lt;p&gt;Next, configure cypress to use webpack preprocessor on every file with options specified in previous step&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;setupNodeEvents(on, config) {
  on('file:preprocessor', webpackPreprocessor(wpOptions));
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 4: Add paths to &lt;code&gt;jsconfig&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;This step will enable intellisense for IDE of your choice. Add following settings to &lt;code&gt;jsconfig.json&lt;/code&gt; file in project root.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "compilerOptions": {
    "paths": {
      "@pages/*": [
        "./cypress/e2e/pages/*"
      ],
      "@fixtures/*": [
        "./cypress/fixtures/*"
      ],
      "@/*": [
        "./*"
      ]
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's all !&lt;/p&gt;

&lt;p&gt;Now you can create imports like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import ProductsPage from '@pages/ProductsPage';
import Navigation from '@pages/Navigation';
import testData from '@fixtures/product-listing.json'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>cypress</category>
      <category>pathaliases</category>
      <category>javascript</category>
      <category>automaton</category>
    </item>
  </channel>
</rss>
