Testing Prompts

Prompts for unit tests, integration tests, E2E testing, mocking, and test infrastructure.

#38 Testing

Unit Test Suite with Mocking

Write a comprehensive unit test suite for a user service module that handles CRUD operations. Use Jest (or Vitest) with TypeScript. Include tests for: creating a user (valid input, duplicate email, validation errors), fetching users (by ID, not found, list with pagination), updating a user (partial updates, optimistic concurrency), and deleting a user (soft delete, cascade check). Mock the database layer using jest.mock(). Include setup/teardown, test grouping with describe blocks, and at least one snapshot test for response shape. Aim for >90% code coverage.

#39 Testing

Integration Test for API Endpoints

Write integration tests for a REST API using supertest and Jest. Test the complete request/response cycle for a resource (e.g., /api/posts). Include tests for: GET list with pagination and filtering, GET single item (found and not found), POST create (valid data, validation errors, authentication required), PUT update (owner only, not found), DELETE (owner only, admin override), and proper error response format. Use a test database that resets between test runs. Include authentication helper that generates valid JWT tokens for test users.

#40 Testing

E2E Test with Playwright

Write end-to-end tests using Playwright for a web application's critical user flows. Test: user registration flow (fill form, submit, verify redirect to dashboard), login flow (valid credentials, invalid password shows error, remember me checkbox), creating a new item (fill form, upload image, submit, verify item appears in list), search and filter (type search query, apply filters, verify results update), and responsive behavior (run the same test at desktop, tablet, and mobile viewports). Include proper test isolation, meaningful test names, and screenshot capture on failure.

#41 Testing

API Contract Testing

Set up API contract testing to ensure your API responses match the expected schema. Use a JSON Schema validation approach (or Zod). For each endpoint, define the expected response schema including: required fields, data types, nested object structures, array item shapes, enum values, and nullable fields. Write tests that make real API calls and validate responses against the schema. Include tests for error responses too (400, 401, 404, 500 should all have consistent error format). Generate schema documentation from the test definitions.

#42 Testing

Visual Regression Testing Setup

Set up visual regression testing for a component library using Playwright or Storybook with Chromatic. Create visual tests for: a button component in all states (default, hover, focus, disabled, loading, each size variant), a form with validation errors displayed, a modal dialog (open state, with scrollable content), a data table with various data states (empty, loading, populated, error), and a responsive navigation (desktop expanded, mobile hamburger). Configure screenshot comparison with a 0.1% threshold for anti-aliasing differences. Include a CI pipeline step that runs visual tests on PRs.

#43 Testing

Test Data Factory

Create a test data factory system (similar to FactoryBot or Fishery) for generating test fixtures in JavaScript/TypeScript. Build factories for User, Post, and Comment entities with: default values using faker, trait overrides (e.g., User.admin(), User.unverified()), sequence generation for unique fields (email, username), association handling (Post.create() automatically creates an associated User), batch creation (User.createMany(10)), and async variants that persist to the test database. Include TypeScript types that infer the return type based on the factory definition.