Lab 13: Testing with Vitest
Step 1: Setup
docker run -it --rm node:20-alpine sh
npm install -g typescript ts-node
mkdir /lab13 && cd /lab13
npm init -y
npm install --save-dev vitest @vitest/coverage-v8
cat > tsconfig.json << 'EOF'
{
"compilerOptions": {
"target": "ES2020",
"module": "ESNext",
"moduleResolution": "bundler",
"strict": true,
"esModuleInterop": true
}
}
EOF
cat > vitest.config.ts << 'EOF'
import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
globals: true,
environment: 'node',
coverage: { provider: 'v8' },
},
});
EOFStep 2: Source Files Under Test
Step 3: Basic Tests
Step 4: Typed Mocks (vi.fn)
Step 5: vi.spyOn and Implementation
Step 6: Async Tests and Timers
Step 7: Package.json & Running Tests
Step 8: Capstone — Full Test Suite
Summary
API
Purpose
Last updated
