<?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: Afraz Ahmed</title>
    <description>The latest articles on Forem by Afraz Ahmed (@afraz33).</description>
    <link>https://forem.com/afraz33</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%2F1383325%2F1308b1b8-70e8-40fe-a6ac-836e7560be7a.jpg</url>
      <title>Forem: Afraz Ahmed</title>
      <link>https://forem.com/afraz33</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/afraz33"/>
    <language>en</language>
    <item>
      <title>Automating Testing in Next.js: Ensuring Code Integrity with Jest and GitHub Actions</title>
      <dc:creator>Afraz Ahmed</dc:creator>
      <pubDate>Thu, 11 Apr 2024 17:51:01 +0000</pubDate>
      <link>https://forem.com/afraz33/automating-testing-in-nextjs-ensuring-code-integrity-with-jest-and-github-actions-3bbe</link>
      <guid>https://forem.com/afraz33/automating-testing-in-nextjs-ensuring-code-integrity-with-jest-and-github-actions-3bbe</guid>
      <description>&lt;p&gt;Often, you work with fellow developers for a company, your small project, or any other contribution. You need to ensure that the code you've written is all right, and performs the intended functionality before pushing and merging to github. However, this process is quite tedious, as you have to first check if the code works all right, and after you've merged the changes into the main/master (production) branch, you need to verify if it works as it is supposed to. This blog will help you eliminate that process by automating testing in the NextJS app so that you can only merge the code once it passes the intended functionality specified by yourself.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up a NextJS app
&lt;/h2&gt;

&lt;p&gt;Create a folder on your device, name it anything you want i.e NextJs-testing, open your favourite IDE i.e VSCode, and type this command in the terminal.&lt;br&gt;
&lt;code&gt;npx create-next-app@latest --example with-jest with-jest-app&lt;/code&gt;&lt;br&gt;
You can also follow the documentation to explore how to setup NextJS app with Jest and customizations&lt;br&gt;
&lt;a href="https://nextjs.org/docs/app/building-your-application/testing/jest" rel="noopener noreferrer"&gt;Setting up Jest with Next.js&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a repository on Github
&lt;/h2&gt;

&lt;p&gt;Go to github.com, and create a new repository. Name it anything you want, i.e Nextjs-test.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjqxv7fcr5yk95ohfvtmm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjqxv7fcr5yk95ohfvtmm.png" alt="GitHub repository creation"&gt;&lt;/a&gt;Click on proceed after you've provided the name and an optional README file.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up a file to test
&lt;/h2&gt;

&lt;p&gt;After you've created your next app, create a components folder inside your app folder. If you've opted not to have the app router, in which case you will not have this folder in your source code, you can create a _components folder in your root directory.&lt;br&gt;
Inside your components folder, create a file as ComponentA.jsx. You can name it anything apart from test.jsx. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9bqpc82s1ribdlc9q7ys.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9bqpc82s1ribdlc9q7ys.png" alt="File Structure"&gt;&lt;/a&gt;&lt;br&gt;
You can copy this code inside your ComponentA.jsx &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

function ComponentA() {
  return &amp;lt;h1&amp;gt;Testing&amp;lt;/h1&amp;gt;;
}

export default ComponentA;





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

&lt;/div&gt;
&lt;h2&gt;
  
  
  Adding a test case
&lt;/h2&gt;

&lt;p&gt;The next step is to create a file where you'll write your test cases. If you have a _&lt;em&gt;tests_&lt;/em&gt; folder, create a page.test.jsx file. If you donot have this folder by default, you can create one manually as well. Once you've created the file, you can copy this code inside your page.test.jsx&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

import "@testing-library/jest-dom";
import { render, screen } from "@testing-library/react";
import ComponentA from "../app/components/ComponentA";

describe("Page", () =&amp;gt; {
  it("renders a heading", () =&amp;gt; {
    render(&amp;lt;ComponentA /&amp;gt;);

    const heading = screen.getByRole("heading", { level: 1 });

    expect(heading).toBeInTheDocument();
  });
});



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

&lt;/div&gt;

&lt;p&gt;This code tests the rendering of a &lt;em&gt;&lt;strong&gt;h1&lt;/strong&gt;&lt;/em&gt; tag in the ComponentA.jsx.&lt;/p&gt;

&lt;h2&gt;
  
  
  Update package.json
&lt;/h2&gt;

&lt;p&gt;In your package.json, add &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

"test": "jest",
"test:watch": "jest --watch"


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

&lt;/div&gt;

&lt;p&gt;inside scripts object.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create workflow for automated testing
&lt;/h2&gt;

&lt;p&gt;In your root directory, create a folder and name it as .github. Inside it, create another folder .workflows, and inside it create a file &lt;strong&gt;main.yml&lt;/strong&gt;. Copy this code inside your main.yml &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

name: Tests for Next.js app

on:
  pull_request:
    branches:
      - main

jobs:
  NextJS-Tests:
    name: Tests for Next.js app
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Setup Node.js
        uses: actions/setup-node@v2
        with:
          node-version: "20"

      - name: Install dependencies
        run: |
          npm install

      - name: Run Jest tests
        run: npm test



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

&lt;/div&gt;

&lt;p&gt;This workflow executes the jobs specified on pull request to the main branch. &lt;br&gt;
&lt;em&gt;Note: If you have the master branch instead of main, you can replace main with master in main.yml&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Add branch protection rules
&lt;/h2&gt;

&lt;p&gt;In your github repository, navigate to the settings tab, and from the sidebar, select Branches under Code and automation. Specify the name of the branch i.e main/master.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbkjhj808z1zrl95rz63n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbkjhj808z1zrl95rz63n.png" alt="Branch Protection tab"&gt;&lt;/a&gt;&lt;br&gt;
Next, you've to specify some settings for your branch under the Protect matching branches.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Check &lt;strong&gt;&lt;em&gt;Require a pull request before merging&lt;/em&gt;&lt;/strong&gt;. &lt;/li&gt;
&lt;li&gt;Check &lt;strong&gt;&lt;em&gt;Require status checks to pass before merging&lt;/em&gt;&lt;/strong&gt;. &lt;/li&gt;
&lt;li&gt;Search for the status checks in the search bar provided below. This should be the name specified for jobs in main.yml &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fegls2tjbi4yi13xnje29.png" alt="Branch Protection rules"&gt;
&lt;/li&gt;
&lt;li&gt;Check &lt;strong&gt;&lt;em&gt;Do not allow bypassing the above settings&lt;/em&gt;&lt;/strong&gt;. This will make sure that not even the repository owner is allowed to directly push onto the branch, or bypass these rules. If not checked, only the repository owner can directly push to the main/master branch without making a pull request.&lt;/li&gt;
&lt;li&gt;Click on Save Changes button.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Merging dev branch into main
&lt;/h2&gt;

&lt;p&gt;Create another branch from main i.e dev. In ComponentA.jsx, replace the h1 tag with h2 tag.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fast1avonfxbt7iv28w94.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fast1avonfxbt7iv28w94.png" alt="ComponentA.jsx code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Commit changes locally and push the code to github repository. Create pull request from dev to main. &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fys77dx85bxr5u23nxtkz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fys77dx85bxr5u23nxtkz.png" alt="Pull Request"&gt;&lt;/a&gt;&lt;br&gt;
After creating a pull request to main, our test cases will execute as we specified the trigger to be on pull request to main.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flpohhwxl5ujfmzcj51ra.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flpohhwxl5ujfmzcj51ra.png" alt="Workflows"&gt;&lt;/a&gt;&lt;br&gt;
However, you will be unable to merge dev into main since our test cases have failed, resulting in the status checks not being passed. This is due to the fact that we replaced h1 tag with h2 tag, however in our &lt;em&gt;&lt;strong&gt;page.test.jsx&lt;/strong&gt;&lt;/em&gt;, we specified that there should be a level 1 heading. &lt;br&gt;
Since our test cases have not passed, we will be unable to merge dev into main.&lt;/p&gt;

&lt;h2&gt;
  
  
  View Workflows
&lt;/h2&gt;

&lt;p&gt;You can view what went wrong when your test cases failed, by clicking on the Actions tab, and selecting the workflow from there.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3gykfgsn4hqgjanmbpa2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3gykfgsn4hqgjanmbpa2.png" alt="Workflows"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Correcting the code to pass test cases
&lt;/h2&gt;

&lt;p&gt;Go to ComponentA.jsx file on your local dev branch. Replace h2 with h1 tag as before. Commit changes locally and push the changes to remote dev branch. &lt;br&gt;
Navigate to pull requests tab on the tab, and you'll see test cases executing again. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv8miqvsrxjndz221rnb2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv8miqvsrxjndz221rnb2.png" alt="Pull Request"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In a few moments, you'll see successful execution of the test cases, and you'll be able to merge dev into main now.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxnq6bv6nbcrt9ti9b4ur.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxnq6bv6nbcrt9ti9b4ur.png" alt="Test cases"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this way, you can write test cases for your NextJs app, and automate them using github actions so that you don't have to worry about any breaking changes that may occur after merging the code to the main/master branch. If the test cases fail, you can always go back and correct the code before pushing the code again to dev/feature branch.&lt;/p&gt;

&lt;p&gt;Do let me know if I've missed anything, or if you feel anything is incorrect or can be improved.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>nextjs</category>
      <category>testing</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
