Testing Patterns — Agent Lens
TDD workflow
- Write a failing test (red)
- Write minimal code to pass (green)
- Refactor while keeping tests green
Vitest setup
- Config:
vitest.config.ts(globals enabled, includessrc/**/*.test.tsandwebview/**/*.test.ts) - Run all:
npm test - Run one file:
npx vitest run src/parsers/agentParser.test.ts - Watch mode:
npm run test:watch
File conventions
- Tests are co-located:
src/parsers/foo.ts→src/parsers/foo.test.ts - Use
describe/itblocks - Use
expectassertions - Vitest globals are enabled (no imports needed for
describe,it,expect)
What to test
| Layer | What to test | Example file |
|---|---|---|
| Parsers | Pure function input/output, edge cases, malformed input | agentParser.test.ts |
| Analyzers | Graph construction, metrics aggregation, edge cases | graphBuilder.test.ts |
| Detectors | Detection logic for agents, skills, tools, MCP servers | detectors.test.ts |
| Layout | D3-DAG layout algorithm, cycle handling | webview/layout.test.ts |
Mocking
- Mock external dependencies (filesystem, VS Code API), not internal logic
- For parsers: pass input strings directly, no mocking needed
- For providers: mock
fsandvscode.workspaceat boundaries
Common patterns
describe("parserName", () => {
it("should parse valid input", () => {
const result = parseAgent(validMarkdown);
expect(result.name).toBe("expected");
});
it("should handle missing frontmatter", () => {
const result = parseAgent("no frontmatter here");
expect(result.name).toBe("");
});
});
Converted and distributed by TomeVault — claim your Tome and manage your conversions.