Vitest / Jest Testing
When to Use
- •Adding tests for new or existing modules (unit or integration).
- •Fixing failing tests (determine whether the failure is in implementation or test; fix minimally).
- •Improving test structure (
describe,it/test, setup) or readability.
Principles
- •Layout: Place tests per project convention:
*.test.ts/*.spec.tsnext to source, or undertest//__tests__/. Usedescribeandit/testwith clear descriptions. - •Assertions: Prefer explicit assertions (e.g.
expect(x).toBe(y)). Keep expected values clear. Use type-safe matchers when available. - •Setup: Use
beforeEach/beforeAllfor shared setup; mock only when necessary; prefer real dependencies for integration-style tests when fast enough. - •Verification: Run the test script (e.g.
npm test,npx vitest run,npx jest) to verify. Do not change behaviour beyond what is needed to fix the failure.
Note
If the project uses Jest instead of Vitest, the same principles apply; adjust commands (e.g. npx jest instead of npx vitest run).