javascript-testing-patterns
Write JavaScript Tests with Jest and Vitest
Writing reliable tests for JavaScript and TypeScript applications is time-consuming and error-prone. This skill provides ready-to-use testing patterns for unit tests, integration tests, and component tests with mocking, fixtures, and TDD workflows.
Download the skill ZIP
Upload in Claude
Go to Settings → Capabilities → Skills → Upload skill
Toggle on and start using
Test it
Using "javascript-testing-patterns". Write tests for a user service that creates, updates, and deletes users
Expected outcome:
- Created UserService with CRUD operations
- Tested create() method - success case and duplicate user error
- Tested update() method - success case and user not found error
- Tested delete() method - successful deletion and non-existent user
- Used beforeEach to reset service state between tests
- Added comprehensive assertions for all edge cases
Using "javascript-testing-patterns". Create a test for an API service that fetches data from an external endpoint
Expected outcome:
- Set up ApiService with fetchUser and createUser methods
- Mocked global.fetch with vi.fn() for isolated testing
- Tested successful user fetch with mock response
- Tested error handling for failed API calls
- Verified correct HTTP methods and request bodies
Using "javascript-testing-patterns". Write a React component test for a form with user input and submission
Expected outcome:
- Rendered UserForm component with Testing Library
- Tested input field rendering and state updates
- Verified onSubmit callback fires with correct data
- Used fireEvent to simulate user interactions
- Added testid attributes for reliable element selection
Security Audit
SafeThis is a documentation-only skill containing testing pattern examples. The static analyzer flagged 153 patterns, but ALL are FALSE POSITIVEs: weak crypto alerts are triggered by test fixture strings (hashed_password), backtick alerts are TypeScript template literals, path traversal alerts are relative import paths in test code, and the critical heuristic is an invalid combination of benign testing patterns. The previous audit correctly classified this as SAFE.
Risk Factors
🌐 Network access (33)
⚙️ External commands (43)
📁 Filesystem access (9)
🔑 Env variables (8)
Quality Score
What You Can Build
Test React components
Write component tests with React Testing Library to verify UI behavior and user interactions.
Test API endpoints
Create integration tests for REST APIs with database fixtures and authentication flows.
Implement TDD workflow
Follow test-driven development patterns to build reliable functions and services with high coverage.
Try These Prompts
Write unit tests for this function that cover normal cases, edge cases, and error conditions.
Create tests for this service that mocks external API calls using vi.mock or jest.mock.
Write component tests using @testing-library/react that test user interactions and rendering.
Create integration tests for this REST API endpoint including authentication and database cleanup.
Best Practices
- Use the AAA pattern: Arrange inputs, Act on the function, Assert expected outcomes
- Mock external dependencies to keep tests fast and isolated from real services
- Test behavior, not implementation details, to make tests resilient to refactoring
Avoid
- Avoid testing implementation details that change frequently
- Do not write tests that depend on execution order or shared state
- Do not skip mocking slow external services in unit tests