Context
Part of the Test Suite Improvement Roadmap to increase test pass rate from 67.7% to 95%+.
Related: See docs/test-suite-improvement-roadmap.md for full context.
Problem
Failing Tests: 42 tests in tests/unit/services/email-service.test.ts
Files Affected:
tests/unit/services/email-service.test.ts
Root Cause:
Tests use CommonJS require() in ES module context and attempt integration-style testing with real API calls instead of proper unit testing with mocks.
Specific Issues
-
Line 42: const { renderTemplate } = require('@/services/email-service')
- Uses
require() instead of ES module import
- Fails in Vitest ES module environment
-
Resend API Mocking: Tests attempt to use real Resend API
- Not properly mocked
- Causes network-dependent test failures
-
Date/Time Handling: Tests fail due to timezone inconsistencies
- Password reset expiration tests
- Verification email expiration tests
-
Template Rendering: Tests for XSS protection and variable substitution fail
- Module import issues prevent testing
Proposed Solution
Changes Required
-
Convert to ES Module Imports:
// Remove this:
const { renderTemplate } = require('@/services/email-service');
// Replace with:
import {
renderTemplate,
sendEmail,
sendOrderConfirmation,
sendShippingConfirmation,
sendPasswordReset
} from '@/services/email-service';
-
Add Proper Resend Mocking:
vi.mock('resend', () => ({
Resend: vi.fn(() => ({
emails: {
send: vi.fn().mockResolvedValue({
id: 'test-email-id',
from: 'noreply@stormcom.test',
to: 'test@example.com',
}),
},
})),
}));
-
Implement Consistent Date Mocking:
beforeEach(() => {
// Mock system time for consistent test results
vi.setSystemTime(new Date('2025-01-01T00:00:00Z'));
});
afterEach(() => {
vi.useRealTimers();
});
-
Refactor from Integration to Unit Tests:
- Test
renderTemplate() function in isolation
- Mock all external dependencies (Resend, database)
- Verify function behavior, not side effects
- Test error handling paths
Expected Outcome
Implementation Checklist
Development
Testing
Documentation
Code Review
Acceptance Criteria
Test Results
Before
❌ FAIL tests/unit/services/email-service.test.ts (42 tests)
Error: Cannot find module '@/services/email-service'
Test Files 1 failed (1)
Tests 42 failed (42)
After
✅ PASS tests/unit/services/email-service.test.ts (42 tests)
Test Files 1 passed (1)
Tests 42 passed (42)
Duration 1.8s
Related Issues
- Part of: Test Suite Improvement Roadmap
- Blocks: #XXX (Notification Service tests - similar pattern)
- See also:
tests/unit/lib/security-headers.test.ts for ES module mock example
Priority
Estimated Effort
Notes
Key Files to Reference
tests/setup.ts - ES module import patterns
tests/unit/lib/security-headers.test.ts - next-auth mocking example
tests/unit/components/gdpr/cookie-consent.test.tsx - date mocking example
Testing Gotchas
- Must use
vi.setSystemTime() not vi.useFakeTimers() for date mocking
- Resend mock must return proper structure with
id, from, to fields
- Template rendering tests need actual HTML string validation
Success Metrics
- Test execution time: < 2 seconds (currently fails to complete)
- Pass rate contribution: +4% to overall suite
- Code coverage: Email service should reach 80%+ coverage
Context
Part of the Test Suite Improvement Roadmap to increase test pass rate from 67.7% to 95%+.
Related: See
docs/test-suite-improvement-roadmap.mdfor full context.Problem
Failing Tests: 42 tests in
tests/unit/services/email-service.test.tsFiles Affected:
tests/unit/services/email-service.test.tsRoot Cause:
Tests use CommonJS
require()in ES module context and attempt integration-style testing with real API calls instead of proper unit testing with mocks.Specific Issues
Line 42:
const { renderTemplate } = require('@/services/email-service')require()instead of ES module importResend API Mocking: Tests attempt to use real Resend API
Date/Time Handling: Tests fail due to timezone inconsistencies
Template Rendering: Tests for XSS protection and variable substitution fail
Proposed Solution
Changes Required
Convert to ES Module Imports:
Add Proper Resend Mocking:
Implement Consistent Date Mocking:
Refactor from Integration to Unit Tests:
renderTemplate()function in isolationExpected Outcome
Implementation Checklist
Development
docs/test-suite-improvement-roadmap.mdnpm run test -- tests/unit/services/email-service.test.tsrequire()to ES importsnpm run type-checknpm run lintTesting
Documentation
Code Review
Acceptance Criteria
Test Results
Before
After
Related Issues
tests/unit/lib/security-headers.test.tsfor ES module mock examplePriority
Estimated Effort
Notes
Key Files to Reference
tests/setup.ts- ES module import patternstests/unit/lib/security-headers.test.ts- next-auth mocking exampletests/unit/components/gdpr/cookie-consent.test.tsx- date mocking exampleTesting Gotchas
vi.setSystemTime()notvi.useFakeTimers()for date mockingid,from,tofieldsSuccess Metrics