Skip to content

Commit 5a06bcf

Browse files
committed
assertEventually: Add dynamic message function generation
This way, a caller can construct the message function on failure, e.g. to provide information about the current status.
1 parent 8ad1a3c commit 5a06bcf

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/assert_utils.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,9 @@ function assertNotIncludes(haystack, needle, message = undefined) {
144144
* await assertEventually(() => called);
145145
* ```
146146
* @param {() => any} testfunc The test function. Must return `true` to signal success.
147-
* @param {{message?: string, timeout?: number, checkEvery?: number, crashOnError?: boolean}} [__namedParameters] Options (currently not visible in output due to typedoc bug)
147+
* @param {{message?: string, messageFunc?: () => string, timeout?: number, checkEvery?: number, crashOnError?: boolean}} [__namedParameters] Options (currently not visible in output due to typedoc bug)
148148
* @param {string?} message Error message shown if the condition never becomes true within the timeout.
149+
* @param {(() => string) ?} messageFunc A callable to generate the error message. If set, the message parameter is ignored.
149150
* @param {number?} timeout How long to wait, in milliseconds.
150151
* @param {number?} checkEvery Intervals between checks, in milliseconds.
151152
* @param {boolean?} crashOnError `true` (default): A thrown error/exception is an immediate failure.
@@ -155,6 +156,7 @@ async function assertEventually(
155156
testfunc,
156157
{
157158
message = 'assertEventually failed',
159+
messageFunc = null,
158160
timeout = 10000,
159161
checkEvery = 200,
160162
crashOnError = true,
@@ -195,6 +197,7 @@ async function assertEventually(
195197
throw caughtError;
196198
}
197199

200+
message = messageFunc ? messageFunc() : message;
198201
throw new Error(`${message} (waited ${output.formatTime(timeout)})`);
199202
}
200203

tests/selftest_assertEventually.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ async function run() {
5050
);
5151
assert.strictEqual(counter, 3);
5252

53+
// Custom message func
54+
await assert.rejects(
55+
assertEventually(() => false, {
56+
checkEvery: 1,
57+
timeout: 1,
58+
messageFunc: () => 'func',
59+
}),
60+
{
61+
message: /func \(waited/,
62+
}
63+
);
64+
5365
// Throws original error
5466
try {
5567
await assertEventually(

0 commit comments

Comments
 (0)