Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

waitForExpect throwing error instead of waiting #29

Open
robross0606 opened this issue Jul 1, 2021 · 6 comments
Open

waitForExpect throwing error instead of waiting #29

robross0606 opened this issue Jul 1, 2021 · 6 comments

Comments

@robross0606
Copy link

robross0606 commented Jul 1, 2021

For reasons I don't understand, attempts to use waitForExpect() are returning a Jest error instead of waiting (and possibly timing out).

    expected [] to be an array containing exactly 1 items

      429 |
      430 |           await waitForExpect(() => {
    > 431 |             expect(receivedMessages).toBeArrayOfSize(1)
          |                                      ^
      432 |           })
      433 |

      at src/__tests__/amqp-service.test.js:431:38
      at Timeout.runExpectation [as _onTimeout] (node_modules/wait-for-expect/lib/index.js:46:25)
@robross0606
Copy link
Author

Or is the problem that it is timing out? If so, it is doing so very quickly.

@robross0606
Copy link
Author

I should note that this didn't start happening until I had two different waitForExpect() calls at two different spots in the same unit test. Could the timer not be resetting between those calls?

@lgandecki
Copy link
Member

Hey there, sorry for the troubles - any chance you could push a tiny reproduction to github?

@robross0606
Copy link
Author

yes, I'm trying to reproduce in a smaller test right now

@robross0606
Copy link
Author

I was able to reproduce this very easily with the following code:

const waitForExpect = require('wait-for-expect')

function sleep(ms) {
  return new Promise(resolve => {
    setTimeout(resolve, ms)
  })
}

const received = []
async function doSomethingLong() {
  await sleep(5000)
  received.push(received.length + 1)
}

test('Testing wait-for-expect', async () => {
  doSomethingLong()

  await waitForExpect(() => {
    expect(received.length).toBe(1)
  })

  doSomethingLong()

  await waitForExpect(() => {
    expect(received.length).toBe(5)
  })
})

Produces:

  ● Testing wait-for-expect

    expect(received).toBe(expected) // Object.is equality

    Expected: 5
    Received: 2

      23 |
      24 |   await waitForExpect(() => {
    > 25 |     expect(received.length).toBe(5)
         |                             ^
      26 |   })
      27 | })
      28 |

      at src/__tests__/waitForExpect.test.js:25:29
      at Timeout.runExpectation [as _onTimeout] (node_modules/wait-for-expect/lib/index.js:46:25)

I'm not sure if this is expected or not since the stack trace location of the failure is different than what is shown on your README.md:

      at waitForExpect (src/waitForExpect.spec.js:13:34)
      at waitUntil.catch (src/index.js:61:5)

@robross0606
Copy link
Author

I think it just may be that the README documentation is slightly outdated and this is a normal indication that waitForExpect() waited and never found the expected result.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants