javascript - Jest mock a module to produce different results on function calls - Stack Overflow

I have a module: foo.jsmodule.exports = async () => {...}This module is called in another modul

I have a module:

// foo.js
module.exports = async () => {
  ...
}

This module is called in another module, which behaviour I'm testing:

// bar.js
const one = await foo();
const two = await foo();

I want to mock foo with Jest, so that multiple calls on it return different results. More precisely, the first call to be successful, the second one to return an error.

This is my mocking mechanism:

const fooMock = jest.mock('../src/foo')
fooMock
  .mockImplementationOnce(() => Promise.resolve({ id: 'asdf' }))
  .mockImplementationOnce(() => Promise.reject(new Error('some error')))

The problem is that mockImplementationOnce is not a function of jest.mock(). It's only a function of jest.fn(). The jest.mock() object only has mockImplementation which will mock and seal the return result of the mocked function and doesn't allow for different results on multiple calls.

How can I mock the module to return different results on 1st and on 2nd call?

Inspiration taken from the jest docs here.

UPDATE:

I also tried this approach:

  jest.mock('../src/foo', () => jest.fn()
      .mockImplementationOnce(() => Promise.resolve({ _id: 'asdf' }))
      .mockImplementationOnce(() => Promise.reject('some error'))
  )

But now no mocking is happening at all.

I have a module:

// foo.js
module.exports = async () => {
  ...
}

This module is called in another module, which behaviour I'm testing:

// bar.js
const one = await foo();
const two = await foo();

I want to mock foo with Jest, so that multiple calls on it return different results. More precisely, the first call to be successful, the second one to return an error.

This is my mocking mechanism:

const fooMock = jest.mock('../src/foo')
fooMock
  .mockImplementationOnce(() => Promise.resolve({ id: 'asdf' }))
  .mockImplementationOnce(() => Promise.reject(new Error('some error')))

The problem is that mockImplementationOnce is not a function of jest.mock(). It's only a function of jest.fn(). The jest.mock() object only has mockImplementation which will mock and seal the return result of the mocked function and doesn't allow for different results on multiple calls.

How can I mock the module to return different results on 1st and on 2nd call?

Inspiration taken from the jest docs here.

UPDATE:

I also tried this approach:

  jest.mock('../src/foo', () => jest.fn()
      .mockImplementationOnce(() => Promise.resolve({ _id: 'asdf' }))
      .mockImplementationOnce(() => Promise.reject('some error'))
  )

But now no mocking is happening at all.

Share Improve this question edited Jul 13, 2021 at 8:29 Milkncookiez asked Jul 12, 2021 at 18:00 MilkncookiezMilkncookiez 7,45712 gold badges68 silver badges109 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

You should use mockFn.mockReturnValueOnce(value):

Accepts a value that will be returned for one call to the mock function. Can be chained so that successive calls to the mock function return different values

After calling jest.mock('./src/foo'), you should import the ./src/foo module and it will be a mocked version instead of using the return value.

const fooMock = require('./src/foo');

jest.mock('./src/foo');

test('should pass', () => {
  fooMock.mockReturnValue('default')
         .mockReturnValueOnce('first call')
         .mockReturnValueOnce('second call')

  // 'first call', 'second call', 'default', 'default'
  console.log(fooMock(), fooMock(), fooMock(), fooMock());
})

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信