saghul/txiki.js

test: worker, no failure detected on test subcommand

EmixamPP opened this issue · 6 comments

Let's replace the content of test-worker.js with

import assert from 'tjs:assert';
import path from 'tjs:path';


const data = JSON.stringify({foo: 42, bar: 'baz!'});
const w = new Worker(path.join(import.meta.dirname, 'helpers', 'worker.js'));
const timer = setTimeout(() => {
    w.terminate();
    assert.fail('Timeout out waiting for worker');
}, 1000);
w.onmessage = event => {
    clearTimeout(timer);
    w.terminate();
    const recvData = JSON.stringify(event.data);
    assert.eq(data, "should fail", 'Message received matches');
};

If you look at the last assert line, the test shouldn't pass, although it is.
After some experiments, it seems that the asserts made inside a callback, like setTimeout() or onemessage(event), do not have an impact on the test status.

Nevertheless, if you execute the test file through the run command, it will correctly show the failure if needed.

Moreover, it does not only concern assert, but more generally any errors that occur at eval time in the worker, it will not trigger a failure of the test, but this may be another challenge not related to the first observation.

Does it show the test failure, or does it dump it to stderr but still exit with 0?

Nothing about that is printed in stderr or stdout and the exit code is 0.

Sorry, I mean when you run the code directly.

$ build/tjs run tests/test-worker.js                                                                                                                                                                                                             
AssertionError: Message received matches
at: f (:0:0)
wanted: should fail
found: {"foo":42,"bar":"baz!"}
operator: equal

But correct, exit code still 0

Gotcha. IIRC the same happens with timers. I need to fix that by making the interpreter exit with an error in that case...

IIRC the same happens with timers.

Correct, I observed the same issue