Skip to content

Commit

Permalink
feat: wait for multiple assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlafroscia committed Apr 3, 2020
1 parent e861edb commit a9d2963
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
14 changes: 10 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,21 @@ export function installWaitFor(QUnit: QUnit) {
{ timeout = 1000 }: Options = { timeout: undefined }
) {
const originalPushResult = this.pushResult;
let lastResult: Result;
let lastResults: Result[];

this.pushResult = (result: Result) => {
lastResult = result;
lastResults.push(result);
};

try {
await waitUntil(() => {
// Reset to capture the most recent round of results
lastResults = [];

assertionCallback();

return lastResult.result;
// `waitUntil` only "passes" if _all_ assertions were successful
return lastResults.every(({ result }) => result);
}, timeout);
} catch (e) {
if (!(e instanceof TimeoutError)) {
Expand All @@ -36,7 +40,9 @@ export function installWaitFor(QUnit: QUnit) {
this.pushResult = originalPushResult;
}

this.pushResult(lastResult);
for (const result of lastResults) {
this.pushResult(result);
}
},
});
}
Expand Down
14 changes: 12 additions & 2 deletions tests/wait-for-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,21 @@ installVerify(QUnit, td);
installWaitFor(QUnit);

test("it can wait for a condition to be successful", async function (assert) {
const stub = td.when(td.function()()).thenReturn(1, 2);
const return2After2 = td.when(td.function()()).thenReturn(1, 2);
const return2After3 = td.when(td.function()()).thenReturn(1, 1, 2);
const callCounter = td.function();

await assert.waitFor(() => {
assert.equal(stub(), 2);
assert.equal(return2After2(), 2);
assert.equal(return2After3(), 2);
callCounter();
});

assert.verify(
callCounter(),
{ times: 3 },
"Took 3 calls to get everything to pass"
);
});

test("it throws a non-timeout error", async function (assert) {
Expand Down

0 comments on commit a9d2963

Please sign in to comment.