How to install Jest and React Testing Library in TypeScript

0 votes
Can someone explain to me the commands for how to install Jest and React Testing Library in TypeScript?
Mar 6 in Java-Script by Nidhi
• 12,580 points
54 views

1 answer to this question.

0 votes

To install and configure Jest and React Testing Library in a TypeScript project, follow these steps:

1. Install Dependencies

npm install --save-dev jest @types/jest @testing-library/react @testing-library/jest-dom ts-jest

2. Configure Jest

import type { Config } from '@jest/types';

const config: Config.InitialOptions = {

  preset: 'ts-jest', // Use ts-jest for TypeScript

  testEnvironment: 'jsdom', // Simulate a browser environment

  setupFilesAfterEnv: ['@testing-library/jest-dom/extend-expect'], // Add jest-dom extensions

  moduleNameMapper: {

    '^@/(.*)$': '<rootDir>/src/$1', // Map aliases (if using path aliases)

  },

};

export default config;

3. Update tsconfig.json{

  "compilerOptions": {

    "types": ["jest", "@testing-library/jest-dom"]

  }

}

4. Write a Test

import React from 'react';

import { render, screen } from '@testing-library/react';

import App from './App';

test('renders learn react link', () => {

  render(<App />);

  const linkElement = screen.getByText(/learn react/i);

  expect(linkElement).toBeInTheDocument();

});

5. Add a Test Script

{

  "scripts": {

    "test": "jest"

  }

}

6. Run Tests

npm test

answered Mar 6 by Anvi

Related Questions In Java-Script

0 votes
1 answer

How to do API call in react js?

Hello, Use fetch method inside componentDidMount to update state: componentDidMount(){ fetch('https://dey.me/api/') ...READ MORE

answered May 18, 2020 in Java-Script by Niroj
• 82,840 points
2,443 views
0 votes
1 answer

How to use Redirect in the new react-router-dom of Reactjs?

Hello, To navigate to another component you can ...READ MORE

answered May 18, 2020 in Java-Script by Niroj
• 82,840 points
8,984 views
0 votes
1 answer

How to create dynamic href in react render function?

Hello @kartik, Use string concatenation: href={'/posts/' + post.id} The JSX ...READ MORE

answered May 18, 2020 in Java-Script by Niroj
• 82,840 points
11,745 views
0 votes
1 answer

How to retrieve config and env variable in vue component?

Hello @kartik, To access your env variables in ...READ MORE

answered Jun 11, 2020 in Java-Script by Niroj
• 82,840 points
11,279 views
0 votes
1 answer
0 votes
1 answer

How to create a mat table in Angular?

1. Install Angular Material ng add @angular/material 2. Import ...READ MORE

answered Mar 6 in Angular by Anvi
74 views
0 votes
1 answer

Cannot find name webgl object TypeScript issue TS2304.

The error Cannot find name 'WebGLObject'. TS2304 ...READ MORE

answered Mar 6 in Angular by Anvi
108 views
0 votes
1 answer

How to get last visited page URL in React?

To get the last visited page URL ...READ MORE

answered Mar 6 in Angular by Anvi
63 views
0 votes
1 answer
0 votes
1 answer

How to create custom pagination in JavaScript?

It enhances user experience by dividing large ...READ MORE

answered Dec 24, 2024 in Java-Script by Navya
117 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP