Home > Software design >  Why does jest mockResolvedValueOnce called multiple times returns the same value?
Why does jest mockResolvedValueOnce called multiple times returns the same value?

Time:02-10

I have a class method where I trigger a @google-cloud/firestore multiple times. I would like to mock the call over the same .get() method multiple times. Using a mockResolvedValueOnce multiple times with different values to return, the 2nd value is ignored.

    jest.doMock('@google-cloud/firestore', () => class {
      collection () {
        return {
          get: jest.fn().mockResolvedValue({
            docs: []
          }),
          doc: () => {
            return {
              set: jest.fn(),
              get: jest.fn().mockResolvedValueOnce({})
            }
          },
          limit: () => {
            return {
              get: jest.fn().mockResolvedValue({ empty: true })
            }
          },
          onSnapshot: jest.fn(),
          select: () => {
            return {
              get: jest.fn() // <------------ MULTIPLE CALLS CHAINED BELOW
                .mockResolvedValueOnce({
                  size: 1
                }).mockResolvedValueOnce({
                  size: 2
                })
            }
          }
        }
      }
    })

When I console.log(snapshot.size) it returns me the same value "1" twice for both calls.

if (isEmptyModels || isStatsEmptyModels) {
  // ...
  console.log('           
  • Related