This project demonstrates unit testing and integration testing in React using Jest and React Testing Library, with support for TypeScript, custom hooks, MSW (Mock Service Worker) for mocking API calls, and code formatting/linting using ESLint and Prettier.
# React Testing Library and Jest DOM
npm install --save-dev @testing-library/react @testing-library/jest-dom
# or
yarn add -D @testing-library/react @testing-library/jest-dom
# User events support
npm install --save-dev @testing-library/user-event
# or
yarn add -D @testing-library/user-event
# Jest types for TypeScript support
npm install --save-dev @types/jestnpm test
# or
npm run testRun tests interactively:
yarn test:watch
# or
npm run test:watchWatch mode will re-run tests related to changed files. You can press:
- w to show more options
- a to run all tests
- p to run by file name pattern
- test.only β run a specific test
- test.skip β skip a specific test
- describe.only β run only a specific test suite
- describe.skip β skip a test suite
- fit β alias for test.only
- xit β alias for test.skip
# Install ESLint plugin for Jest
npm install --save-dev eslint-plugin-jest
# Install and configure Prettier
npm install --save-dev --save-exact prettier
# Install Prettier config to avoid conflicts with ESLint
npm install --save-dev eslint-config-prettier
# or
yarn add -D eslint-config-prettiernpm run lint && npm run format
# or
yarn lint && yarn format"coverage": "jest --coverage"npm run coverage
npm test --coverage
npm run test -- --coverage --watchAll"coverage": "jest --coverage --watchAll --collectCoverageFrom='src/components/**/*.{ts,tsx}'""collectCoverageFrom": [
"src/components/**/*.{ts,tsx}",
"!**/*.{test,spec}.{ts,tsx}",
"!**/__tests__/**"
]Mock Service Worker is used to intercept and mock network requests in tests and development.
- Official site: https://mswjs.io
npm install --save-dev msw- Handlers are defined in src/mocks/handlers.ts
- Requests are intercepted using msw/node in test environment
npm list mswnpm install --save-dev whatwg-fetch
# or
yarn add whatwg-fetch --dev``md
| Method | Use Case |
|---|---|
getBy... |
Synchronous. Throws error if element not found |
queryBy... |
Synchronous. Returns null if not found |
findBy... |
Asynchronous. Returns a Promise |
- Use render with wrapper for custom context providers
- Use renderHook from @testing-library/react-hooks (or vitest equivalent) to test custom hooks
- act β to wrap updates that trigger re-renders or effects
- jest.fn() β to mock functions
- jest.spyOn() β to spy on real implementations
npx husky-init
npm installBy default, this sets up a pre-commit hook that runs:
npm testInstead of just running tests, you can also run lint and format:
# Replace the contents of .husky/pre-commit
npm run lint && npm run format
# or
yarn lint && yarn formatAdd pre-push hook that runs tests:
npx husky add .husky/pre-push "npm test -- --watchAll=false"To run linters and formatters only on staged files, install and configure lint-staged:
npm install --save-dev lint-stagedExample package.json config:
"lint-staged": {
"**/*.{js,ts,tsx}": [
"eslint --fix",
"prettier --write"
]
}- Testing Playground is a helpful online tool to inspect DOM elements and generate selectors for React Testing Library. You can copy the query code it suggests directly into your tests.
- A great tool to inspect DOM and generate queries for RTL
This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.
Currently, two official plugins are available:
- @vitejs/plugin-react uses Babel for Fast Refresh
- @vitejs/plugin-react-swc uses SWC for Fast Refresh
If you are developing a production application, we recommend updating the configuration to enable type-aware lint rules:
export default tseslint.config([
globalIgnores(['dist']),
{
files: ['**/*.{ts,tsx}'],
extends: [
// Other configs...
// Remove tseslint.configs.recommended and replace with this
...tseslint.configs.recommendedTypeChecked,
// Alternatively, use this for stricter rules
...tseslint.configs.strictTypeChecked,
// Optionally, add this for stylistic rules
...tseslint.configs.stylisticTypeChecked,
// Other configs...
],
languageOptions: {
parserOptions: {
project: ['./tsconfig.node.json', './tsconfig.app.json'],
tsconfigRootDir: import.meta.dirname,
},
// other options...
},
},
])You can also install eslint-plugin-react-x and eslint-plugin-react-dom for React-specific lint rules:
// eslint.config.js
import reactX from 'eslint-plugin-react-x'
import reactDom from 'eslint-plugin-react-dom'
export default tseslint.config([
globalIgnores(['dist']),
{
files: ['**/*.{ts,tsx}'],
extends: [
// Other configs...
// Enable lint rules for React
reactX.configs['recommended-typescript'],
// Enable lint rules for React DOM
reactDom.configs.recommended,
],
languageOptions: {
parserOptions: {
project: ['./tsconfig.node.json', './tsconfig.app.json'],
tsconfigRootDir: import.meta.dirname,
},
// other options...
},
},
])