nodejs / nodejs/node

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

Open
#52,033 12 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

assert feature request test_runner
Dominant language
JavaScript
Stars
122k
Forks
37.3k
Avg merge
4d 2h
Merged PRs (30d)
283

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the node:test testContext API and the node:assert module, then inspect how assertion failures reach the test runner. Define the test-context assertion interface and verify that failures cannot be caught by the code under test while the runner still reports them.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
testing
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.