Create tests with mocks, validate behavior, and improve coverage.
When to activate this skill:
| Trigger Pattern | Example Request |
|---|---|
| Keywords: test, coverage, spec, mock | "Write tests for UserService" |
| "add...tests" | "Add tests for the new API" |
| "test...coverage" | "Improve test coverage" |
| "create...spec" | "Create a spec for this function" |
| Auto-chain from coding.skill | After code implementation |
What this skill can do:
context.md - For project context.github/copilot/docs/testing.md - For test strategycoding.skill - Chains from after implementation.github/skills/index.yaml - To verify specialized test skill coverageThis core testing skill covers generic unit/integration/component/e2e workflows.
Before executing, run a specialization check:
1. Extract requested test subtype from the user prompt
2. Check if subtype is explicitly covered in this skill
3. Check if a dedicated skill already exists in .github/skills/index.yaml
4. If subtype is specialized and uncovered:
- STOP direct test generation
- Trigger missing-skill generation via setup workflow
- Create/register dedicated skill (example: test-contract/SKILL.md)
- Re-route execution to that new skill
Specialized subtypes that require dedicated skills when not already covered:
1. Read .github/copilot/docs/testing.md for:
- Test framework (Jest, Vitest, pytest, etc.)
- Coverage requirements
- Test structure conventions
2. Read existing tests to match patterns
| Code Type | Test Type | Priority |
|---|---|---|
| Functions | Unit tests | High |
| API endpoints | Integration tests | High |
| Components | Component tests | Medium |
| Utilities | Unit tests | Medium |
| UI flows | E2E tests | Low |
┌─────────────────────────────────────────────────────────────────────────┐
│ MANDATORY: USE MOCKING DATA │
│ │
│ ALL tests MUST use mocked data: │
│ • Mock external API calls │
│ • Mock database operations │
│ • Mock file system operations │
│ • Mock time-dependent functions │
│ │
│ NEVER make real external calls in tests │
└─────────────────────────────────────────────────────────────────────────┘
// [language: TypeScript/Jest example]
describe('[ModuleName]', () => {
// Setup
beforeEach(() => {
// Reset mocks
});
describe('[functionName]', () => {
it('should [expected behavior] when [condition]', () => {
// Arrange
const mockData = { /* mocked input */ };
// Act
const result = functionName(mockData);
// Assert
expect(result).toEqual(expected);
});
it('should handle [edge case]', () => {
// Test edge case
});
it('should throw when [error condition]', () => {
// Test error handling
});
});
});
describe('[API Endpoint]', () => {
// Mocked dependencies
let mockDb: MockDatabase;
let mockAuth: MockAuthService;
beforeAll(() => {
mockDb = createMockDatabase();
mockAuth = createMockAuth();
});
afterEach(() => {
mockDb.reset();
});
describe('GET /api/[resource]', () => {
it('should return [expected] when authenticated', async () => {
// Arrange
mockAuth.setUser({ id: 'test-user' });
mockDb.seed([{ /* test data */ }]);
// Act
const response = await request(app).get('/api/resource');
// Assert
expect(response.status).toBe(200);
expect(response.body).toMatchObject({ /* expected */ });
});
it('should return 401 when not authenticated', async () => {
mockAuth.setUser(null);
const response = await request(app).get('/api/resource');
expect(response.status).toBe(401);
});
});
});
Create reusable mock data:
// tests/factories/user.factory.ts
export const createMockUser = (overrides = {}) => ({
id: 'test-user-id',
email: '[email protected]',
name: 'Test User',
createdAt: new Date('2024-01-01'),
...overrides
});
// tests/mocks/database.mock.ts
export const createMockDatabase = () => ({
users: [],
seed: function(data) { this.users = data; },
reset: function() { this.users = []; },
findUser: function(id) { return this.users.find(u => u.id === id); }
});
┌─────────────────────────────────────────────────────────────────────────┐
│ TEST COVERAGE TARGETS │
│ │
│ Minimum coverage (unless project specifies otherwise): │
│ • Statements: 80% │
│ • Branches: 70% │
│ • Functions: 80% │
│ • Lines: 80% │
│ │
│ Critical paths (auth, payments, etc.): 90%+ │
└─────────────────────────────────────────────────────────────────────────┘
| Framework | Convention |
|---|---|
| Jest/Vitest | *.test.ts or *.spec.ts |
| pytest | test_*.py or *_test.py |
| Go | *_test.go |
| Rust | #[test] in same file or tests/ |
After creating tests:
1. Run test suite
2. Verify all tests pass
3. Check coverage meets requirements
4. Report results
Before completing:
Return to orchestrator: