$npx -y skills add Aaronontheweb/dotnet-skills --skill verify-email-snapshotsSnapshot test email templates using Verify to catch regressions. Validates rendered HTML output matches approved baseline. Works with MJML templates and any email renderer.
| 1 | # Snapshot Testing Email Templates with Verify |
| 2 | |
| 3 | ## When to Use This Skill |
| 4 | |
| 5 | Use this skill when: |
| 6 | - Testing email template rendering for regressions |
| 7 | - Validating MJML templates compile to expected HTML |
| 8 | - Reviewing email changes in code review (diffs are visual) |
| 9 | - Ensuring variable substitution works correctly |
| 10 | |
| 11 | **Related skills:** |
| 12 | - `aspnetcore/mjml-email-templates` - MJML template authoring |
| 13 | - `aspire/mailpit-integration` - Test email delivery locally |
| 14 | - `testing/snapshot-testing` - General Verify patterns |
| 15 | |
| 16 | --- |
| 17 | |
| 18 | ## Why Snapshot Test Emails? |
| 19 | |
| 20 | Email templates are: |
| 21 | 1. **Visual** - Small changes can break rendering across clients |
| 22 | 2. **Hard to unit test** - Output is complex HTML, not simple values |
| 23 | 3. **Prone to regression** - Template changes can have unintended effects |
| 24 | |
| 25 | Snapshot testing captures the rendered HTML and fails when it changes unexpectedly. |
| 26 | |
| 27 | --- |
| 28 | |
| 29 | ## Installation |
| 30 | |
| 31 | ```bash |
| 32 | dotnet add package Verify.Xunit # or Verify.NUnit, Verify.MSTest |
| 33 | ``` |
| 34 | |
| 35 | --- |
| 36 | |
| 37 | ## Basic Email Snapshot Test |
| 38 | |
| 39 | ```csharp |
| 40 | [Fact] |
| 41 | public async Task UserSignupInvitation_RendersCorrectly() |
| 42 | { |
| 43 | // Arrange |
| 44 | var renderer = _services.GetRequiredService<IMjmlTemplateRenderer>(); |
| 45 | |
| 46 | var variables = new Dictionary<string, string> |
| 47 | { |
| 48 | { "PreviewText", "You've been invited to join Acme Corp" }, |
| 49 | { "OrganizationName", "Acme Corporation" }, |
| 50 | { "InviteeName", "John Doe" }, |
| 51 | { "InviterName", "Jane Admin" }, |
| 52 | { "InvitationLink", "https://example.com/invite/abc123" }, |
| 53 | { "ExpirationDate", "December 31, 2025" } |
| 54 | }; |
| 55 | |
| 56 | // Act |
| 57 | var html = await renderer.RenderTemplateAsync( |
| 58 | "UserInvitations/UserSignupInvitation", |
| 59 | variables); |
| 60 | |
| 61 | // Assert |
| 62 | await Verify(html, extension: "html"); |
| 63 | } |
| 64 | ``` |
| 65 | |
| 66 | This creates `UserSignupInvitation_RendersCorrectly.verified.html` on first run. |
| 67 | |
| 68 | --- |
| 69 | |
| 70 | ## Reviewing Email Changes |
| 71 | |
| 72 | When a template changes, the test fails with a diff. Review options: |
| 73 | |
| 74 | ### 1. Visual Diff Tool |
| 75 | |
| 76 | ```bash |
| 77 | # Configure diff tool (one-time) |
| 78 | dotnet tool install -g verify.tool |
| 79 | verify accept # Accept all pending changes |
| 80 | verify review # Open diff tool |
| 81 | ``` |
| 82 | |
| 83 | ### 2. Browser Preview |
| 84 | |
| 85 | Open the `.received.html` file in a browser to see the actual rendering. |
| 86 | |
| 87 | ### 3. IDE Integration |
| 88 | |
| 89 | Most IDEs show inline diffs for `.verified.html` vs `.received.html` files. |
| 90 | |
| 91 | --- |
| 92 | |
| 93 | ## Test Each Template Variant |
| 94 | |
| 95 | Create tests for each email template to catch regressions: |
| 96 | |
| 97 | ```csharp |
| 98 | public class EmailTemplateSnapshotTests : IClassFixture<EmailTestFixture> |
| 99 | { |
| 100 | private readonly IMjmlTemplateRenderer _renderer; |
| 101 | |
| 102 | public EmailTemplateSnapshotTests(EmailTestFixture fixture) |
| 103 | { |
| 104 | _renderer = fixture.Services.GetRequiredService<IMjmlTemplateRenderer>(); |
| 105 | } |
| 106 | |
| 107 | [Fact] |
| 108 | public async Task WelcomeEmail_NewUser() => |
| 109 | await VerifyTemplate("Welcome/NewUser", new Dictionary<string, string> |
| 110 | { |
| 111 | { "UserName", "John Doe" }, |
| 112 | { "LoginUrl", "https://example.com/login" } |
| 113 | }); |
| 114 | |
| 115 | [Fact] |
| 116 | public async Task WelcomeEmail_InvitedUser() => |
| 117 | await VerifyTemplate("Welcome/InvitedUser", new Dictionary<string, string> |
| 118 | { |
| 119 | { "UserName", "John Doe" }, |
| 120 | { "InviterName", "Jane Admin" }, |
| 121 | { "OrganizationName", "Acme Corp" } |
| 122 | }); |
| 123 | |
| 124 | [Fact] |
| 125 | public async Task PasswordReset() => |
| 126 | await VerifyTemplate("PasswordReset/PasswordReset", new Dictionary<string, string> |
| 127 | { |
| 128 | { "UserName", "John Doe" }, |
| 129 | { "ResetLink", "https://example.com/reset/abc123" }, |
| 130 | { "ExpirationMinutes", "30" } |
| 131 | }); |
| 132 | |
| 133 | [Fact] |
| 134 | public async Task PaymentReceipt() => |
| 135 | await VerifyTemplate("Billing/PaymentReceipt", new Dictionary<string, string> |
| 136 | { |
| 137 | { "UserName", "John Doe" }, |
| 138 | { "Amount", "$10.00" }, |
| 139 | { "InvoiceNumber", "INV-2025-001" }, |
| 140 | { "Date", "January 15, 2025" } |
| 141 | }); |
| 142 | |
| 143 | private async Task VerifyTemplate( |
| 144 | string templateName, |
| 145 | Dictionary<string, string> variables) |
| 146 | { |
| 147 | var html = await _renderer.RenderTemplateAsync(templateName, variables); |
| 148 | await Verify(html, extension: "html") |
| 149 | .UseMethodName(templateName.Replace("/", "_")); |
| 150 | } |
| 151 | } |
| 152 | ``` |
| 153 | |
| 154 | --- |
| 155 | |
| 156 | ## Scrubbing Dynamic Values |
| 157 | |
| 158 | Some values change between test runs. Scrub them: |
| 159 | |
| 160 | ```csharp |
| 161 | [Fact] |
| 162 | public async Task EmailWithTimestamp_ScrubsDynamicValues() |
| 163 | { |
| 164 | var html = await _renderer.RenderTemplateAsync("Welcome", variables); |
| 165 | |
| 166 | await Verify(html, extension: "html") |
| 167 | .ScrubLinesContaining("Generated at:") |
| 168 | .ScrubInlineGuids(); // Scrubs GUIDs in URLs |
| 169 | } |
| 170 | ``` |
| 171 | |
| 172 | ### Common Scrubbers |
| 173 | |
| 174 | ```csharp |
| 175 | // Scrub dates |
| 176 | .ScrubLinesContaining("Date:") |
| 177 | .AddScrubber(s => Regex.Replace(s, @"\d{4}-\d{2}-\d{2}", "SCRUBBED-DAT |