1

how can i test the below snippet using jest. I am trying to test the winston custom format the printf

// sample.js

import {aa:{b}} = require("thirparty-package")

const a = () => {
   return b((log) => {
     return `log message will be ${log.message}`
   })
}

module.exports = {
  a
}


// sample.test.js
const customFunctions = require('./sample')

test('should check b function is called and returns a string', () => {
   expect(customFunctions.a).toHaveBeenCalled() // throwing error 
    //jest.fn() value must be a mock function or spy.
})
skyboyer
  • 22,209
  • 7
  • 57
  • 64
Learner
  • 8,379
  • 7
  • 44
  • 82
  • It's unclear what's going on. Why should `a` be called? You're not calling it. – Estus Flask Feb 02 '19 at 10:33
  • then how can i know that b will be called. Check the orginal snippet it will give you better view https://github.com/winstonjs/winston#formats. here if we need a customFormat we can give that but customFormat is not a function it is just a variable. Since i was not able to test it i moved out the customFormat and made it as a functon tried to test it. But it is not working – Learner Feb 02 '19 at 10:37

1 Answers1

1

If it's b that needs to be tested then it should be a spy, not a.

Third-party module should be mocked (a demo):

const bMock = jest.fn();
jest.mock('thirparty-package', () => ({ aa: { b: bMock } }));
const { a } = require('./sample');
a();
const callback = bMock.mock.calls[0][0]; 
expect(callback).toEqual(expect.any(Function));
expect(callback({ message: 'foo' })).toBe('log message will be foo');
Estus Flask
  • 206,104
  • 70
  • 425
  • 565
  • i have already mocked the entire winston library with jest.genMockFromModule('winston'); – Learner Feb 02 '19 at 11:50
  • i tried the code but it is not covering the line, still the lines are uncovered – Learner Feb 02 '19 at 12:02
  • I don't think that `genMockFromModule` will generate nested `aa` object.. You may also need to test a callback, like `const cb = bMock.calls[0][1]; expect(cb({ message: ... })).toBe(...)`; – Estus Flask Feb 03 '19 at 07:24
  • i didn't understand can you update the answer snippet – Learner Feb 03 '19 at 10:35
  • i have posted the entire code , can you check this one https://stackoverflow.com/questions/54502290/testing-the-logger-using-jest-customformat-with-the-default-printf and suggest a solution – Learner Feb 03 '19 at 11:18
  • I updated the answer. I have hard time following the code in another question, it's quite complex. Considering that it works the same way as this one, the answer would be the same. Replace `thirparty-package`, `aa`, etc. with things that are applicable to your case. – Estus Flask Feb 04 '19 at 07:33
  • still didnt worked bmock.calls is undefined, can you try to check the question in local set up https://stackoverflow.com/questions/54502290/testing-the-logger-using-jest-customformat-with-the-default-printf. i think then you will be able to understand the problem – Learner Feb 04 '19 at 14:43
  • Yes, there was a typo, it should be `.mock.calls`. – Estus Flask Feb 05 '19 at 07:01
  • mock.calls is an empty array – Learner Feb 07 '19 at 06:33
  • In case a spy was called, it shouldn't be empty. If it's empty this means that it wasn't mocked properly. I didn't test it locally because the thing I suggest is fundamental to Jest, I'm positive it's workable. I updated the answer with a demo. – Estus Flask Feb 07 '19 at 10:42
  • thanks it got worked, since i was mocking module in __mocks__ folder – Learner Feb 09 '19 at 10:51
  • Glad you sorted this out. – Estus Flask Feb 09 '19 at 12:02