最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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());
})
发布评论

评论列表(0)

  1. 暂无评论