What you'll build
An automated test suite that verifies all your API endpoints work correctly before each deploy. You'll use Vitest and Supertest to write tests that simulate HTTP requests, verify responses, and mock the database for isolated and fast tests. When finished, you'll have tests for complete CRUD operations, error cases, authentication, and a coverage report that tells you what percentage of your code is tested.
Step 1: Ask an AI for the tests
I need tests for a REST API with:
- Vitest + supertest
- Tests for each endpoint (GET, POST, PUT, DELETE)
- Database mock
- Authentication tests
- Coverage report
Give me complete tests for a users CRUD.
Setup
pnpm add -D vitest supertest @types/supertest
Test example
import { describe, it, expect } from 'vitest'
import request from 'supertest'
import { app } from '../app'
describe('Users API', () => {
it('GET /api/users returns users', async () => {
const res = await request(app).get('/api/users')
expect(res.status).toBe(200)
expect(Array.isArray(res.body)).toBe(true)
})
it('POST /api/users creates user', async () => {
const res = await request(app)
.post('/api/users')
.send({ email: 'test@test.com', name: 'Test' })
expect(res.status).toBe(201)
expect(res.body.email).toBe('test@test.com')
})
})