jest test intermittently failing with no assertion calls

36 Views Asked by At

I have a strange behaviour with jest (version 27.0). Very randomly one test out of 400 tests fails ( 1 out of 100 times) but when I just rerun it doesn't fail.

The error I get from the test is this:

 Expected four assertions to be called but received zero assertion calls.

This is how the test looks like:

it('sometest', async () => {
  expect.assertions(4);
  jest.spyOn(sfnMock, 'startExecution').mockResolvedValue({
    executionArn: config.arn,
    startDate: new Date(),
  } as StartExecutionCommandOutput);

  const stringifyMock = jest.spyOn(JSON, 'stringify');


  const result = await sut.doSomething();


  expect(stringifyMock).toHaveBeenCalledTimes(1);
  expect(stringifyMock.mock.calls[0][0]).toEqual(expect.objectContaining(expectedPayload));

  expect(sfnMock.startExecution).toHaveBeenCalledWith(config.arn, expect.any(String), expect.any(String));

  expect(result).toMatchObject({
    // some object
  });
});

It is very hard to reproduce since I have to run the complete tests multiple times but this happened already 3 times and it is always the same test.

Maybe there is an issue regarding async tests and expect? I have no clue how to solve this.

0

There are 0 best solutions below