Skip to content

The assertion module should signals failures through messages to the test runner #52033

Closed
@bunglegrind

Description

@bunglegrind

What is the problem this feature will solve?

The assertion module signals failures via exceptions. This may causes some subtle issues during program testing, since the "exception channel" is also used by the program under test which may catch the exceptions generated by failing assertions resulting in weird test behavior.

import {test} from "node:test";
import assert from "node:assert";

function function_under_test(callback, value) {
    let r = 0;
    try {
        r = callback(value);
    } catch (error) {
       //log error...
    }
    //continue executing operations...
}

test("weird test supposed to fail, but it doesn't", function () {
    function_under_test(function (v) {
        assert.fail("BOOM!");
    }, 4);
});

test passes...

What is the feature you are proposing to solve the problem?

When in a test runner context provide the assertion module via the testContext object:

import {test} from "node:test";
import assert from "node:assert";

function function_under_test(callback, value) {
    let r = 0;
    try {
        r = callback(value);
    } catch (error) {
       //log error...
    }
    //continue executing operations...
}

test("weird test", function (testContext) {
    function_under_test(function (v) {
        testContext.assert.fail("BOOM!");
    }, 4);
});

and signal the failures via messages between testContext and assert methods.

What alternatives have you considered?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    assertIssues and PRs related to the assert subsystem.feature requestIssues that request new features to be added to Node.js.test_runnerIssues and PRs related to the test runner subsystem.

    Type

    No type

    Projects

    Status

    Awaiting Triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions