jest icon indicating copy to clipboard operation
jest copied to clipboard

[Feature]: add a waitBeforeRetry option to jest.retryTimes

Open saltman424 opened this issue 2 years ago • 3 comments

🚀 Feature Proposal

It would be nice to be able to tell Jest to wait some amount of time before retrying tests. I think for most use cases, just a static number of milliseconds would be sufficient.

Motivation

The use case that I am running into is: I have a test that modifies some data, and then a subsequent tests that query the data and then ensure that data is the updated data. However, since there is some caching for the queries (on the back-end), I sometimes need to wait a little bit to get the updated data. So the sequence I would want is:

  1. Test 1: update data
  2. Test 2: get updated data - fails because cached data is returned
  3. Wait some amount of time (e.g. 10 seconds)
  4. Test 2 retry: get updated data - succeeds because cached data is no longer returned

Another case I could see benefitting from this is network errors or other temporary issues, but I think most of the time, that type of case can and should be handled by putting retry logic in the client that is making the network request.

Example

describe('Updating item', () => {
  const originalItem: Item = { ... }
  const updatedItem: Item = { ... }

  test('should succeed', async () => {
    await updateItem(originalItem.id, updatedItem)
  })

  describe('subsequent queries', () => {
    jest.retryTimes(3, { waitBeforeRetry: 10e3 })
  
    test('getting item should show updates', async () => {
      const item = await getItem(originalItem.id) // Might return cached results
      expect(item).toEqual(updatedItem)
    })

    test('item list should show updates', async () => {
      const items = await listItems() // Might return cached results
      expect(items).toContainEqual(updatedItem)
    })
  })
})

Pitch

The reason I want this in jest.retryTimes is because cached data doesn't cause the request itself to fail, it just causes one of the following expect assertions to fail. So the necessary retry logic must be put in the test suite.

To be honest, I am not sure there are many cases where I would use jest.retryTimes without a waitBeforeRetry option since most of the time if there is an error that an immediate retry would solve, I would prefer to implement that retry logic in the underlying client or service. To me, jest.retryTimes should mostly be used when there is not an error in the request but rather in the expect assertions (generally due to caching or eventual consistency).

saltman424 avatar Dec 05 '22 15:12 saltman424

Hi guys this is my first time here, Happy to contribute to the jest community. I've created a sample PR as per my understanding of the repository. I'm not sure where to add tests to support this new feature, would be happy to do so if someone could point me in the right direction.

Tarun047 avatar Dec 15 '22 12:12 Tarun047

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Jan 14 '23 12:01 github-actions[bot]

Keep the issue open

saltman424 avatar Jan 15 '23 02:01 saltman424

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Feb 14 '23 02:02 github-actions[bot]

Keep the issue open

saltman424 avatar Feb 26 '23 03:02 saltman424

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Mar 28 '23 03:03 github-actions[bot]

Keep the issue open

saltman424 avatar Mar 28 '23 16:03 saltman424

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Apr 27 '23 17:04 github-actions[bot]

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

github-actions[bot] avatar May 27 '23 18:05 github-actions[bot]

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

github-actions[bot] avatar Jun 27 '23 00:06 github-actions[bot]