Skip to content

Instantly share code, notes, and snippets.

@qunabu
Created November 26, 2020 09:33
Show Gist options
  • Save qunabu/f6062e6e45f3d0650278fa51258dc6e9 to your computer and use it in GitHub Desktop.
Save qunabu/f6062e6e45f3d0650278fa51258dc6e9 to your computer and use it in GitHub Desktop.
Test Mocki
const should = require('should');
const mockery = require('mockery');
const nodemailerMock = require('nodemailer-mock');
describe('Tests that send email', async () {
before(async () {
mockery.enable({warnOnUnregistered: false}); // Enable mockery to mock objects
mockery.registerMock('nodemailer', nodemailerMock)
});
afterEach(async () {
nodemailerMock.mock.reset(); // Reset the mock back to the defaults after each test
});
after(async () {
// Remove our mocked nodemailer and disable mockery
mockery.deregisterAll();
mockery.disable();
});
it('should send an email using nodemailer-mock', async () {
app.sendEmail(); // call a service that uses nodemailer
const sentMail = nodemailerMock.mock.getSentMail(); // get the array of emails we sent
sentMail.length.should.be.exactly(1);// we should have sent one email
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment