Agent skill
jest
Jest testing framework for JavaScript/TypeScript
Install this agent skill to your Project
npx add-skill https://github.com/majiayu000/claude-skill-registry/tree/main/skills/other/other/jest-baphled-dotopencode
SKILL.md
Skill: jest
What I do
I provide Jest testing expertise: test structure, mocking strategies, async testing, snapshot tests, and coverage configuration for JavaScript/TypeScript projects.
When to use me
- Writing unit or integration tests in JavaScript/TypeScript
- Mocking modules, functions, or timers
- Testing async code (promises, async/await, callbacks)
- Setting up test configuration and coverage thresholds
- Debugging flaky or slow tests
Core principles
- Arrange-Act-Assert - Clear test structure with setup, action, and verification
- Mock at boundaries - Mock external dependencies, not internal implementation
- Test behaviour, not implementation - Assert outcomes, not function calls
- Isolate tests - Each test runs independently, no shared mutable state
- Fast feedback - Keep tests fast; mock network/disk; use
--watch
Patterns & examples
Basic test structure:
describe('CartService', () => {
let cart;
beforeEach(() => {
cart = new CartService();
});
it('adds item and updates total', () => {
cart.addItem({ id: 1, price: 9.99 });
expect(cart.items).toHaveLength(1);
expect(cart.total).toBeCloseTo(9.99);
});
it('throws on negative quantity', () => {
expect(() => cart.addItem({ id: 1, qty: -1 }))
.toThrow('Quantity must be positive');
});
});
Mocking modules:
// ✅ Correct: mock at module boundary
jest.mock('./api-client');
const { fetchUser } = require('./api-client');
fetchUser.mockResolvedValue({ id: 1, name: 'Alice' });
it('loads user profile', async () => {
const profile = await loadProfile(1);
expect(profile.name).toBe('Alice');
expect(fetchUser).toHaveBeenCalledWith(1);
});
// ❌ Wrong: mocking internal implementation details
jest.spyOn(service, '_privateHelper'); // brittle
Async testing:
// ✅ Correct: async/await pattern
it('fetches data successfully', async () => {
const data = await fetchData('/api/items');
expect(data).toEqual(expect.arrayContaining([
expect.objectContaining({ id: 1 })
]));
});
// ✅ Correct: testing rejections
it('rejects on network error', async () => {
await expect(fetchData('/bad')).rejects.toThrow('Network error');
});
Timer mocking:
beforeEach(() => jest.useFakeTimers());
afterEach(() => jest.useRealTimers());
it('debounces search input', () => {
const handler = jest.fn();
const search = debounce(handler, 300);
search('he');
search('hel');
search('hello');
jest.advanceTimersByTime(300);
expect(handler).toHaveBeenCalledTimes(1);
expect(handler).toHaveBeenCalledWith('hello');
});
Snapshot testing:
// ✅ Correct: small, focused snapshots
it('renders user card', () => {
const { container } = render(<UserCard name="Alice" role="admin" />);
expect(container.firstChild).toMatchSnapshot();
});
// ❌ Wrong: snapshotting entire page (brittle, noisy diffs)
expect(document.body).toMatchSnapshot();
Anti-patterns to avoid
- ❌ Testing implementation details (spying on private methods)
- ❌ Large snapshot files (snapshot entire components, not pages)
- ❌ Shared mutable state between tests (use
beforeEachfor fresh state) - ❌ Forgetting
awaiton async assertions (test passes falsely) - ❌ Over-mocking (mock boundaries, not everything—test real logic)
KB Reference
~/vaults/baphled/3. Resources/Knowledge Base/AI Development System/Skills/Testing-BDD/Jest.md
Related skills
javascript- Core JS/TS idioms and patternsbdd-workflow- Red-Green-Refactor cycleclean-code- SOLID principles in test codecypress- E2E testing (complementary to Jest unit tests)
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
Didn't find tool you were looking for?