javascript - How to mock instantiated object with Jest - Stack Overflow

I have a file (srcdclient) that does this:import DataClient from 'srcclientsdata'const DCl

I have a file (src/dclient) that does this:

import DataClient from 'src/clients/data'

const DClient = new DataClient({ id: 'xxx' })
export default DClient

And I have a file (which I am trying to test) that does this:

import DClient from src/dclient

// Some code

DClient.alert('hello')

I am trying to write expectations on Dclient.alert but failing to do so. I have tried to set up the jest test as:

alertMock = jest.fn();
require('src/dclient').alert = alertMock

But this is not working when I check alertMock.mock.calls even though I know it has been called. I think because dclient returns an instance and actually doesn't have alert defined on it.

How can I set up this jest so I can write expectations on alert?

I have a file (src/dclient) that does this:

import DataClient from 'src/clients/data'

const DClient = new DataClient({ id: 'xxx' })
export default DClient

And I have a file (which I am trying to test) that does this:

import DClient from src/dclient

// Some code

DClient.alert('hello')

I am trying to write expectations on Dclient.alert but failing to do so. I have tried to set up the jest test as:

alertMock = jest.fn();
require('src/dclient').alert = alertMock

But this is not working when I check alertMock.mock.calls even though I know it has been called. I think because dclient returns an instance and actually doesn't have alert defined on it.

How can I set up this jest so I can write expectations on alert?

Share Improve this question edited Mar 25, 2019 at 12:08 Teneff 32.2k13 gold badges76 silver badges104 bronze badges asked Mar 25, 2019 at 11:43 Hommer SmithHommer Smith 27.9k62 gold badges176 silver badges307 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

There are several ways to test this.

The way you are trying works fine, you just need to change it to this:

test('code', () => {
  const alertMock = jest.fn();
  require('src/dclient').default.alert = alertMock;  // <= mock alert on 'default'

  require('./code');  //  <= require the code that calls DClient.alert('hello')
  expect(alertMock).toHaveBeenCalledWith('hello');  // Success!
})

...because src/dclient is an ES6 module with a default export.


The approach I would probably use is to mock the alert function on the DataClient class:

import DataClient from 'src/clients/data';

test('code', () => {
  const alertSpy = jest.spyOn(DataClient.prototype, 'alert');
  alertSpy.mockImplementation(() => {});

  require('./code');  //  <= require the code that calls DClient.alert('hello')
  expect(alertSpy).toHaveBeenCalledWith('hello');  // Success!
})

Jest has a really well-made auto-mocking feature, which generates jest.fn() for each method on the exported object, so you can just:

import DClient from 'src/dclient'; // import the module
jest.mock('src/dclient'); // generate auto-mock

describe('alert', () => {
    beforeAll(() => {
        DClient.alert.mockReturnValue(true);
        // ^ not really needed in the alert case, but you'll get
        // error if the exported object doesn't have alert method
    });

    it('should have been called', () => {
        DClient.alert('hello');
        expect(DClient.alert).toHaveBeenCalledWith()
    });
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744958456a4603340.html

相关推荐

  • javascript - How to mock instantiated object with Jest - Stack Overflow

    I have a file (srcdclient) that does this:import DataClient from 'srcclientsdata'const DCl

    1天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信