Skip to content

Commit

Permalink
Avoid late start message after test has finished in --runInBand mode
Browse files Browse the repository at this point in the history
The start message that is sent over a test's MessageChannel may arrive after
the tests' Promise has already resolved, i.e. the finished callback has
already been executed. This results in excessive status message logging,
as the late start event does activate the test as "running" even though
it has already finished, rendering it in the "running" state forever (and
consequently generating excessive status messages as more and more tests become
considered "running").

Fixes #76
  • Loading branch information
JoostK committed Nov 26, 2023
1 parent a7966dc commit 78ed50e
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,16 @@ export default class LightRunner {

return Promise.all(
tests.map(test => {
// When `--runInBand` is enabled, `InBandPiscina` executes tasks in a microtask which may complete prior to
// having received the start message on the `MessageChannel`. As such, the start callback should not be invoked
// after the completion callbacks have already been invoked.
let finished = false;
const mc = new MessageChannel();
mc.port2.onmessage = () => onStart(test);
mc.port2.onmessage = () => {
if (!finished) {
onStart(test);
}
};
mc.port2.unref();

return this._piscina
Expand All @@ -58,8 +66,14 @@ export default class LightRunner {
{ transferList: [mc.port1] }
)
.then(
result => void onResult(test, result),
error => void onFailure(test, error)
result => {
finished = true;
onResult(test, result);
},
error => {
finished = true;
onFailure(test, error);
}
);
})
);
Expand Down

0 comments on commit 78ed50e

Please sign in to comment.