You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
3 months ago | |
|---|---|---|
| .. | ||
| README.md | 3 months ago | |
| date-testing-utils.ts | 3 months ago | |
| hook-testing-utils.ts | 3 months ago | |
| mock-factories.ts | 3 months ago | |
| test-helpers.ts | 3 months ago | |
| tinybird-helpers.ts | 3 months ago | |
README.md
Testing Utilities
This directory contains utilities to improve test reliability, maintainability, and consistency across the stats app test suite.
Files Overview
test-helpers.ts
Main export file that provides backward compatibility and re-exports all testing utilities.
date-testing-utils.ts
Purpose: Eliminate date-related test flakiness
Key Features:
- Fixed date (
2024-01-15T12:00:00.000Z) for consistent test behavior - Mock system date to prevent timing issues
- Date range calculation utilities that work reliably
- Mocking utilities for
@tryghost/shadedate functions
Usage:
import { setupDateMocking, getExpectedDateRange } from '../../utils/test-helpers';
beforeEach(() => {
const dateMocking = setupDateMocking();
});
// Use consistent date ranges in assertions
const { expectedDateFrom, expectedDateTo } = getExpectedDateRange(30);
mock-factories.ts
Purpose: Builder pattern for test data creation
Key Features:
MockPostBuilder- Flexible post creation with sensible defaultsMockStatsBuilder- Statistics data with configurable valuesMockApiResponseBuilder- Standard API response shapes- Quick factory functions for common scenarios
Usage:
import { MockPostBuilder, createMockPost } from '../../utils/test-helpers';
// Builder pattern for complex data
const post = new MockPostBuilder()
.withId('test-123')
.withAuthors([{name: 'Test Author'}])
.withoutEmail()
.build();
// Quick factories for simple cases
const simplePost = createMockPost({ id: 'simple-123' });
hook-testing-utils.ts
Purpose: Reduce boilerplate in hook testing
Key Features:
createStandardApiMock()- Complete API mock setupcreateStandardHookTestSuite()- Generate common test patternssetupCommonHookMocks()- Standard dependency mocking- Test generators for parameters, shouldFetch, loading/error states
Usage:
import { createStandardHookTestSuite } from '../../utils/test-helpers';
const testSuite = createStandardHookTestSuite(
'useMyHook',
useMyHook,
mockApiCall,
{ hasRange: true, hasOrder: true, hasShouldFetch: true }
);
testSuite.forEach(({name, test}) => {
it(name, test);
});
Migration Benefits
Before (Problems)
- Date Flakiness: Tests failed randomly based on execution time
- Verbose Setup: Each test repeated 50+ lines of mock configuration
- Inconsistent Patterns: Different approaches across test files
- Maintenance Burden: Changes required updates in multiple files
After (Solutions)
- Reliable Dates: Fixed dates eliminate timing-dependent failures
- Reduced Boilerplate: Standard patterns generate common test cases
- Consistent Mocking: Reusable builders for complex data structures
- Maintainable: Changes in one place affect all tests
Test Results
All tests now pass consistently:
- 228 tests passing (0 failures)
- Hook coverage: 97.68% (up from ~75%)
- Utils coverage: 100%
- Reliable date handling across all tests
Future Improvements
- Integration Tests: Add utilities for component integration testing
- Performance Tests: Add utilities for testing hook performance
- Accessibility Tests: Add utilities for a11y testing
- Visual Tests: Add utilities for snapshot testing
Usage Guidelines
- Use fixed dates for any time-dependent tests
- Use builders for complex mock data that varies between tests
- Use factories for simple mock data that's consistent
- Use generators for standard hook test patterns
- Migrate gradually - existing tests continue to work while new tests adopt these patterns