Giter Club home page Giter Club logo

Comments (5)

doublesharp avatar doublesharp commented on August 15, 2024 2

@popod @Darkripper214 I made an update to make the module more compatible with jest - the passthrough of the transport.verify() is not possible as the module is mocked before the mock module loads. In version 1.5.5+ it checks to see if nodemailer is available and bypasses it if not - make sure to use this version with jest tests.

I also included some examples for both jest and mocha in the README and source code in ./examples/. For jest, it would look something like the following:

/** 
 * Jest Mock
 * ./__mocks__/nodemailer.js 
 **/
module.exports = require('nodemailer-mock');
/** 
 * Jest Test
 * ./__tests__/my-test.js 
 **/
const { mock } = require('nodemailer');

test('Send an email using the mocked nodemailer', async () => {
  /* ... run your tests that send emails here */

  // check the mock for our sent emails
  const sentEmails = mock.getSentMail();
  // there should be one
  expect(sentEmails.length).toBe(1);
  // and it should match the to address
  expect(sentEmails[0].to).toBe('[email protected]');
});

Let me know if you have any issues, thanks!

from nodemailer-mock.

doublesharp avatar doublesharp commented on August 15, 2024 1

@Darkripper214 I figured out how to do it... you will need to update to [email protected]+ to use the .getMockFor() method which will allow you to pass in the real nodemailer when the mock is loaded.

/** 
 * Jest Mock
 * ./__mocks__/nodemailer.js 
 **/
// load the real nodemailer
const nodemailer = require('nodemailer');
// pass it in when creating the mock using getMockFor()
const nodemailerMock = require('nodemailer-mock').getMockFor(nodemailer);
// export the mocked module
module.exports = nodemailerMock;

from nodemailer-mock.

PhakornKiong avatar PhakornKiong commented on August 15, 2024

I'm exploring using this library with jest. Would be more than happy to assist to update the document if the maintainer is interested. Let me know!

from nodemailer-mock.

doublesharp avatar doublesharp commented on August 15, 2024

@Darkripper214 That would be great! I just don't have time to work on it right now but would be happy to update the documentation if you get it working. Thanks!

from nodemailer-mock.

PhakornKiong avatar PhakornKiong commented on August 15, 2024

@doublesharp I was going to do update this weekend. Basically everything works fine for jest except for the transport.verify() due to the way mocking works.

I was going to ask if it is necessary to provide user ability to use the actual instance to do the transport.verify() in a test scenario?

from nodemailer-mock.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.