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"]')
})