Unit testing with Vitest
Test that your email components render without errors and produce expected HTML.Setup
Install testing dependencies:vite.config.ts:
vite.config.ts
Basic rendering test
Test that an email component renders successfully:src/lib/emails/welcome.test.ts
Testing with custom config
Test emails with custom Tailwind configuration:src/lib/emails/branded.test.ts
Snapshot testing
Snapshot tests capture the rendered HTML and alert you to unexpected changes.src/lib/emails/welcome.test.ts
Testing plain text conversion
Ensure the plain text version is generated correctly:src/lib/emails/welcome.test.ts
Testing email sending
Test email sending logic without actually sending emails.Mock email provider
Use Vitest mocking to test sending logic:src/routes/api/send-email/+server.test.ts
Visual testing
Use theEmailPreview component for manual visual testing during development.
Setup preview route
Create a preview route to visually test all your emails:src/routes/email-preview/[...email]/+page.server.ts
src/routes/email-preview/[...email]/+page.svelte
/email-preview to visually test all templates.
Testing responsive designs
Test that responsive classes generate proper media queries:src/lib/emails/responsive.test.ts
Test email in real clients
For production readiness, test in actual email clients:Using Email on Acid or Litmus
- Render your email template
- Copy the HTML output
- Paste into Email on Acid or Litmus
- Test across multiple clients
Manual testing
Use the preview component to send test emails to yourself:- Navigate to your preview route
- Select an email template
- Click “Send Test Email”
- Enter your email address
- Check the email in various clients (Gmail, Outlook, Apple Mail, etc.)
Continuous Integration
Add email testing to your CI pipeline:.github/workflows/test.yml
package.json includes:
package.json
Testing checklist
Before deploying email templates, verify:- Template renders without errors
- Props are correctly interpolated
- Custom Tailwind config is applied
- Plain text version is readable
- Responsive classes generate media queries
- Links are correct and trackable
- Images load properly
- Tested in major email clients (Gmail, Outlook, Apple Mail)
- Subject line is appropriate
- From address is configured
- Unsubscribe link is present (if required)
Best practices
- Test early and often: Run tests during development, not just before deployment.
- Use snapshots wisely: Snapshot tests are great for catching unintended changes, but update them when you make intentional changes.
- Mock external services: Don’t send real emails in tests; mock your email provider.
- Test edge cases: Empty strings, long names, special characters, etc.
- Visual testing matters: Automated tests can’t catch everything. Always do visual checks.
- Test across clients: Different email clients render HTML differently. Test the important ones for your audience.
Next steps
Email Preview
Set up visual testing with the preview component
Sending Emails
Learn how to send emails in production