Component Testing Guide
This guide outlines how to test components in our hybrid Astro/React architecture.Table of Contents
- Overview
- Testing React Components
- Testing Astro Components
- Testing Converted Components
- Best Practices
Overview
Our testing strategy handles both React components and Astro components:- React components: Tested using Vitest and @testing-library/react
- Astro components: Tested using a combination of unit tests for React parts and integration tests with Playwright
Testing React Components
For standard React components, we follow traditional React testing patterns:Testing Astro Components
Astro components require a different approach since they’re primarily server-rendered:Unit Testing React Parts
If your Astro component wraps a React component with client-side hydration:- Test the underlying React component directly
- Use integration tests for the full Astro component
Integration Tests with Playwright
For full Astro component testing:Testing Converted Components
When testing components that have been converted from React to Astro using our wrapper pattern:1. Update Test Imports
Update your test imports to point to the React version of the component:2. Update Component References
Make sure to update component references in your tests:3. Add Integration Tests for Astro Wrappers
Best Practices
Testing Strategy
- Unit test React components for logic, rendering, and interactions
- Integration test Astro components for full rendering, hydration, and end-user interactions
- Mock API calls and services to focus on component behavior
Test Location
- Place tests for React components in
__tests__directory next to the component - Place Playwright integration tests in
tests/integration
Test Coverage
Aim for:- High coverage of React component logic
- Focused integration tests for Astro components
- Complete coverage of critical user flows
Handling Astro-Specific Features
When testing components that use Astro-specific features:Slots
For components that use Astro’s slot system, test the underlying React components with children props:Client Directives
When testing React components that would be hydrated with client directives in Astro:- Test the React component directly
- Use Playwright to test the hydration behavior in integration tests
Example: Testing Converted Analytics Dashboard
Troubleshooting
Common Testing Issues
- Component not hydrating in tests: When testing React components that are wrapped by Astro, make sure you’re testing the React component directly.
- Props mismatch: If a component was converted and props were changed, ensure test data is updated accordingly.
- Styles missing in tests: Remember that Astro’s scoped styles won’t be applied in React component tests. Focus on functionality over styling in unit tests.
- Hydration errors in integration tests: Check that your Astro component is using the correct client directive for the React component it wraps.