vite react project typescript need testing. Add and configure a test runner: Jest or Vitest. Test runner should show the test coverage. You should aim to reach at least 80% of the test coverage.
Add a testing library: React Testing Library. You should add tests for the several scenarios keeping in mind that mocked data should be used instead of real API calls.
Tests for the Card List component:
Verify that the component renders the specified number of cards;
Check that an appropriate message is displayed if no cards are present.
Tests for the Card component:
Ensure that the card component renders the relevant card data;
Validate that clicking on a card opens a detailed card component;
Check that clicking triggers an additional API call to fetch detailed information.
Tests for the Detailed Card component:
Check that a loading indicator is displayed while fetching data;
Make sure the detailed card component correctly displays the detailed card data;
Ensure that clicking the close button hides the component.
Tests for the Pagination component:
Make sure the component updates URL query parameter when page changes.
Tests for the Search component:
Verify that clicking the Search button saves the entered value to the local storage;
Check that the component retrieves the value from the local storage upon mounting.
Lastly, update Husky to run tests on the pre-push hook, ensuring that tests are automatically executed before any code is pushed.
Assistant
To achieve the desired setup and testing for your Vite + React + TypeScript project, follow these steps:
1. Setting Up the Test Environment
1.1 Installing Jest or Vitest
For this example, we'll use Jest. The following steps are required to set up Jest with Vite:
Create a Pagination.test.tsx file under src/components/Pagination/__tests__/:
tsx
import React from 'react';
import { render, screen, fireEvent } from '@testing-library/react';
import { BrowserRouter as Router } from 'react-router-dom';
import Pagination from '../Pagination'; // adjust the import
test('updates URL query parameter when page changes', () => {
render(
<Router>
<Pagination total={100} />
</Router>
);
const nextPageButton = screen.getByText(/next/i);
fireEvent.click(nextPageButton);
expect(window.location.search).toContain('page=2');
});
2.5 Search Component
Create a Search.test.tsx file under src/components/Search/__tests__/:
tsx
import React from 'react';
import { render, screen, fireEvent } from '@testing-library/react';
import Search from '../Search';
test('saves search value to local storage', () => {
render(<Search />);
const inputElement = screen.getByPlaceholderText(/search/i);
fireEvent.change(inputElement, { target: { value: 'test query' } });
const buttonElement = screen.getByText(/search/i);
fireEvent.click(buttonElement);
expect(localStorage.getItem('searchQuery')).toBe('test query');
});
test('retrieves search value from local storage on mount', () => {
localStorage.setItem('searchQuery', 'stored query');
render(<Search />);
const inputElement = screen.getByPlaceholderText(/search/i);
expect(inputElement.value).toBe('stored query');
});
3. Setting Up Husky
3.1 Installing Husky
bash
npx husky-init && npm install
3.2 Adding Pre-Push Hook
Edit the .husky/pre-push file to run tests:
bash
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
npm test
This ensures tests run automatically before any code is pushed to the repository.
4. Final Thoughts
Follow the steps above to configure testing with Jest, testing several scenarios with React Testing Library and ensuring a robust pipeline with Husky. Maintain at least 80% test coverage, mock necessary data, and execute tests efficiently.
Run your tests with the following command:
bash
npm test
Or watch mode with:
bash
npm test -- --watch
By setting up everything as described, you should have a solid foundation for maintaining high code quality through comprehensive testing.