
ReactJS
Write maintainable, effective tests.
// GOOD: Tests visible behavior
test('shows clicked message', async () => {
render(<Button />);
await userEvent.click(screen.getByRole('button'));
expect(screen.getByText('Clicked!')).toBeInTheDocument();
});// GOOD: Semantic query
const button = screen.getByRole('button', { name: /submit/i });
// AVOID: CSS selectors
const button = document.querySelector('.submit-btn');test('user can login', async () => {
render(<LoginPage />);
await userEvent.type(
screen.getByLabelText('Email'),
'user@example.com'
);
await userEvent.type(
screen.getByLabelText('Password'),
'password'
);
await userEvent.click(screen.getByRole('button'));
expect(await screen.findByText('Welcome!')).toBeInTheDocument();
});// GOOD: Describes what is tested
test('displays user name from props', () => {});
test('submits form with correct data', () => {});
// AVOID: Vague names
test('works', () => {});// GOOD: One focus per test
test('renders loading state', () => {
render(<DataLoader isLoading={true} />);
expect(screen.getByText('Loading...')).toBeInTheDocument();
});jest.mock('./api');
test('handles API error', async () => {
fetchUser.mockRejectedValue(new Error('Network error'));
render(<UserProfile />);
expect(await screen.findByText('Network error')).toBeInTheDocument();
});Resources
Ojasa Mirai
Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.
Learn Deep โข Build Real โข Verify Skills โข Launch Forward