testing a function inside another function using jest

前端 未结 1 1564
孤街浪徒
孤街浪徒 2021-01-23 19:02

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-packa         


        
相关标签:
1条回答
  • 2021-01-23 19:33

    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');
    
    0 讨论(0)
提交回复
热议问题