Testing
Add automated testing to your Kodkod project with Vitest.
Testing
Kodkod makes it easy to add a testing framework to your project and generate integration tests for your routes.
Setting Up Tests
Run the following command from your project root:
npx kodkod add testOnce run, it automatically sets up Vitest and installs all necessary dependencies.
Note
Looking for Jest? We are working on specialized ESM support for Jest. It will be added as an option in a future update.
What gets created
vitest.config.ts– Vitest configurationtests/setup.ts– Test setup file with database mockingtests/integration/– Directory for integration tests- Updated
package.jsonwith test dependencies and scripts
Generating Tests
Once testing is set up, you can generate tests for any route:
npx kodkod generate test productsThis creates tests/integration/products.test.ts with:
- GET all items
- GET by ID (including 404 case)
- POST create
- PUT update
- DELETE
Example Generated Test
import { describe, it, expect } from 'vitest';
import request from 'supertest';
import { app } from '../setup.js';
describe('Products API', () => {
describe('GET /products', () => {
it('should return a list', async () => {
const res = await request(app).get('/products');
expect(res.status).toBe(200);
});
});
describe('POST /products', () => {
it('should handle creation request', async () => {
const res = await request(app)
.post('/products')
.send({
// TODO: Add valid request body
});
expect([201, 400, 500]).toContain(res.status);
});
});
});Running Tests
After generating tests, run them with:
# Run all tests
npm run test
# Watch mode
npm run test:watch
# With coverage
npm run test:coverageCustomizing Tests
Generated tests are starting points. You should:
- Add real test data – Replace
// TODOcomments with actual request bodies - Add assertions – Verify response structure matches your schema
- Add edge cases – Test validation errors, auth requirements, etc.
