- Unit Testing: Testing one unit or one component in isolation, is known as Unit Testing.
- Integration Testing: Integration Testing the integration of the component.
- End to End Testing - e2e testing: Testing a react application as soon as user lands on the website to the user leaves the website, is known as End to End(e2e) Testing.
- Install React Testing Library
- npm install --save-dev @testing-library/react @testing-library/dom
- Installed jest
- Installed Babel dependencies
- Configure Babel
- Configure Parcel Config file to disable default babel transpilation
- Jest - npx jest --init
- Install jsdom library
- Install @babel/preset-react - to make JSX work in test cases
- Include @babel/preset-react inside my babel config
- npm i -D @testing-library/jest-dom
-
React Testing Librarybuids on top ofDOM Testing Libraryby adding APIs for working with React components. -
React Testing LibraryusesJestbehind the scence. -
Jestis a delightful JavaScript Testing Framework with a focus on simplicity.It works with projects using: Babel, TypeScript, Node, React, Angular, Vue and more! -
Install
React Testing Librarynpm install -D @testing-library/react
-
Install
Jestnpm install -D jest
-
When you use
Babel, you should these dependencies alsonpm install --save-dev babel-jest @babel/core @babel/preset-env
-
Configure
Babelto target your current version of Node by creating a babel.config.js file in the root of your project:// babel.config.js module.exports = { presets: [["@babel/preset-env", { targets: { node: "current" } }]], };
-
Configure Parcel Config file(
.parcelrc) to disable default babel transpilation// .parcelrc { "extends": "@parcel/config-default", "transformers": { "*.{js,mjs,jsx,cjs,ts,tsx}": [ "@parcel/transformer-js", "@parcel/transformer-react-refresh-wrap" ] } }
-
Run the test case: Command to run test cases
npm run test -
Jest Configuration: Initialize the Jest, it will create new Jest Configuration file(
jest.config.js)npx jest --init
-
Install
jsdomLibrarynpm install --save-dev jest-environment-jsdom
-
Install
@babel/preset-reactto make JSX work in test casesnpm install @babel/preset-react
-
Include
@babel/preset-reactinside mybabel.config.jsfile -@babel/preset-reacthelping our testing library to convert JSX code to HTML, so it can read properly// babel.config.js module.exports = { presets: [ ["@babel/preset-env", { targets: { node: "current" } }], ["@babel/preset-react", { runtime: "automatic" }], ], };
-
Install
@testing-library/jest-domnpm install -D @testing-library/jest-dom
-
Create test file inside
__test__flder -
These file are considered as test file -
**/__tests__/**/*.[jt]s?(x), **/?(*.)+(spec|test).[tj]s?(x) -
__is known asDunder Tests.
fileName.test.js
fileName.test.ts
fileName.spec.js
fileName.spec.ts