apps/admin/test-utils/analytics/README.md
This directory contains utilities to improve test reliability, maintainability, and consistency across the stats app test suite.
test-helpers.tsMain export file that provides backward compatibility and re-exports all testing utilities.
date-testing-utils.tsPurpose: Eliminate date-related test flakiness
Key Features:
2024-01-15T12:00:00.000Z) for consistent test behavior@tryghost/shade date functionsUsage:
import { setupDateMocking, getExpectedDateRange } from '@test-utils/analytics/test-helpers';
beforeEach(() => {
const dateMocking = setupDateMocking();
});
// Use consistent date ranges in assertions
const { expectedDateFrom, expectedDateTo } = getExpectedDateRange(30);
mock-factories.tsPurpose: Builder pattern for test data creation
Key Features:
MockPostBuilder - Flexible post creation with sensible defaultsMockStatsBuilder - Statistics data with configurable valuesMockApiResponseBuilder - Standard API response shapesUsage:
import { MockPostBuilder, createMockPost } from '@test-utils/analytics/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.tsPurpose: Reduce boilerplate in hook testing
Key Features:
createStandardApiMock() - Complete API mock setupcreateStandardHookTestSuite() - Generate common test patternssetupCommonHookMocks() - Standard dependency mockingUsage:
import { createStandardHookTestSuite } from '@test-utils/analytics/test-helpers';
const testSuite = createStandardHookTestSuite(
'useMyHook',
useMyHook,
mockApiCall,
{ hasRange: true, hasOrder: true, hasShouldFetch: true }
);
testSuite.forEach(({name, test}) => {
it(name, test);
});
All tests now pass consistently: