How can I send a report via nodemailer with Cypress?

795 Views Asked by At

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: "[email protected]",
        pass: "********."
      }
    }
    const client = nodemailer.createTransport(sgTransport(options));
  
    const email = {
      from: '[email protected]',
      to: '[email protected]',
      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"]')
  
    
})
1

There are 1 best solutions below

2
On

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")