# Testing Standards — {{projectName}} > **Scope:** All testing activities | **Loaded On-Demand** --- ## Testing Philosophy {{#if testingPhilosophy}} {{testingPhilosophy}} {{else}} - **Tests are documentation** — Readability matters - **Fast feedback** — Unit tests should run in < 1 second - **Test behavior, not implementation** — Black-box over white-box - **Arrange-Act-Assert** — Clear test structure - **One assertion per test** — When possible {{/if}} --- ## Test Structure ### File Organization {{#if testStructure}} {{testStructure}} {{else}} ``` src/ ├── features/auth/ │ ├── auth.service.ts │ ├── auth.service.test.ts # Unit tests │ ├── auth.controller.ts │ └── auth.controller.test.ts # Integration tests ├── __tests__/ │ ├── e2e/ # End-to-end tests │ └── fixtures/ # Test data, factories └── test/ ├── setup.ts # Global test setup └── teardown.ts # Global test teardown ``` {{/if}} ### Test File Naming {{#if testNaming}} {{testNaming}} {{else}} - Unit tests: `*.test.ts` or `*.spec.ts` - E2E tests: `*.e2e.test.ts` - Co-locate tests with source code {{/if}} --- ## Unit Tests ### What to Test {{#if unitTestScope}} {{unitTestScope}} {{else}} - **Business logic** — Pure functions, services - **Utilities** — Helpers, formatters - **Components** — React/Vue/Svelte components - **Hooks** — Custom React hooks ### What NOT to Test - Third-party libraries (trust them) - Implementation details (private methods) - Trivial getters/setters {{/if}} ### Test Template ```typescript describe('{{feature}}', () => { describe('{{scenario}}', () => { it('{{expected outcome}}', () => { // Arrange const input = { // setup }; // Act const result = doSomething(input); // Assert expect(result).toBe(expected); }); }); }); ``` ### Coverage Requirements {{#if coverage}} {{coverage}} {{else}} - Minimum: {{coverage.min}}% overall - Critical paths: 100% - New code: 100% before merge {{/if}} --- ## Integration Tests ### Scope {{#if integrationScope}} {{integrationScope}} {{else}} - API endpoints with real database (in-memory) - Database interactions and migrations - Authentication/authorization flows - External service integrations (mocked) {{/if}} ### Database Testing {{#if dbTesting}} {{dbTesting}} {{else}} - Use in-memory database for tests - Migrate up/down in `beforeAll`/`afterAll` - Seed test data in `beforeEach` - Clean tables in `afterEach` {{/if}} --- ## End-to-End Tests ### Framework {{#if e2eFramework}} {{e2eFramework}} {{else}} - Use {{e2eTool}} (Playwright/Cypress) - Test critical user journeys - Run in CI before deployment {{/if}} ### E2E Test Checklist {{#if e2eChecklist}} {{e2eChecklist}} {{else}} {{#each e2eScenarios}} - {{this}} {{/each}} {{/if}} --- ## Test Data Management ### Factories & Fixtures {{#if testFactories}} {{testFactories}} {{else}} Use factories for test data: ```typescript // test/factories/user.factory.ts export const userFactory = (overrides = {}) => ({ id: 'user-1', email: 'test@example.com', name: 'Test User', role: 'user', ...overrides, }); ``` {{/if}} ### Seeding Strategy {{#if seedingStrategy}} {{seedingStrategy}} {{else}} - Use deterministic IDs - Clean database between tests - Use transactions when possible {{/if}} --- ## Mocking & Stubbing ### Mocking Guidelines {{#if mockingGuidelines}} {{mockingGuidelines}} {{else}} - Mock external services only - Prefer real implementations over mocks - Clear mocks in `afterEach` - Verify mock calls when relevant {{/if}} ### Example ```typescript jest.mock('./external-api', () => ({ getData: jest.fn().mockResolvedValue({ data: 'mock' }), })); // In test await expect(action()).resolves.toEqual(expected); expect(externalApi.getData).toHaveBeenCalledWith(expectedArgs); ``` --- ## Performance Tests {{#if performanceTests}} {{performanceTests}} {{else}} - Load test API endpoints (k6, artillery) - Benchmark critical functions - Test database query performance - Set performance budgets in CI {{/if}} --- ## Security Tests {{#if securityTests}} {{securityTests}} {{else}} - Test authentication bypass attempts - Test authorization checks - Test input validation (SQL injection, XSS) - Test rate limiting - Run SAST/DAST scans in CI {{/if}} --- ## Test Commands {{#if testCommands}} {{testCommands}} {{else}} ```bash # Run all tests npm test # Run with coverage npm test -- --coverage # Run watch mode npm test -- --watch # Run specific test file npm test -- auth.service.test # Run E2E tests npm run test:e2e # Run performance tests npm run test:performance ``` {{/if}} --- ## CI/CD Integration {{#if ciTesting}} {{ciTesting}} {{else}} ### Required Checks - Unit tests pass - Coverage threshold met - E2E tests pass (on main branch) - Security scan passes - Linting passes ### Test Results - Upload coverage reports - Visualize test trends - Report flaky tests {{/if}} --- > **Token Budget:** ~1000 tokens max > **Loaded On-Demand** — Only when working on tests