<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Jose</title>
    <description>The latest articles on Forem by Jose (@zamora).</description>
    <link>https://forem.com/zamora</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F56610%2F2b4cca56-8f89-420b-8712-6f6c48c2262a.jpg</url>
      <title>Forem: Jose</title>
      <link>https://forem.com/zamora</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/zamora"/>
    <language>en</language>
    <item>
      <title>End-To-End Email Testing With MailSlurp</title>
      <dc:creator>Jose</dc:creator>
      <pubDate>Fri, 09 Feb 2018 14:01:20 +0000</pubDate>
      <link>https://forem.com/sparkpost/end-to-end-email-testing-with-mailslurp-57mk</link>
      <guid>https://forem.com/sparkpost/end-to-end-email-testing-with-mailslurp-57mk</guid>
      <description>&lt;h3&gt;
  
  
  How To Conduct End-To-End Testing With MailSlurp
&lt;/h3&gt;

&lt;p&gt;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? &lt;a href="https://www.mailslurp.com/"&gt;MailSlurp&lt;/a&gt; is an awesome &lt;em&gt;free&lt;/em&gt; 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.&lt;/p&gt;

&lt;p&gt;In this post, we will use the MailSlurp &lt;a href="https://www.npmjs.com/package/mailslurp-client"&gt;Javascript SDK&lt;/a&gt; to write an end-to-end test for a node function that sends an email. We’ll be using &lt;a href="https://sparkpost.com"&gt;SparkPost&lt;/a&gt; to send the email. If you aren’t already sending email, here’s a &lt;a href="https://www.sparkpost.com/docs/getting-started/getting-started-sparkpost/"&gt;good place to start (for free)&lt;/a&gt;. For our test we’ll use &lt;a href="https://facebook.github.io/jest/"&gt;Jest&lt;/a&gt;, but any framework should work just fine.&lt;/p&gt;

&lt;p&gt;Our code will be using a few es2015 features, so be sure to be running a version of node that supports them (version &lt;a href="http://node.green/"&gt;6.4.0&lt;/a&gt; or above should be fine). We’ll be installing dependencies using &lt;a href="https://www.npmjs.com/"&gt;npm&lt;/a&gt; version 5.6.0.&lt;/p&gt;

&lt;p&gt;All the code for this &lt;a href="https://github.com/jgzamora/mailslurp-sparkpost"&gt;demo&lt;/a&gt; can be found here.&lt;/p&gt;

&lt;h3&gt;
  
  
  Getting Set Up
&lt;/h3&gt;

&lt;p&gt;Alright, let’s get started! The first step is to get set up with a MailSlurp account. It’s quick and free: &lt;a href="https://www.mailslurp.com/sign-up"&gt;Sign up&lt;/a&gt; for MailSlurp. Next, log in to your dashboard and grab your API key.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mVBg44H3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/dm7goc8dt9qj1laipbvs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mVBg44H3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/dm7goc8dt9qj1laipbvs.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"dependencies"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"sparkpost"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^2.1.2"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"mailslurp-client"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^0.1.1514850454"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"jest"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^22.1.2"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And run &lt;code&gt;npm install&lt;/code&gt; Here is our full &lt;a href="https://github.com/jgzamora/mailslurp-sparkpost/blob/master/package.json"&gt;package.json&lt;/a&gt;. Normally packages like Mailslurp client and jest should go under “dev-dependencies”, but we kept it simple for this demo.&lt;/p&gt;

&lt;p&gt;Now from the top of our test file, we require Mailslurp-client and our &lt;code&gt;sendEmail&lt;/code&gt; code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// test file: mailslurp.test.js&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MailslurpClient&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mailslurp-client&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sendEmail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./sendEmail.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// our SparkPost email function&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You would import whatever function you use to send email.&lt;/p&gt;

&lt;p&gt;Right after, we initialize the MailSlurp client. This is also a good spot to store your API key in a variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// initialize the MailSlurp Client&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;slurp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;MailslurpClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;InboxcontrollerApi&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;// Sign up at MailSlurp.com to get one of these for free.&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;slurpKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;MS_APIKEY&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We used &lt;a href="https://medium.com/ibm-watson-data-lab/environment-variables-or-keeping-your-secrets-secret-in-a-node-js-app-99019dfff716"&gt;environment&lt;/a&gt; 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.&lt;/p&gt;

&lt;p&gt;Next step is to create an inbox we can send to with the MailSlurp client’s &lt;code&gt;createRandomInboxUsingPOST&lt;/code&gt; method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;SendEmail Tests&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;inboxId&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nx"&gt;beforeAll&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Creates the inbox and set address &amp;amp; inboxId for use in tests&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;slurp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createRandomInboxUsingPOST&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;slurpKey&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(({&lt;/span&gt; &lt;span class="nx"&gt;payload&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;address&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// the email address we will be sending to&lt;/span&gt;
        &lt;span class="nx"&gt;inboxId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// UUID used to identify the inbox&lt;/span&gt;
      &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

 &lt;span class="cm"&gt;/* tests will go here */&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The important things here are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Execute this code before you run your tests&lt;/li&gt;
&lt;li&gt;Store both address &amp;amp; id from the response as variables, so you can send to and access your inbox&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now for the actual test we want to send an email (&lt;code&gt;sendEmail()&lt;/code&gt; function for us) and then use the MailSlurp client’s &lt;code&gt;getEmailsForInboxUsingGET&lt;/code&gt; method to get the email from the inbox:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Email delivers&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// increase the default jest timeout to wait for delivery&lt;/span&gt;
    &lt;span class="nx"&gt;jest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;60000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// pass address given to us by MailSlurp&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;sendEmail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(({&lt;/span&gt; &lt;span class="nx"&gt;total_accepted_recipients&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// make sure SparkPost accepted the request&lt;/span&gt;
        &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;total_accepted_recipients&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;toEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// Get the list of emails from the inbox&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;slurp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getEmailsForInboxUsingGET&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;slurpKey&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;inboxId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;minCount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// minimum number of emails to wait for, default wait time is 60s&lt;/span&gt;
          &lt;span class="p"&gt;})&lt;/span&gt;
          &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(({&lt;/span&gt; &lt;span class="nx"&gt;payload&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// console.log(payload) // uncomment this to see the full email&lt;/span&gt;
            &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;toHaveLength&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;subject&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;toEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;MailSlurp Test Email&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
          &lt;span class="p"&gt;});&lt;/span&gt;
      &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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 &lt;code&gt;address&lt;/code&gt; to our &lt;code&gt;sendEmail&lt;/code&gt; function, and then passing &lt;code&gt;inboxId&lt;/code&gt; to &lt;code&gt;getEmailsForInboxUsingGET&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;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 &lt;a href="https://github.com/jgzamora/mailslurp-sparkpost/blob/master/sendEmail.js"&gt;sendEmail.js&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That’s it! Run the test however you usually do. Ours is set up with npm scripts in the package.json, which runs with &lt;code&gt;npm test&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Next Steps
&lt;/h3&gt;

&lt;p&gt;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 &lt;a href="https://facebook.github.io/jest/docs/en/snapshot-testing.html"&gt;snapshot tests&lt;/a&gt; to ensure you are sending the content you want.&lt;/p&gt;

&lt;p&gt;If this was interesting to you, you should check out the rest of &lt;a href="https://www.mailslurp.com/"&gt;MailSlurp&lt;/a&gt; features. You can reach us on &lt;a href="https://twitter.com/SparkPost"&gt;Twitter&lt;/a&gt; for any questions. And you can also send 15,000 emails a month for free on &lt;a href="https://pages.sparkpost.com/bl-account-signup-generic.html?src=Blog&amp;amp;pc=bl-rfi-generic-2017&amp;amp;utm_source=blog&amp;amp;utm_medium=blog&amp;amp;utm_campaign=all&amp;amp;utm_content=signup"&gt;SparkPost&lt;/a&gt;! Special thanks to &lt;a href="https://github.com/jackmahoney"&gt;Jack Mahoney&lt;/a&gt; for building MailSlurp and answering my questions for this post.&lt;/p&gt;

&lt;p&gt;–&lt;a href="https://twitter.com/josegzamora"&gt;Jose&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The post &lt;a href="https://www.sparkpost.com/blog/email-testing-mailslurp/"&gt;End-To-End Email Testing With MailSlurp&lt;/a&gt; appeared first on &lt;a href="https://www.sparkpost.com"&gt;SparkPost&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>email</category>
      <category>testing</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
