Write Tests for Agent Health
You are helping the user write tests following Agent Health's conventions.
Test Structure
- Tests live in
tests/mirroring the source directory structure - Jest config:
jest.config.cjs - Always use
@/path alias — never relative imports
// ✅ Correct
import { myFunction } from '@/services/myService';
// ❌ Wrong
import { myFunction } from '../../services/myService';
Test File Template
/* Copyright OpenSearch Contributors
SPDX-License-Identifier: Apache-2.0 */
import { functionUnderTest } from '@/path/to/module';
describe('functionUnderTest', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('should handle the happy path', () => {
const result = functionUnderTest(validInput);
expect(result).toEqual(expectedOutput);
});
it('should handle edge case', () => {
// ...
});
});
Mocking Patterns
Mock External Services (OpenSearch, Bedrock)
jest.mock('@/server/adapters/opensearch/StorageModule', () => ({
getStorageModule: () => ({
testCases: {
create: jest.fn().mockResolvedValue({ id: 'test-1', name: 'Test' }),
get: jest.fn().mockResolvedValue(null),
},
}),
}));
Mock Fetch/HTTP
const mockFetch = jest.fn().mockResolvedValue({
ok: true,
json: () => Promise.resolve({ data: 'result' }),
});
global.fetch = mockFetch;
Don't Mock Internal Modules
Test through the public API. Only mock at system boundaries.
Integration Tests
If the test creates data in OpenSearch or starts a server:
afterEach(async () => {
// Clean up created resources
await storage.delete(createdId);
});
afterAll(async () => {
// Close connections
await server.close();
});
Coverage Thresholds
| Metric | Minimum |
|---|---|
| Lines | 90% |
| Statements | 90% |
| Functions | 80% |
| Branches | 80% |
Run coverage: npm test -- --coverage
Running Tests
npm test # All tests
npm run test:unit # Unit only
npm run test:integration # Integration only
npm test -- path/to/file.test.ts # Single file
npm test -- --watch # Watch mode