javascript-testing-patterns
Master JavaScript Testing Patterns
Também disponível em: wshobson
Writing tests is hard. This skill provides proven patterns and examples for unit, integration, and end-to-end testing using Jest, Vitest, and Testing Library so you can build reliable test suites quickly.
Baixar o ZIP da skill
Upload no Claude
Vá em Configurações → Capacidades → Skills → Upload skill
Ative e comece a usar
Testar
A utilizar "javascript-testing-patterns". Write unit tests for an add function that takes two numbers and returns their sum
Resultado esperado:
- ```typescript
- import { describe, it, expect } from 'vitest';
- import { add } from './calculator';
- describe('add', () => {
- it('should add two positive numbers', () => {
- expect(add(2, 3)).toBe(5);
- });
- it('should add negative numbers', () => {
- expect(add(-2, -3)).toBe(-5);
- });
- it('should handle zero', () => {
- expect(add(0, 5)).toBe(5);
- });
- });
- ```
A utilizar "javascript-testing-patterns". Create a test for a user form component that validates email and submits data
Resultado esperado:
- ```typescript
- import { render, screen, fireEvent } from '@testing-library/react';
- import { describe, it, expect, vi } from 'vitest';
- import { UserForm } from './UserForm';
- describe('UserForm', () => {
- it('should show validation error for invalid email', () => {
- render(<UserForm onSubmit={vi.fn()} />);
- fireEvent.change(screen.getByTestId('email-input'), {
- target: { value: 'invalid-email' }
- });
- fireEvent.click(screen.getByRole('button', { name: 'Submit' }));
- expect(screen.getByText('Invalid email')).toBeInTheDocument();
- });
- it('should call onSubmit with form data', () => {
- const onSubmit = vi.fn();
- render(<UserForm onSubmit={onSubmit} />);
- fireEvent.change(screen.getByTestId('name-input'), {
- target: { value: 'John Doe' }
- });
- fireEvent.change(screen.getByTestId('email-input'), {
- target: { value: 'john@example.com' }
- });
- fireEvent.click(screen.getByRole('button', { name: 'Submit' }));
- expect(onSubmit).toHaveBeenCalledWith({
- name: 'John Doe',
- email: 'john@example.com'
- });
- });
- });
- ```
Auditoria de Segurança
SeguroAll 145 static findings are false positives. The detected patterns exist only in code examples within markdown documentation files, not in executable code. The skill provides educational content about testing patterns with code samples demonstrating legitimate testing practices including mock setup, API testing, email service testing, and database integration testing. No executable code, no security risks.
Pontuação de qualidade
O Que Você Pode Construir
New Project Test Setup
Developers starting a new JavaScript/TypeScript project who need to establish test infrastructure from scratch with proper framework configuration, directory structure, and CI/CD integration.
Add Tests to Existing Codebase
Teams maintaining legacy or untested applications who need to gradually introduce testing for existing functions, APIs, and components without breaking existing functionality.
Improve Test Coverage
Developers seeking to increase code coverage and improve test quality by learning advanced mocking patterns, fixture factories, and integration testing strategies.
Tente Estes Prompts
Write unit tests for a function that [describe what function does]. Include tests for success cases and error handling.
Create React Testing Library tests for a component that [describe component behavior]. Test user interactions, form submissions, and error states.
Write integration tests for a REST API endpoint that [describe endpoint functionality]. Include database setup, authentication, and cleanup using supertest.
Create tests for [service/class] that mock external dependencies including [list dependencies]. Use dependency injection and vi.mock patterns.
Melhores Práticas
- Follow the Arrange-Act-Assert (AAA) pattern in every test for clear structure and readability
- Test behavior and user interactions rather than implementation details to make tests resilient to refactoring
- Use beforeEach and afterEach hooks for setup and teardown to keep tests isolated and prevent pollution
Evitar
- Testing implementation details like internal function calls or component state instead of observable behavior
- Writing brittle tests that break when code is refactored but behavior remains unchanged
- Creating overly complex test setups with too many mocks that make tests hard to understand and maintain
Perguntas Frequentes
Should I use Jest or Vitest?
How do I test code that uses external APIs?
What test coverage should I aim for?
Should I test private methods?
How do I handle database tests?
What is the difference between unit and integration tests?
Detalhes do Desenvolvedor
Autor
sickn33Licença
MIT
Repositório
https://github.com/sickn33/antigravity-awesome-skills/tree/main/skills/javascript-testing-patternsReferência
main
Estrutura de arquivos