CodebaseExamples
__tests__
Integration tests that verify the example code works correctly and demonstrates proper API usage patterns. These tests catch breaking changes before users encou
Example Tests
Integration tests that verify the example code works correctly and demonstrates proper API usage patterns. These tests catch breaking changes before users encounter them.
Why Test Examples?
- Examples stay working - Refactoring or API changes that break examples are caught immediately
- Examples serve as integration tests - They test real usage patterns, not just isolated units
- Documentation stays accurate - Examples are validated as correct usage patterns
- User confidence - Users can trust that examples actually work
Test Structure
basic-usage.test.ts
Tests the basic usage examples:
simple-geocoding.ts- Minimal setup without cachingwith-caching.ts- Cache performance demonstrationbatch-geocoding.ts- Processing multiple addresses
Verifies:
- Examples execute without errors
- Results have expected structure
- Cache behavior works as documented
- Error handling works correctly
custom-adapter.test.ts
Tests the custom adapter examples:
fake-geocoding-service.ts- Creating test doublesredis-cache-example.ts- Implementing custom cache adapters
Verifies:
- Adapters implement the correct port interfaces
- Custom implementations work with use cases
- Examples demonstrate proper patterns
extracting-domain.test.ts
Tests the domain extraction examples:
express-api-example.ts- Using domain in Express.jscli-tool-example.ts- Using domain in CLI tools
Verifies:
- Examples run without errors
- Documentation provides necessary information
- Examples mention key integration points
Running Tests
# Run all example tests
bun run test:examples
# Run specific test file
bun run test examples/__tests__/basic-usage.test.ts
# Run with coverage
bun run test --coverage examples/__tests__/
Writing New Example Tests
When adding a new example:
- Create a test that verifies it runs without errors
- Test the key functionality it demonstrates
- Verify error handling if applicable
- Keep tests focused on integration, not implementation details
Example pattern:
describe("New Example", () => {
test("should demonstrate key feature", async () => {
// Setup using same pattern as example
const service = createFakeGeocodingService();
const cache = createInMemoryCache();
const useCase = createLookupAddressUseCase(service, cache);
// Execute
const result = await useCase.execute(address);
// Verify key behavior
expect(result).toBeDefined();
expect(result.municipality.name).toBeDefined();
});
});
Benefits
- Breaking change detection - API changes that break examples are caught in CI
- Living documentation - Examples are continuously validated
- User confidence - Users can trust examples actually work
- Integration testing - Tests real usage patterns across multiple modules
- Refactoring safety - Can refactor with confidence examples still work
CI Integration
These tests run as part of the standard test suite. Add to CI with:
- name: Test examples
run: bun run test:examples