7

Does anyone know how to test mail-notifier module in nodejs? I want to test the logic inside n.on():

const notifier = require('mail-notifier');

const imap = {
  user: 'mailId',
  password: 'password',
  host: 'host',
  port: 'port',
  tls: true,
  tlsOptions: { rejectUnauthorized: false }
};

const n = notifier(imap);

n.on('mail', async function (mail) {
  console.log('mail:', mail);
}).start();
2
  • So exactly what part of the code is it you want to test?
    – James
    Aug 7, 2018 at 11:42
  • @James, I want to test whatever logic thats inside the n.on() part where I have given the console.log() Aug 7, 2018 at 11:49

2 Answers 2

2
+25

Assuming your code exists in notifier.js it can be tested exactly as written as follows:

// mock 'mail-notifier'
jest.mock('mail-notifier', () => {
  // create mock for 'start'
  const startMock = jest.fn();
  // create mock for 'on', always returning the same result
  const onResult = { start: startMock };
  const onMock = jest.fn(() => onResult);
  // create mock for 'notifier', always returning the same result
  const notifierResult = { on: onMock };
  const notifierMock = jest.fn(() => notifierResult);
  // return the mock for notifier
  return notifierMock;
});

// get the mocks we set up from the mocked 'mail-notifier'
const notifierMock = require('mail-notifier');
const onMock = notifierMock().on;
const startMock = onMock().start;
// clear the mocks for notifier and on since we called
// them to get access to the inner mocks
notifierMock.mockClear();
onMock.mockClear();

// with all the mocks set up, require the file
require('./notifier');

describe('notifier', () => {

  it('should call notifier with the config', () => {
    expect(notifierMock).toHaveBeenCalledTimes(1);
    expect(notifierMock).toHaveBeenCalledWith({
      user: 'mailId',
      password: 'password',
      host: 'host',
      port: 'port',
      tls: true,
      tlsOptions: { rejectUnauthorized: false }
    });
  });

  it('should call on with mail and a callback', () => {
    expect(onMock).toHaveBeenCalledTimes(1);
    expect(onMock.mock.calls[0][0]).toBe('mail');
    expect(onMock.mock.calls[0][1]).toBeInstanceOf(Function);
  });

  it('should call start', () => {
    expect(startMock).toHaveBeenCalledTimes(1);
    expect(startMock).toHaveBeenCalledWith();
  });

  describe('callback', () => {

    const callback = onMock.mock.calls[0][1];

    it('should do something', () => {
      // test the callback here
    });

  });

});
2
  • I just realized a way to fully test the code exactly as it is written. I've updated my response with the test. Aug 11, 2018 at 19:05
  • @TonyMathew let me know if this covers what you need or if you have additional questions Aug 13, 2018 at 15:35
1

Assuming you are just looking to test that the callback is triggered then you can use a mock function e.g.

const mailCallback = jest.fn();
n.on('mail', mailCallback).start();
...
expect(mailCallback.mock.calls.length).toBe(1); // or however many calls you expect
2
  • James, thanks for the reply :) . But do you know how I can test the functionalities inside the callback function? And how do I send mock requests to the listener? Aug 7, 2018 at 13:26
  • @TonyMathew I think you need to update your question to be more specific, "test the functionalities" is far too vague.
    – James
    Aug 7, 2018 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.