Logo

dev-resources.site

for different kinds of informations.

Guide - Setting Up Jest for Unit Testing in a TypeScript React Project

Published at
1/10/2025
Categories
typescript
jest
testing
react
Author
rakhee
Categories
4 categories in total
typescript
open
jest
open
testing
open
react
open
Author
6 person written this
rakhee
open
Guide - Setting Up Jest for Unit Testing in a TypeScript React Project

Recently, I got a chance to work on my first typescript project, and my first task was to set up unit test cases. At first glance, it seemed simple enough. I started following random documentation and ChatGPT suggestions, only to end up with a mess. If you’ve experienced GPT giving a solution only to later suggest removing it, you’ll relate to my frustration.

After countless docs, GitHub issues, and a little frustration, I finally cracked the code and set up a clean, robust unit testing environment. 🎉

And then I thought, “Why not document this to save future-me and fellow devs from the same struggle?” So, here it is:

Here, I’ve written one more comprehensive guide on the internet to help fellow developers set up their first unit test environment in a TypeScript project—the right way.


Step 1: Install Required Libraries

Start by installing the necessary libraries to set up a Jest environment:

npm install -D @types/jest @types/react-dom @types/react ts-jest typescript @testing-library/jest-dom @testing-library/react
Enter fullscreen mode Exit fullscreen mode

Step 2: Initialize tsconfig.json

Run the following command to create a tsconfig.json file in your project’s root directory:

ts-jest config:init
Enter fullscreen mode Exit fullscreen mode

Step 3: Configure tsconfig.json

Replace the content of your tsconfig.json file with the following configuration:

{
  "compilerOptions": {
   "types": ["@testing-library/jest-dom", "node", "jest"],
    "module": "commonjs",
    "target": "es6",
    "outDir": "./dist",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "jsx": "react",  
    "lib": ["es2019", "dom"]  
  },
  "include": ["./src/**/**.*", "src/*.ts", "src/setupTests.tsx"]
  "exclude": ["node_modules"]
}

Enter fullscreen mode Exit fullscreen mode

Step 4: Configure Jest

Replace the content of your jest.config.js file with the following:

module.exports = {
  testEnvironment: "jest-environment-jsdom",
  setupFilesAfterEnv: ["<rootDir>/src/setupTests.tsx"], // Update the path if your setupTests file is located elsewhere
  transform: {
    "^.+\\.tsx?$": "babel-jest", // Or ts-jest if you're using ts-jest
  },
  moduleFileExtensions: ["ts", "tsx", "js", "jsx"],
};
Enter fullscreen mode Exit fullscreen mode

Next, create a setupTests.tsx file in your src folder and add the following:

import "@testing-library/jest-dom";
Enter fullscreen mode Exit fullscreen mode

Step 5: Write Test Cases

Create a test file, e.g., YourComponent.test.tsx, in your project’s tests folder. This is where you’ll write your unit test cases.


Step 6: Run Tests

Run the following command to execute your tests:

npm test
Enter fullscreen mode Exit fullscreen mode

Personal Note - When you are working with Typescript you will see this below error

Property 'toBeInTheDocument' does not exist on type 'JestMatchers<HTMLElement>'

To resolve I have already added configuration in the above code but If you still see the issue then

  • Try importing this library @testing-library/jest-dom in all your test files.

  • Make sure that you have the correct Babel configuration.


Notes

  • Remember, this guide focuses on setting up the environment; you’ll need to figure out how to write the actual test cases based on your application.

I hope this guide saves you the frustration I faced. Happy testing! 🚀

jest Article's
30 articles in total
Favicon
Why You Hate Test Coverage
Favicon
Mockando Constantes em Testes com Jest: Um Exemplo Prático
Favicon
Taming the CI Beast: Optimizing a Massive Next.js Application (Part 1)
Favicon
Testing a GraphQL Application with Jest and SuperTest
Favicon
[Boost]
Favicon
How to write unit test in react?
Favicon
4. Testing (async) searchParams with Jest in Next 15
Favicon
3. How to setup Jest in a Next 15 project (+ eslint for testing)
Favicon
Sharding Jest tests. Harder than it should be?
Favicon
Migration from Jest to Vitest: A Real-World Experience with 2900+ Tests
Favicon
Mastering Jest: A Guide to Efficient JavaScript Testing
Favicon
Guide - Setting Up Jest for Unit Testing in a TypeScript React Project
Favicon
Testes Unitários com Jest
Favicon
5. Mocking usePathName, useSearchParams and useRouter with Jest in Next 15
Favicon
🛠️ Writing Reliable Code from the Ground Up !
Favicon
How To Test Your JavaScript Application With Jest Framework?
Favicon
Mocking with Jest and typescript - a cheatsheet
Favicon
Mastering Testing in React with Jest and React Testing Library
Favicon
Exploring Snapshot Testing in Jest: Pros and Cons
Favicon
Implementing CI with GitHub Actions Workflow
Favicon
How to write jest test cases for react component?
Favicon
Testing LLM Applications: Misadventures in Mocking SDKs vs Direct HTTP Requests
Favicon
Creating tests in real database with NestJS, TypeORM and PostgreSQL
Favicon
Let’s Make Jest Run Much Faster
Favicon
Testing and Debugging: Basic Tools and Techniques for Effective Full-Stack Tests
Favicon
Comparing Jest, React Testing Library, and Playwright: Testing Approaches for React Applications
Favicon
Error in Jest integration with Vue js
Favicon
Declarative Programming
Favicon
Maximize a Performance dos Seus Testes com Jest e Vitest
Favicon
Introduction to Jest: Unit Testing, Mocking, and Asynchronous Code

Featured ones: