This guide covers how to run and write tests for the Events Manager application.
The project uses Vitest as the testing framework, providing fast test execution, excellent TypeScript support, and a modern testing experience.
Ensure all dependencies are installed:
npm install
npm test
This runs all tests once and displays results in the terminal.
npm test -- --watch
Automatically re-runs tests when files change. Perfect for development.
npm run test:ui
Opens an interactive web interface showing:
npm run test:coverage
Generates coverage reports in multiple formats:
coverage/index.htmlcoverage/coverage-final.jsonnpm test -- database-types.test.ts
npm test -- -t "Event Priorities"
npm test -- shared/__tests__
npm test -- --reporter=verbose
shared/__tests__/)Comprehensive unit tests for all database types, schemas, and validations:
database-types.test.ts (60+ tests)
database-schema.test.ts (30+ tests)
schema.test.ts (20+ tests)
Total: 100+ tests with 90%+ coverage
Test files should be named with the .test.ts suffix:
shared/
database-types.ts # Source file
__tests__/
database-types.test.ts # Test file
import { describe, it, expect } from 'vitest';
import { myFunction } from '../my-module';
describe('Feature Name', () => {
it('should do something specific', () => {
// Arrange
const input = { value: 'test' };
// Act
const result = myFunction(input);
// Assert
expect(result).toBe('expected');
});
});
import { insertEventSchema } from '@shared/database-types';
describe('insertEventSchema', () => {
it('should validate valid event data', () => {
const validEvent = {
name: 'Test Conference',
link: 'https://example.com',
start_date: '2025-06-01',
end_date: '2025-06-03',
location: 'San Francisco',
priority: 'high',
type: 'conference',
};
const result = insertEventSchema.safeParse(validEvent);
expect(result.success).toBe(true);
});
it('should reject invalid event data', () => {
const invalidEvent = { name: 'Test' };
const result = insertEventSchema.safeParse(invalidEvent);
expect(result.success).toBe(false);
});
});
it('should fetch data successfully', async () => {
const result = await fetchData();
expect(result).toBeDefined();
});
it('should throw error for invalid input', () => {
expect(() => {
dangerousFunction(null);
}).toThrow('Invalid input');
});
// ✅ Good
it('should validate event with all required fields', () => { ... });
// ❌ Bad
it('test1', () => { ... });
// ✅ Good
it('should validate required username', () => {
const result = schema.safeParse({});
expect(result.success).toBe(false);
});
it('should accept valid username', () => {
const result = schema.safeParse({ username: 'test' });
expect(result.success).toBe(true);
});
// ❌ Bad
it('should validate username', () => {
expect(schema.safeParse({})).toBe(false);
expect(schema.safeParse({ username: 'test' })).toBe(true);
});
Each test should work in isolation:
// ✅ Good
it('should create user', () => {
const user = createUser({ username: 'test' });
expect(user).toBeDefined();
});
// ❌ Bad - depends on previous test
let userId;
it('should create user', () => {
userId = createUser({ username: 'test' }).id;
});
it('should find user', () => {
const user = findUser(userId);
expect(user).toBeDefined();
});
// ✅ Good
expect(result.success).toBe(true);
expect(result.data.name).toBe('Test Event');
// ❌ Bad
expect(result).toBeTruthy();
After running npm run test:coverage, open coverage/index.html in your browser to see:
Tests should run automatically in CI/CD:
# Example GitHub Actions workflow
name: Tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- run: npm install
- run: npm test
- run: npm run test:coverage
npm run test:ui
Then:
it('debug test', () => {
console.log('Debug info:', data);
expect(data).toBeDefined();
});
If tests take too long:
it('slow operation', async () => {
await slowOperation();
}, 10000); // 10 second timeout
Check these:
vitest.config.ts path aliases.js vs .ts)Always use async/await:
// ✅ Good
it('async test', async () => {
const result = await asyncOp();
expect(result).toBeDefined();
});
// ❌ Bad
it('async test', () => {
const result = asyncOp(); // Missing await!
expect(result).toBeDefined();
});
Questions? See the Support page for help.