3.9 KiB
3.9 KiB
Vitest Migration Summary
Overview
Successfully migrated the test suite from Node.js built-in test runner to Vitest with global setup/teardown for database initialization.
Changes Made
1. Dependencies Added
pnpm add -D vitest@latest @vitest/ui@latest
- vitest: Modern test runner with better DX
- @vitest/ui: Visual test runner UI
2. Configuration Files Created
vitest.config.ts
- Test environment: Node.js
- Global setup/teardown hooks configured
- Test files:
src/__tests__/**/*.test.ts - Single fork mode for consistency
- Timeout: 30s per test, 60s per hook
src/__tests__/globalSetup.ts
- Exports
setup()function that runs once before all tests- Creates test database and users
- Generates access tokens
- Exports
teardown()function that runs once after all tests- Cleans up all test data
src/__tests__/setup.ts
- Per-test-file setup (currently minimal)
- Can be extended for per-file setup needs
3. Test Files Converted
All test files migrated from Node.js test API to Vitest API:
Import Changes
// Before
import assert from "node:assert/strict";
import { describe, it } from "node:test";
// After
import { describe, it, expect } from "vitest";
Assertion Changes
// Before
assert.strictEqual(actual, expected);
assert.ok(value);
// After
expect(actual).toBe(expected);
expect(value).toBeTruthy();
Files Converted (14 files)
auth/auth.test.tsauth/maybeAuth.test.tsmiddlewares/middlewares.test.tsroutes/user.test.tsroutes/stripe.test.tsroutes/tablo.test.tsroutes/tasks.test.tsroutes/public.test.tsroutes/invite.test.tsroutes/tablo_data.test.tsroutes/notes.test.tshelpers/auth.test.tshelpers/uriComponent.test.tshelpers/slots.test.ts
4. Package.json Scripts Updated
{
"test": "NODE_ENV=test vitest run",
"test:watch": "NODE_ENV=test vitest",
"test:ui": "NODE_ENV=test vitest --ui"
}
Benefits of Vitest
- Better Global Setup: Proper global setup/teardown hooks that run once for entire test suite
- Faster Test Runs: Smart test running with better parallelization
- Better DX:
- Instant feedback with watch mode
- Visual UI for debugging tests
- Better error messages
- Modern API: Expect-style assertions that are more readable
- Vite Integration: Can leverage Vite's module resolution if needed
Test Database Setup
The global setup now properly:
- Creates 3 test users before any tests run
- Populates all main database tables with test data
- Generates access tokens for each user
- Makes test data available via
testUtils - Cleans up everything after all tests complete
Running Tests
# Run all tests once
npm test
# Watch mode - re-runs on changes
npm run test:watch
# Visual UI
npm run test:ui
Next Steps
To run tests successfully, ensure:
-
Supabase is running:
supabase start -
Environment variables are set in
.env.test:SUPABASE_URLSUPABASE_SERVICE_ROLE_KEYSUPABASE_CONNECTION_STRING- Other required env vars
-
Database schema is up to date:
supabase db reset
Troubleshooting
"Invalid API key" error
- Check
.env.testhas correctSUPABASE_SERVICE_ROLE_KEY - Ensure Supabase local is running
- Verify
SUPABASE_URLpoints to the correct instance
Test data not available
- Check that global setup completed successfully
- Look for errors in the global setup output
- Verify database connection is working
Tests failing after migration
- Check assertion syntax has been converted correctly
- Verify imports use Vitest instead of Node test
- Look for async/await issues in test setup
Documentation
See src/__tests__/README.md for detailed documentation on:
- Test data structure
- How to use test utilities
- Example test patterns
- File organization