mailslurp-examples - sendgrid

https://github.com/mailslurp/examples

Table of Contents

sendgrid/tsconfig.json

{
  "compileOptions": {
    "module": "commonjs"
  }
}

sendgrid/sendgrid.test.ts

// configure SendGrid and MailSlurp APIs
const sendgrid = require('@sendgrid/mail');
sendgrid.setApiKey(process.env.SENDGRID_API_KEY);

const MailSlurp = require("mailslurp-client").default;
const mailslurp = new MailSlurp({apiKey: process.env.API_KEY});

describe("my apps email action", () => {

  it("triggering the action sends an email to a user", async () => {
    // create a new email address to represent a user
    const {id, emailAddress} = await mailslurp.createInbox();
    expect(emailAddress).toContain("@mailslurp.com");
    // trigger an action that we expect will send an email to our user
    triggerEmailAction(emailAddress);

    // receive the email that and verify its contents
    const {subject, body} = await mailslurp.waitForLatestEmail(id, 10000);
    expect(subject).toBe('Thanks for subscribing');
    expect(body).toContain('Welcome!');
  });

  // this is what your app might do to send emails
  // we trigger it here so we can verify that emails are sent
  function triggerEmailAction(emailAddress) {
    sendgrid.send({
      to: emailAddress,
      from: 'example@example.com',
      subject: 'Thanks for subscribing',
      text: 'Welcome!'
    }, false, (err, res) => {
      if(err) {
        console.log(`SendGrid Error : ${err}`)
      }  
    });
  }

});

sendgrid/package.json

{
  "dependencies": {
    "@sendgrid/mail": "^6.4.0",
    "@types/jest": "^24.0.18",
    "jest": "^24.9.0",
    "mailslurp-client": "^6.3.0",
    "ts-jest": "^24.1.0",
    "typescript": "^3.6.3"
  },
  "jest": {
    "roots": [
      "<rootDir>"
    ],
    "transform": {
      "^.+\\.tsx?$": "ts-jest"
    }
  }
}

sendgrid/README.md

# MailSlurp SendGrid Test Example

See code for instructions.

sendgrid/Makefile

node_modules:
	npm install

include ../.env
include .env
test: node_modules
	SENDGRID_API_KEY=$(SENDGRID_API_KEY) API_KEY=$(API_KEY) npx jest