<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Jose Zamora on Medium]]></title>
        <description><![CDATA[Stories by Jose Zamora on Medium]]></description>
        <link>https://medium.com/@jzamora?source=rss-3546c8d371de------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*MLZgPfOxdXl9Ufmj7oR7cg.jpeg</url>
            <title>Stories by Jose Zamora on Medium</title>
            <link>https://medium.com/@jzamora?source=rss-3546c8d371de------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 14:10:38 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jzamora/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[End-To-End Email Testing With MailSlurp]]></title>
            <link>https://medium.com/@jzamora/end-to-end-email-testing-with-mailslurp-50a52834ca47?source=rss-3546c8d371de------2</link>
            <guid isPermaLink="false">https://medium.com/p/50a52834ca47</guid>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[email]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[sparkpost]]></category>
            <dc:creator><![CDATA[Jose Zamora]]></dc:creator>
            <pubDate>Sat, 10 Feb 2018 00:36:00 GMT</pubDate>
            <atom:updated>2018-02-10T00:36:00.663Z</atom:updated>
            <content:encoded><![CDATA[<p>If you send email from an application, you probably have tests that ensure your code is attempting to send that email. But do you have a right way of testing whether that email got sent? <a href="https://www.mailslurp.com/">MailSlurp</a> is an awesome free tool that helps you do that. It allows you to easily generate random inboxes to send email to and then confirm delivery through its API.</p><p>In this post, we will use the MailSlurp <a href="https://www.npmjs.com/package/mailslurp-client">Javascript SDK</a> to write an end-to-end test for a node function that sends an email. We’ll be using <a href="https://www.sparkpost.com/">SparkPost</a> to send the email. If you aren’t already sending email, here’s a <a href="https://www.sparkpost.com/docs/getting-started/getting-started-sparkpost/">good place to start (for free)</a>. For our test we’ll use <a href="https://facebook.github.io/jest/">Jest,</a> but any framework should work just fine.</p><p>Our code will be using a few es2015 features, so be sure to be running a version of node that supports them (version <a href="http://node.green/">6.4.0 </a>or above should be fine). We’ll be installing dependencies using <a href="https://www.npmjs.com/">npm</a> version 5.6.0.</p><p>All the code for this <a href="https://github.com/jgzamora/mailslurp-sparkpost">demo</a> can be found here.</p><h3>Getting Set Up</h3><p>Alright, let’s get started! The first step is to get set up with a MailSlurp account. It’s quick and free: <a href="https://www.mailslurp.com/sign-up">Sign up</a> for MailSlurp. Next, log in to your dashboard and grab your API key.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*NrVPYCy9gBt3y3fB." /><figcaption><a href="https://www.mailslurp.com/dashboard">https://www.mailslurp.com/dashboard</a></figcaption></figure><p>Once you have your API Key, we can get started with the test setup. Make sure you have these dependencies in your package.json — here’s what ours looks like, using the SparkPost client library:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3ef1323c8f27e8ea27e275b5de6c2502/href">https://medium.com/media/3ef1323c8f27e8ea27e275b5de6c2502/href</a></iframe><p>And run npm install. Here is our full <a href="https://github.com/jgzamora/mailslurp-sparkpost/blob/master/package.json">package.json</a>. Normally packages like Mailslurp client and jest should go under “dev-dependencies”, but we kept it simple for this demo.</p><p>Now from the top of our test file, we require Mailslurp-client and our sendEmail code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/93f161b01392aa1f968d65fe9741daaf/href">https://medium.com/media/93f161b01392aa1f968d65fe9741daaf/href</a></iframe><p>You would import whatever function you use to send email.</p><p>Right after, we initialize the MailSlurp client. This is also a good spot to store your API key in a variable.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/17e51ff822ca6d76ac95315bbf8c666a/href">https://medium.com/media/17e51ff822ca6d76ac95315bbf8c666a/href</a></iframe><p>We used <a href="https://medium.com/ibm-watson-data-lab/environment-variables-or-keeping-your-secrets-secret-in-a-node-js-app-99019dfff716">environment</a> variables to access ours; if you store credentials a different way, that’s ok too. Our slurpKey variable will be the first parameter we pass to every MailSlurp client method.</p><p>Next step is to create an inbox we can send to with the MailSlurp client’s createRandomInboxUsingPOST method:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fe1bc7512ceb97908177d757390b064f/href">https://medium.com/media/fe1bc7512ceb97908177d757390b064f/href</a></iframe><p>The important things here are:</p><ul><li>Execute this code before you run your tests</li><li>Store both address &amp; id from the response as variables, so you can send to and access your inbox</li></ul><p>Now for the actual test we want to send an email ( sendEmail() function for us) and then use the MailSlurp client’s getEmailsForInboxUsingGET method to get the email from the inbox:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6b7e4f53e1740cd319e32430fec22076/href">https://medium.com/media/6b7e4f53e1740cd319e32430fec22076/href</a></iframe><p>Email delivery can take more than a few seconds, so make sure your test waits around long enough for the email to be delivered. In the code snippet above, we change the timeout threshold to handle that. Note that we are passing address to our sendEmail function, and then passing inboxId to getEmailsForInboxUsingGET.</p><p>For this test we first asserted that MailSlurp returned an array of emails with a length of 1 (we only sent one email). Then to make sure it was the email we sent, we asserted that its subject was ‘MailSlurp Test Email’ as defined in <a href="https://github.com/jgzamora/mailslurp-sparkpost/blob/master/sendEmail.js">sendEmail.js</a></p><p>That’s it! Run the test however you usually do. Ours is set up with npm scripts in the package.json, which runs with npm test.</p><h3>Next Steps</h3><p>There is plenty of room for more assertions in our example test. When using an email delivery service like SparkPost in combination with complex HTML templates, an email’s body can get pretty complicated. To make assertions about an email’s actual content, we suggest using an advanced email body parser. Once you are able to extract the HTML/text content from the body, you could easily use <a href="https://facebook.github.io/jest/docs/en/snapshot-testing.html">snapshot tests</a> to ensure you are sending the content you want.</p><h3>️✌️</h3><p>If this was interesting to you, you should check out the rest of <a href="https://www.mailslurp.com/">MailSlurp’s features</a>. Feel free to reach out to me on twitter <a href="https://twitter.com/josegzamora">@josegzamora</a> if you have any questions. And you can also send 15,000 <a href="https://pages.sparkpost.com/bl-account-signup-generic.html?src=Blog&amp;pc=bl-rfi-generic-2017&amp;utm_source=blog&amp;utm_medium=blog&amp;utm_campaign=all&amp;utm_content=signup">emails a month for free</a> on SparkPost! Special thanks to <a href="https://github.com/jackmahoney">Jack Mahoney</a> for building MailSlurp and answering my questions for this post.</p><p><a href="https://twitter.com/josegzamora">–Jose</a></p><p><em>Originally published at </em><a href="https://www.sparkpost.com/blog/email-testing-mailslurp/"><em>www.sparkpost.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=50a52834ca47" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>