title: jest mock toHaveBeenCalledWith expecting different arguments
body: <p>I am testing this function in <code>TripService</code>:</p>
  
  <pre><code>async createAndMapTrips&lt;T extends ITripEntity&gt;(
          driver: DriverEntity,
          entities: T[],
          intervalInMinutes: number = 10,
      ): Promise&lt;[TripEntity[], T[]]&gt; {
          const trips: TripEntity[] = [];
  
          const [tripsDb] = await this.tripRepository.getAndCountTrips(driver, {
              driverId: driver.driverId,
              fromEndTime: moment.utc(entities[0].timestamp).subtract(intervalInMinutes, 'minutes').toISOString(),
              toEndTime: moment.utc(entities[entities.length - 1].timestamp).add(intervalInMinutes, 'minutes').toISOString(),
          });
  
          this.logger.log(tripsDb);
  
          return [
              trips,
              entities.map((entity: T, idx: number) =&gt; {
                  let trip: TripEntity = this.findLastTripWithinRangeFromEntity(trips, entity);
                  if (!trip) {
                      trip = this.findLastTripWithinRangeFromEntity(tripsDb, entity);
                      if (!trip) {
                          this.logger.log('Trip does not exist, creating new trip');
                          trip = this.tripRepository.create({
                              driver,
                              id: this.generateRandomTripId(),
                              startTime: moment.utc(entity.timestamp).toISOString(),
                              endTime: moment.utc(entity.timestamp).toISOString(),
                          });
                      }
                      trips.push(trip);
                  }
                  trip.endTime = moment.utc(entity.timestamp).toISOString();
                  entity.trip = trip;
                  return entity;
              }),
          ];
      }
  
      private findLastTripWithinRangeFromEntity&lt;T extends ITripEntity&gt;(trips: TripEntity[], entity: T, intervalInMinutes: number = 10): TripEntity {
          return findLast(
              trips,
              ((t: TripEntity) =&gt;
                  moment.utc(entity.timestamp).isSameOrAfter(moment.utc(t.startTime), 'milliseconds')
                  &amp;&amp; moment.utc(entity.timestamp).diff(moment.utc(t.endTime), 'minutes') &lt;= intervalInMinutes),
          );
      }
  </code></pre>
  
  <p>This is my test:</p>
  
  <pre><code>describe('TripService.createAndMapTrips()', () =&gt; {
      const entities: ITripEntity[] = [
        { timestamp: '2019-05-07T11:10:00.000Z', trip: null },
      ];
  
      const driver: DriverEntity = plainToClass(DriverEntity, {
        id: 1,
        driverId: 'one',
        clientId: 'one',
      });
  
      const tripsDb: TripEntity[] = plainToClass(TripEntity, [
        { id: '1', startTime: '2019-05-07T10:50:00.000Z', endTime: '2019-05-07T11:01:00.000Z' },
        // this should be selected as it is the last one according to time
        { id: '2', startTime: '2019-05-07T10:55:00.000Z', endTime: '2019-05-07T11:06:00.000Z' },
      ]);
  
      const result: [TripEntity[], ITripEntity[]] = [
        plainToClass(TripEntity, [
          { id: '2', startTime: '2019-05-07T10:55:00.000Z', endTime: '2019-05-07T11:10:00.000Z' },
        ]),
        [{
          timestamp: '2019-05-07T11:10:00.000Z', trip: plainToClass(TripEntity, {
            id: '2',
            startTime: '2019-05-07T10:55:00.000Z',
            endTime: '2019-05-07T11:10:00.000Z',
          }),
        }],
      ];
  
      it('should map existing trips if the trips already exist within the time range requested', async () =&gt; {
        tripRepositoryMock.getAndCountTrips.mockReturnValue([[tripsDb], 2]);
  
        const findLastTripWithinRangeFromEntity = jest.spyOn(TripService.prototype as any, 'findLastTripWithinRangeFromEntity');
  
        findLastTripWithinRangeFromEntity
          .mockReturnValue(plainToClass(TripEntity, {
            id: '2',
            startTime: '2019-05-07T10:55:00.000Z',
            endTime: '2019-05-07T11:06:00.000Z',
          }))
          .mockReturnValueOnce(undefined);
  
        await expect(service.createAndMapTrips(driver, entities)).resolves.toEqual(result);
        expect(tripRepositoryMock.getAndCountTrips).toHaveBeenCalledTimes(1);
        expect(tripRepositoryMock.getAndCountTrips).toHaveBeenCalledWith(driver, {
          driverId: driver.driverId,
          fromEndTime: moment.utc(entities[0].timestamp).subtract(10, 'minutes').toISOString(),
          toEndTime: moment.utc(entities[entities.length - 1].timestamp).add(10, 'minutes').toISOString(),
        });
        expect(findLastTripWithinRangeFromEntity).toHaveBeenCalledTimes(2);
        expect(findLastTripWithinRangeFromEntity).toHaveBeenNthCalledWith(1, [], entities[0]);
        expect(findLastTripWithinRangeFromEntity).toHaveBeenNthCalledWith(2, tripsDb, entities[0]);
        expect(findLastTripWithinRangeFromEntity).toHaveBeenCalledWith([], entities[0]);
  
        expect(tripRepositoryMock.create).toHaveBeenCalledTimes(0);
      });
  });
  </code></pre>
  
  <p>I am getting the following error:</p>
  
  <pre><code> ● TripService › TripService.createAndMapTrips() › should map existing trips if the trips already exist within the time range requested
  
      expect(jest.fn()).toHaveBeenNthCalledWith(expected)
  
      Expected mock function first call to have been called with:
        []
      as argument 1, but it was called with
        [{"endTime": "2019-05-07T11:10:00.000Z", "id": "2", "startTime": "2019-05-07T10:55:00.000Z"}].
  
      Difference:
  
      - Expected
      + Received
  
      - Array []
      + Array [
      +   TripEntity {
      +     "endTime": "2019-05-07T11:10:00.000Z",
      +     "id": "2",
      +     "startTime": "2019-05-07T10:55:00.000Z",
      +   },
      + ]
  
        415 |       });
        416 |       expect(findLastTripWithinRangeFromEntity).toHaveBeenCalledTimes(2);
      &gt; 417 |       expect(findLastTripWithinRangeFromEntity).toHaveBeenNthCalledWith(1, [], entities[0]);
            |                                                 ^
        418 |       expect(findLastTripWithinRangeFromEntity).toHaveBeenNthCalledWith(2, tripsDb, entities[0]);
        419 |       expect(findLastTripWithinRangeFromEntity).toHaveBeenCalledWith([], entities[0]);
        420 |
  
        at Object.&lt;anonymous&gt; (trip/trip.service.spec.ts:417:49)
        at fulfilled (trip/trip.service.spec.ts:4:58)
  </code></pre>
  
  <p>I am clearly setting the mock returned value and according to the logic the first call to <code>findLastTripWithinRangeFromEntity</code> should be made with <code>[]</code> and second call with the <code>tripsDb</code> but it gets called with different value.</p>
  
  <p>I can't seem to find the cause.</p>
  
  <p>Any help will be appreciated.</p>