ts-testing
Skill1 starsChanged 27 days agoMITView on GitHub
TypeScript testing advisor. Always use this skill when writing tests in TypeScript, setting up Jest or Vitest with TypeScript, creating type-safe mocks, or writing type-level tests. Use when the user asks "how do I mock this in TypeScript", "my mock has the wrong type", "how do I test that a type is correct", "jest.fn doesn't match my interface", "how do I set up Jest with TypeScript", "how do I avoid as any in tests", or "how do I write a type-level test". Read this skill before writing any TypeScript tests or mocks.
We split this skill into the rules it gives an agent
8 rules. 0 are common, 2 are distinctive, and 6 are very specific to this project.
0
Common
Most of the 3 edited versions we read keep these.
2
Distinctive
This author's own choices. Worth a look.
6
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 12
Use ts-jest or Vitest's native TypeScript support.
- Very specific: line 29
Always provide the type parameter to jest.fn.
- Very specific: line 54
Use Partial + casting only as a last resort.
- Very specific: line 68
If you find yourself adding as any to satisfy a mock type, that's usually a signal the interface is too wide or the test is coupled to …
- Distinctive: line 82
If you feel pulled to test a private method, that's often a design signal: the logic may deserve its own module with a public interface.
- Very specific: line 86
Use expectTypeOf (Vitest) or tsd (standalone) to assert that types are what you expect, important when building utility types or library …
- Very specific: line 109
When asserting shape, satisfies is often cleaner than as:
- Distinctive: line 123
Always return or await promises in tests.
“Common” is measured: kept by at least 6 in 10 of the 3 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
TypeScript Testing
Tests that use as any to satisfy the type checker aren't testing the types; they're hiding them. The goal is tests that are type-safe at the boundary and test the public interface rather than internals. They should also give meaningful errors when types change.
Jest setup for TypeScript
Use ts-jest or Vitest's native TypeScript support. Avoid transpile-only setups (like babel-jest without type checking) for type-related tests: they skip type errors.
Vitest (preferred for new projects):
// vitest.config.ts
import { defineConfig } from 'vitest/config';
export default defineConfig({ test: { globals: true } });Jest with ts-jest:
// jest.config.json
{ "preset": "ts-jest", "testEnvironment": "node" }Type-safe mocks with jest.fn
Always provide the type parameter to jest.fn; don't let it default to jest.Mock (which is effectively any).
// Bad: loses type information
const fetchUser = jest.fn();
// Good: typed mock
const fetchUser = jest.fn<Promise<User>, [string]>();
fetchUser.mockResolvedValue({ id: '1', name: 'Alice' });For mocking module-level functions, use jest.mocked() (Jest 27+) or vi.mocked() (Vitest) instead of casting:
import { sendEmail } from './email';
jest.mock('./email');
const mockSendEmail = jest.mocked(sendEmail);
mockSendEmail.mockResolvedValue(undefined);jest.mocked() returns the function with its mock type overlaid; no as any needed.
Mocking interfaces and classes
Use Partial + casting only as a last resort. Prefer purpose-built mock factories or dependency injection.
// Acceptable for simple cases, but loses full interface coverage
const mockRepo: jest.Mocked<UserRepository> = {
findById: jest.fn(),
save: jest.fn(),
delete: jest.fn(),
};
// Better: inject the mock through the constructor
const service = new UserService(mockRepo);If you find yourself adding as any to satisfy a mock type, that's usually a signal the interface is too wide or the test is coupled to implementation details.
Testing the public interface, not internals
TypeScript's private keyword is a compile-time constraint. In tests, resist the urge to test private methods. They're an implementation detail.
// Bad: testing a private method directly (requires `as any` to bypass TS)
expect((service as any).buildQuery(params)).toEqual(...);
// Good: test through the public method that exercises the behavior
expect(await service.search(params)).toEqual(expectedResults);If you feel pulled to test a private method, that's often a design signal: the logic may deserve its own module with a public interface.
Type-level testing
Use expectTypeOf (Vitest) or tsd (standalone) to assert that types are what you expect, important when building utility types or library code.
Vitest:
import { expectTypeOf } from 'vitest';
expectTypeOf(parseDate('2026-01-01')).toEqualTypeOf<Date>();
expectTypeOf(first([])).toEqualTypeOf<undefined | number>();tsd (for libraries, runs as part of CI):
// index.test-d.ts
import { expectType } from 'tsd';
import { parseDate } from '.';
expectType<Date>(parseDate('2026-01-01'));Type tests catch regressions in generic utilities that runtime tests can't: a function that works correctly at runtime can still have a broken type signature.
The satisfies operator in test assertions
When asserting shape, satisfies is often cleaner than as:
const config = {
timeout: 5000,
retries: 3,
} satisfies Partial<Config>;
// TypeScript validates the shape AND preserves literal types
// so config.timeout is 5000, not numberTesting async code
Always return or await promises in tests; otherwise failures become false positives.
// Bad: test passes even if the promise rejects
it('should save user', () => {
service.save(user); // not awaited — test exits before the promise settles
});
// Good
it('should save user', async () => {
await service.save(user);
expect(mockRepo.save).toHaveBeenCalledWith(user);
});For testing rejected promises:
await expect(service.save(invalidUser)).rejects.toThrow('Validation failed');What to watch for in code review
jest.fn()without a type parameter (loses the mock's type contract)as anyused to satisfy mock types (hides type mismatches that should be fixed)- Tests that access private members via
as any - Unawaited promises in async tests
- Mocks that return hardcoded shapes that diverge from the actual type will pass tests but fail at runtime when the real type changes
Other versions3 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 3
- johnlindquist/claude-workshop-skills★ 16same name
- batur/ts-baseline-docs★ 1same name
- Baba01hacker666/dummyskills★ 0same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository25 other files it gives its agents.
- api-designSkill
- arch-reviewSkill
- db-reviewSkill
- debugSkill
- dotnet-asyncSkill
- dotnet-opsSkill
- dotnet-testingSkill
- dotnet-typesSkill
- go-concurrencySkill
- go-opsSkill
- go-testingSkill
- go-typesSkill
- incidentSkill
- node-asyncSkill
- node-opsSkill
- perfSkill
- pr-prepSkill
- py-asyncSkill
- py-opsSkill
- py-testingSkill
- py-typesSkill
- refactorSkill
- securitySkill
- test-genSkill
- ts-typesSkill
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
Reviews can't be read right now.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

