Skip to content

Async await usages not showing expectation library error #32

@sekhavati

Description

@sekhavati

Early on in the README it mentions:

Nice thing about this simple tool is that if the expectation keeps failing till the timeout, it will check it one last time, but this time the same way your test runner would run it - so you basically get your expectation library error

For example:

 FAIL  src/waitForExpect.spec.js (5.042s)
  ✕ it waits for the number to change (4511ms)

  ● it waits for the number to change

    expect(received).toEqual(expected)
    
    Expected value to equal:
      105
    Received:
      100

       9 |   }, 600);
      10 |   await waitForExpect(() => {
    > 11 |     expect(numberToChange).toEqual(105);
      12 |   });
      13 | });
      14 | 
      
      at waitForExpect (src/waitForExpect.spec.js:11:28)
      at waitUntil.catch (src/index.js:61:5)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        5.807s

But when I have an async/await scenario I no longer get this nice print out highlighting the line where the expectation didn't hold true, instead I get a Jest timeout error. Tweaking your own async example from the docs I get this:

const waitForExpect = require("wait-for-expect");

test("it works with promises", async () => {
  let numberToChange = 10;
  const randomTimeout = Math.floor(Math.random() * 300);

  setTimeout(() => {
    numberToChange = 100;
  }, randomTimeout);

  const sleep = (ms) =>
    new Promise((resolve) => setTimeout(() => resolve(), ms));

  await waitForExpect(async () => {
    await sleep(10);
    expect(numberToChange).toEqual(200); // <--  deliberately cause it to fail
  });
});

Result:

Exceeded timeout of 5000ms for a test.
Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test.

Am I doing something wrong, is this expected behaviour or is it a bug? (I was testing with version 3.0.2)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions