Search code examples
smtpcypressnodemailer

unable to send email via cypress-email-result


I want to email the cypress test results programmatically preferably a single file for all spec files. I am using cypress-email-results But the file cypress/plugins/index.js is not running. I am checking this by console.log on first line

    console.log("inside plugin/index.js");
const nodemailer = require('nodemailer');

var transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
      user: '**********@gmail.com',
      pass: '***********'
    }
  });



module.exports = (on, config) => {
    require('cypress-email-results')(on, config, {
      email: ['***********@gmail.com'],
      transporter
    })
  }

Solution

  • If you are using Cypress version 10 or greater, the plugins configuration has changed.

    You now need to put them in the cypress.config.js which is at the root of the project.

    The quickest way to test it out is to import your old cypress/plugins/index.js into the new file, like this

    const { defineConfig } = require('cypress')
    
    module.exports = defineConfig({
      e2e: {
        setupNodeEvents(on, config) {
          return require('./cypress/plugins/index.js')(on, config) // run old config
        },
        baseUrl: '...',
      },
    })
    

    If that works you can migrate the code from cypress/plugins/index.js to setupNodeEvents() one by one and run your tests in between each step.

    It may be that not all plugins will migrate.


    Looking at cypress-email-results package, it looks straight forward to upgrade.

    const { defineConfig } = require('cypress')
    
    module.exports = defineConfig({
      e2e: {
        setupNodeEvents(on, config) {
    
          require('cypress-email-results')(on, config, {
            email: ['user1@email.com', 'user2@email.com'],
          })
        },
        baseUrl: '...',
      },
    })
    

    Nodemailer configuration

    According to the sample given in cypress-email-results repository, nodemailer is set up as follows

    // create reusable transporter object using the default SMTP transport
    // the settings could come from .env file or environment variables
    const host = process.env.SMTP_HOST || 'localhost'
    const port = Number(process.env.SMTP_PORT || 7777)
    
    // create your own SMTP transport
    const transport = nodemailer.createTransport({
      host,
      port,
      secure: port === 456,
    })
    
    module.exports = (on, config) => {
      require('cypress-email-results')(on, config, {
        email: ['user1@email.com', 'user2@email.com'],
        // pass your transport object
        transport,
      })
    }
    

    So inside createTransport, set up host and port instead of service and auth.