Catalog
github/javascript-typescript-jest

github

javascript-typescript-jest

Best practices for writing JavaScript/TypeScript tests using Jest, including mocking strategies, test structure, and common patterns.

v1.0Latest
New~545Updated Jun 26, 2026

Test Structure

  • Name test files with .test.ts or .test.js suffix
  • Place test files next to the code they test or in a dedicated __tests__ directory
  • Use descriptive test names that explain the expected behavior
  • Use nested describe blocks to organize related tests
  • Follow the pattern: describe('Component/Function/Class', () => { it('should do something', () => {}) })

Effective Mocking

  • Mock external dependencies (APIs, databases, etc.) to isolate your tests
  • Use jest.mock() for module-level mocks
  • Use jest.spyOn() for specific function mocks
  • Use mockImplementation() or mockReturnValue() to define mock behavior
  • Reset mocks between tests with jest.resetAllMocks() in afterEach

Testing Async Code

  • Always return promises or use async/await syntax in tests
  • Use resolves/rejects matchers for promises
  • Set appropriate timeouts for slow tests with jest.setTimeout()

Snapshot Testing

  • Use snapshot tests for UI components or complex objects that change infrequently
  • Keep snapshots small and focused
  • Review snapshot changes carefully before committing

Testing React Components

  • Use React Testing Library over Enzyme for testing components
  • Test user behavior and component accessibility
  • Query elements by accessibility roles, labels, or text content
  • Use userEvent over fireEvent for more realistic user interactions

Common Jest Matchers

  • Basic: expect(value).toBe(expected), expect(value).toEqual(expected)
  • Truthiness: expect(value).toBeTruthy(), expect(value).toBeFalsy()
  • Numbers: expect(value).toBeGreaterThan(3), expect(value).toBeLessThanOrEqual(3)
  • Strings: expect(value).toMatch(/pattern/), expect(value).toContain('substring')
  • Arrays: expect(array).toContain(item), expect(array).toHaveLength(3)
  • Objects: expect(object).toHaveProperty('key', value)
  • Exceptions: expect(fn).toThrow(), expect(fn).toThrow(Error)
  • Mock functions: expect(mockFn).toHaveBeenCalled(), expect(mockFn).toHaveBeenCalledWith(arg1, arg2)
Files1
1 files · 1.0 KB

Select a file to preview

Overall Score

78/100

Grade

B

Good

Safety

95

Quality

75

Clarity

82

Completeness

68

Summary

This skill teaches best practices for writing JavaScript/TypeScript tests using Jest, covering test structure, mocking strategies, async testing, snapshot testing, React component testing, and common matchers. It provides read-only reference guidance without file writes or shell operations.

Detected Capabilities

code analysisbest practices documentationtest structure guidancemocking strategy explanationmatcher reference

Trigger Keywords

Phrases that MCP clients use to match this skill to user intent.

write jest teststest react componentsmock external dependenciesjest best practicesunit test structuretesting async codejest matcherssnapshot testing

Use Cases

  • Write unit tests for JavaScript/TypeScript functions and classes
  • Mock external dependencies and APIs in tests
  • Test React components using React Testing Library
  • Create organized test suites with nested describe blocks
  • Test asynchronous code with promises and async/await
  • Write snapshot tests for UI components
  • Apply appropriate Jest matchers for different assertion scenarios

Quality Notes

  • Comprehensive coverage of Jest fundamentals with clear, practical examples
  • Well-organized sections (Test Structure, Mocking, Async, Snapshots, React, Matchers)
  • Provides specific patterns and syntax rather than vague guidance
  • Includes recommendations for React Testing Library over Enzyme, demonstrating awareness of modern best practices
  • Jest matcher reference is well-categorized and exhaustive
  • Clear guidance on test file naming conventions and directory organization
  • Explicitly addresses common patterns and anti-patterns (fireEvent vs userEvent)
  • No edge case discussion or error handling guidance — assumes user knows Jest API
  • Lacks examples of complete working test files — only shows fragments
  • No coverage of advanced topics like timer mocks, module mocking edge cases, or performance testing
Model: claude-haiku-4-5-20251001Analyzed: Jun 26, 2026

Reviews

Add this skill to your library to leave a review.

No reviews yet

Be the first to share your experience.

Use github/javascript-typescript-jest in your dev environment

Command Palette

Search for a command to run...