1

What would be the best way to use nodemailer with Cypress? I've been playing with the code bellow for the while now but with no avail. I am getting an error "cy.task('sendMail') failed with the following error:

sendAnEmail is not a function

Because this error occurred during a after all hook we are skipping all of the remaining tests."

Thanks for any tips and advices.

//Cypress config file
const { defineConfig } = require("cypress");
const sendAnEmail = require("nodemailer")

module.exports = defineConfig({
  pageLoadTimeout: 180000,
  e2e: {
    setupNodeEvents(on, config) {
      on('task', {
        sendMail (message) {
          return sendAnEmail(message);
        }
      })
    },
  },
});

//Nodemailer file
const sendAnEmail = (message) => {
    function sendAnEmail()
    const nodemailer = require('nodemailer');
    const sgTransport = require('nodemailer-sendgrid-transport');
    const options = {
      auth: {
        user: "glorioustester123@outlook.com",
        pass: "********."
      }
    }
    const client = nodemailer.createTransport(sgTransport(options));
  
    const email = {
      from: 'glorioustester123@outlook.com',
      to: 'some.email@gmail.com',
      subject: 'Hello',
      text: message,
      html: '<b>Hello world</b>'
    };
    client.sendMail(email, function(err, info) {
      return err? err.message : 'Message sent: ' + info.response;
    });
  }

//The Cypress test file
/// <reference types = "cypress" />


after(() => {
    cy.task('sendMail', 'This will be output to email address')
      .then(result => console.log(result));
  })

//zadanie A
it("navstiv stranku a vyhladaj a elementy v casti Framework Support", ()=>{
    cy.visit('https://sortablejs.github.io/Sortable/#cloning')
    
    
    cy.get('.col-6').find('a') 
})
//zadanie B
it("navstiv stranku a vyhladaj prvy a element casti v Framework Support", ()=>{
    cy.visit('https://sortablejs.github.io/Sortable/#cloning')
     
    cy.get('[href="https://github.com/SortableJS/Vue.Draggable"]')
  
    cy.get('.col-6').contains('a')
    //contains najde prvy vyskyt, v tomto pripade to pasuje do zadania



})
//zadanie C
it("navstiv stranku vyhladaj posledny a element v casti Framework Support ", ()=>{
    cy.visit('https://sortablejs.github.io/Sortable/#cloning')
     
    cy.get('[href="https://github.com/SortableJS/ember-sortablejs"]')
  
    
})
Giacomo
  • 163
  • 10
Dave Han
  • 35
  • 5

1 Answers1

3

You nodemailer file needs adjusting a bit. The is no export which is why the message sendAnEmail is not a function

const nodemailer = require('nodemailer');
const sgTransport = require('nodemailer-sendgrid-transport');

export function sendAnEmail(message)
    
  const options = {
    ...
  }
  const client = nodemailer.createTransport(sgTransport(options));
  
  const email = {
    ...
  };

  client.sendMail(email, function(err, info) {
    return err? err.message : 'Message sent: ' + info.response;
  });
}

Also, in cypress.config.js import it with a relative path

const { defineConfig } = require("cypress");
const sendAnEmail = require("./nodemailer")

and to be a clean-coder, us a different name from the npm package (something like

const sendAnEmail = require("./send-an-email")
Giacomo
  • 163
  • 10
  • Hi, thanks, so I tried all of these, but the error persists for whatever reason. I also tried to set a relative path to nodemailer but that crashed cypress entirely, so I had to revert it back – Dave Han Dec 07 '22 at 10:36
  • Try etherialmail instead - there's some good info here [How to integrate Ethereal email service with Cypress 10](https://stackoverflow.com/a/73641599/20706556) and here [cypress-ethereal-email-example](https://github.com/bahmutov/cypress-ethereal-email-example) – Giacomo Dec 08 '22 at 09:15