Skip to content

assert.match should return match result #33869

@awwright

Description

@awwright

Is your feature request related to a problem? Please describe.
I was using assert(haystack.match(/needle/)); in my unit tests, and I found that assert.match(haystack, /needle/); produced much cleaner errors. +1, suggest to move to stable.

However, in a small handful of cases, I was also using the match results later on in the test, like so:

const m = haystack.match(/nee(\d+)le/);
assert(m);
// ...
assert(haystack2.match(m[1]));

Describe the solution you'd like

If I could rescue the match results from assert.match, and if the regexp argument could be a string, this code could be simplified:

const m = assert.match(haystack, /nee(\d+)le/);
// ...
assert.match(haystack2, m[1]);

Describe alternatives you've considered
This feature simply leads to cleaner tests.

Possible downsides are that people may overlook assert calls used like this, because they always expect "assert" to work like a C assertion, or having a return value is too inconsistent with the rest of the library.

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.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions