docs/dev/testing-conventions.md
This document explains the testing strategy and conventions used in the Foam VS Code extension.
We use two distinct types of test files, each serving different purposes:
.test.ts Files - Pure Unit Tests.spec.ts Files - Integration Tests with VS Code APIsvscode module), otherwise avoid incurring the performance hitsrc/features/ and service layers.spec.ts Files.spec.ts files use VS Code APIs, but they can run in different environments:
This dual-environment capability allows us to:
| Test Type | Environment | Typical Duration | VS Code APIs |
|---|---|---|---|
.test.ts | Vitest (Node.js) | fastest | No |
.spec.ts (mock) | Vitest + VS Code Mocks | fast | Yes (mocked) |
.spec.ts (real) | VS Code Extension Host | sloooooow. | Yes (real) |
yarn test:unit: Runs .test.ts files (no VS Code dependencies) + @unit-ready marked .spec.ts files using mocksyarn test:unit-without-specs: Runs only .test.ts filesyarn test:e2e: Runs all .spec.ts files in full VS Code extension hostyarn test: Runs both unit and e2e test suites sequentiallyWe're gradually enabling .spec.ts files to run in our fast mock environment while maintaining their ability to run in real VS Code.
@unit-ready AnnotationSpec files marked with /* @unit-ready */ can run in both environments:
/* @unit-ready */
import * as vscode from 'vscode';
// ... test uses VS Code APIs but works with our mocks
Configuration defaults: Our mocks don't load package.json defaults
// Before
const format = getFoamVsCodeConfig('openDailyNote.filenameFormat');
// After (defensive)
const format = getFoamVsCodeConfig(
'openDailyNote.filenameFormat',
'yyyy-mm-dd'
);
File system operations: Ensure proper async handling
// Mock file operations are immediate but still async
await vscode.workspace.fs.writeFile(uri, content);
Some specs should remain real-VS-Code-only:
Our vscode-mock.ts provides comprehensive VS Code API mocking:
When adding new tests:
Choose the right type:
.test.ts for pure business logic with no VS Code dependencies.spec.ts for anything that needs VS Code APIsConsider mock compatibility:
.spec.ts files, consider if they could run in mock environment/* @unit-ready */ if the test works with our mocksFollow naming conventions:
Performance awareness:
This testing strategy gives us the best of both worlds: fast feedback during development and comprehensive integration verification when needed.
@foam/graph-view)The webview component has its own test stack, separate from the extension tests.
packages/foam-graph/src/yarn workspace @foam/graph-view testThe webview tests focus on the framework-agnostic library code in src/lib/ (graph utilities, color logic, painter). Lit component rendering tests can be added as the component grows.