| name | typescript-testing |
| description | Comprehensive testing guidance for TypeScript projects including unit testing patterns, mocking strategies, and test organization best practices |
| version | 1.0.0 |
| priority | 25 |
| tags | ["testing","typescript","vitest","jest","builtin"] |
| triggers | [{"type":"keyword","pattern":"test"},{"type":"keyword","pattern":"vitest"},{"type":"keyword","pattern":"jest"},{"type":"glob","pattern":"**/*.test.ts"},{"type":"glob","pattern":"**/*.spec.ts"}] |
| globs | ["**/*.test.ts","**/*.spec.ts","**/vitest.config.ts","**/jest.config.ts"] |
TypeScript Testing
Guidance for writing effective tests in TypeScript projects using Vitest or Jest.
Rules
- Arrange-Act-Assert: Structure tests with clear setup, execution, and verification phases
- Single Assertion Focus: Each test should verify one specific behavior
- Descriptive Names: Use
describe blocks for grouping and it/test with clear descriptions
- Test Isolation: Tests must not depend on execution order or shared mutable state
- Mock External Dependencies: Always mock I/O, network calls, and system interactions
- Coverage Targets: Aim for 80%+ line coverage, 70%+ branch coverage
- No Logic in Tests: Avoid conditionals and loops in test code
- Clean Up: Use
beforeEach/afterEach for setup/teardown, never leave test artifacts
Patterns
Test Structure
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { MyService } from "./my-service.js";
describe("MyService", () => {
let service: MyService;
beforeEach(() => {
service = new MyService();
vi.useFakeTimers();
});
afterEach(() => {
vi.useRealTimers();
vi.restoreAllMocks();
});
describe("methodName", () => {
it("should return expected result when given valid input", () => {
const input = "test";
const result = service.methodName(input);
expect(result).toBe("expected");
});
it("should throw error when given invalid input", () => {
expect(() => service.methodName(null)).toThrow("Invalid input");
});
});
});
typescript
{ vi, } ;
{ } ;
vi.(, ({
: vi.().( ({
: vi.(),
: vi.(),
})),
}));
mockFetch = vi.() < fetch>;
vi.(, mockFetch);
mockFetch.(
(.({ : }), {
: ,
: { : },
})
);
typescript
(, () => {
promise = service.();
vi.();
result = promise;
(result).();
});
(, () => {
mockDependency.( ());
(service.())..();
});
typescript
(, {
result = service.();
(result).();
});
(, {
result = service.({ : });
(result).();
});
typescript
(, {
spy = vi.(service, );
service.();
(spy).();
});
(, {
(service.()).();
(service.()).();
(service.()).();
});
: ;
(, { sharedState = ; });
(, { (sharedState).(); });
(, {
service.().( (r).());
});
mockFn.()
.()
.();
typescript
{ describe, it, expect, vi, beforeEach } ;
{ } ;
{ } ;
(, {
: ;
: ;
( {
mockRepo = {
: vi.(),
: vi.(),
: vi.(),
};
service = (mockRepo);
});
(, {
(, () => {
user = { : , : };
vi.(mockRepo.).(user);
result = service.();
(result).(user);
(mockRepo.).();
});
(, () => {
vi.(mockRepo.).();
result = service.();
(result).();
});
});
});
typescript
(, {
(, () => {
dbError = ();
vi.(mockRepo.).(dbError);
(service.())..();
});
(, {
( service.()).();
( service.()).();
});
});
References