Astro Integration Testing Guide
This document outlines our approach to integration testing for Astro components and pages, focusing on testing the interaction between components, SSR functionality, client-side hydration, and static generation.Testing Infrastructure
Our integration tests are built using:- Playwright for browser automation and testing
- Vitest for test organization and assertions
- GitHub Actions for CI/CD pipeline integration
Test Categories
The integration tests are organized into four main categories:1. Layout Component Integration
These tests focus on how our layout components work together with other components:- Main and Dashboard Layouts: Testing that layouts render correctly with expected child components
- Error Boundary: Testing that the ErrorBoundary component catches errors and displays appropriate messages
- Client Router: Testing that client-side navigation works correctly without full page reloads
2. SSR Functionality
These tests verify that our Astro pages render correctly on the server before any JavaScript hydration:- Pre-rendered HTML: Testing pages with JavaScript disabled to ensure they still function
- Meta Tags Generation: Testing that meta tags are correctly generated for SEO
- Dynamic Routes: Testing that dynamic routes render with the correct data
- Client Router: Testing that Astro’s Client Router feature works correctly
3. Component Hydration
These tests focus on interactive components that use Astro’s client directives:- Theme Toggle: Testing that the theme toggle component hydrates and functions correctly
- Form Validation: Testing that form submission and validation works correctly
- Interactive Components: Testing AIChat, AdminDashboard, and other interactive components
- Modal Components: Testing that modal components with client:visible directive work correctly
4. Static Generation
These tests verify that our content collections and static generation features work correctly:- Content Collections: Testing that blog posts and other content are correctly generated
- Tag Filtering: Testing that tag-based filtering works correctly
- RSS and Sitemap: Testing that RSS feeds and sitemaps are correctly generated
- Syntax Highlighting: Testing that code blocks have proper syntax highlighting
Running the Tests
Local Development
To run the integration tests locally:CI/CD Pipeline
The integration tests are automatically run in our CI/CD pipeline:- On pull requests to verify changes don’t break existing functionality
- Before deployment to staging to verify the build is stable
- After deployment to production as a smoke test to verify the deployment
Test Best Practices
When writing integration tests:- Focus on user flows: Test how users interact with the application rather than implementation details
- Use data attributes for selection: Use
data-testidattributes when appropriate to make tests more resilient to UI changes - Test without JavaScript: Always include tests with JavaScript disabled to ensure SSR works correctly
- Test across viewports: Include tests for mobile, tablet, and desktop viewports
- Minimize test duplication: Use test helpers and setup functions to avoid duplicating code
- Keep tests independent: Each test should be able to run independently of other tests
Debugging Failed Tests
When tests fail:- Check the screenshots: Playwright automatically captures screenshots of failed tests in the
test-resultsdirectory - Run with
--headed: Run the test with a visible browser to see what’s happening - Add
page.pause(): Insertawait page.pause()in your test to pause at a specific point and use the Playwright Inspector - Check the logs: Check the test logs for any error messages or console logs
Adding New Tests
When adding new integration tests:- Create a new file: Add a new
.spec.tsfile in the appropriate directory - Focus on a specific feature: Each test file should focus on a specific feature or component
- Follow the existing patterns: Use the existing test files as examples
- Run the tests locally: Verify that your tests pass locally before pushing changes
- Add to CI pipeline: Make sure your tests are included in the CI pipeline
Current Status
We currently have integration tests for:- Layout component integration
- SSR functionality
- Component hydration
- Static generation
- Expanding cross-browser compatibility tests
- Adding more mobile-specific tests
- Implementing performance benchmarking tests