Skip to content

doc(proposal): expectFailure label and/or matcher - #10

Merged
JakobJingleheimer merged 9 commits into
nodejs:mainfrom
Han5991:proposals/expect-failure-reason
Feb 9, 2026
Merged

doc(proposal): expectFailure label and/or matcher#10
JakobJingleheimer merged 9 commits into
nodejs:mainfrom
Han5991:proposals/expect-failure-reason

Conversation

@Han5991

Copy link
Copy Markdown
Contributor

This PR adds a proposal for enhancing expectFailure to support both custom messages (for reasoning) and validation (matching errors via regex/objects), addressing the needs discussed in nodejs/node#61563.

@vassudanagunta

vassudanagunta commented Jan 29, 2026

Copy link
Copy Markdown

@Han5991 this should reference nodejs/node#61570 which goes beyond nodejs/node#61563. It may end up being the case that a fix for the latter lands before the former, since the latter is relatively trivial and simply brings expectFailure in line with todo/skip.

--EDIT--

I'm a little confused. Your first proposal, #9, included a way to supply both a reason and an expected error as I proposed in nodejs/node#61570, but this one does not.

@Han5991

Copy link
Copy Markdown
ContributorAuthor

@vassudanagunta

Sorry for the confusion. PR #9 was accidentally opened from my other account, so I closed it and opened this one instead.
I have updated this proposal to include the Object support (both reason and validation) as you suggested in #61570. Thanks for pointing that out!

@vassudanagunta

Copy link
Copy Markdown

Alternative syntaxes to consider for supplying both reason and expected error:

test('fails with reason and specific error',{expectFailure: 'Bug #123: Edge case behavior',expectFailureMessage: /errmsgpattern/},()=> ...);

or for consistency with assert.throws:

test('fails with reason and specific error',{expectFailure: 'Bug #123: Edge case behavior',expectFailureError: <RegExp> | <Function> | <Object> | <Error>},()=> ...);

The user can supply either one of the two new options or both.

@vassudanagunta

Copy link
Copy Markdown

Consistency with assert.throws could be trivial to implement either by sharing code or by calling assert.throws in a try-catch to invert the effect.

@Han5991

Copy link
Copy Markdown
ContributorAuthor

Thanks for the feedback!

Implementation:
Great suggestion! I plan to update the implementation to use assert.throws internally for validation. This will allow us to leverage all the existing validation capabilities (RegExp, Object, Class, custom function) without reinventing the wheel and keep the code cleaner.
API Syntax:
Regarding the flat options (expectFailureError), I lean towards keeping the nested object structure { expectFailure: { with: ..., message: ... } }. It keeps all failure expectations grouped under a single property and seems more extensible if we need to add more configuration in the future, without polluting the top-level options namespace.

@vassudanagunta

Copy link
Copy Markdown

the terseness and legibility of with+message is very nice. as i said on your PR, it gets my top vote.

Update the `expectFailure` enhancements proposal based on feedback and implementation alignment:
- Consolidate validation logic under the `with` property within an object.
- Remove direct RegExp support in favor of the object syntax for consistency.
- Specify usage of `assert.throws` for robust error validation.
- Document alternatives considered (flat options).
@JakobJingleheimer

Copy link
Copy Markdown
Member

IIR, we had originally envisioned expectFailure would eventually accept a string for reason, and an object or regex for a matcher:

it('should do the thing',{expectFailure: 'chromium#1234'},()=>{});it('should do another thing',{expectFailure: /sometelltaleneedle/},()=>{});it('should do something else',{expectFailure: newRangeError()},()=>{});
should do the thing # EXPECT FAILURE "chromium#1234"
should do another thing # EXPECT FAILURE matching /some telltale needle/
should do something else # EXPECT FAILURE matching RangeError(…)

Where matcher would follow the behaviour of assert.throws (and would probably leverage it under the hood).

@Han5991
Han5991force-pushed the proposals/expect-failure-reason branch from 0de23a6 to abb5912CompareJanuary 29, 2026 21:55
Comment threadproposals/expect-failure-enhancements.md Outdated
@Han5991
Han5991force-pushed the proposals/expect-failure-reason branch from 6e1e4a8 to 87802e3CompareJanuary 30, 2026 02:21
@Han5991

Copy link
Copy Markdown
ContributorAuthor

IIR, we had originally envisioned expectFailure would eventually accept a string for reason, and an object or regex for a matcher:

it('should do the thing',{expectFailure: 'chromium#1234'},()=>{});it('should do another thing',{expectFailure: /sometelltaleneedle/},()=>{});it('should do something else',{expectFailure: newRangeError()},()=>{});
should do the thing # EXPECT FAILURE "chromium#1234"
should do another thing # EXPECT FAILURE matching /some telltale needle/
should do something else # EXPECT FAILURE matching RangeError(…)

Where matcher would follow the behaviour of assert.throws (and would probably leverage it under the hood).

@JakobJingleheimer

I've updated the proposal to reflect the latest feedback. Ready for re-review!

Comment threadproposals/expect-failure-enhancements.md
Comment threadproposals/expect-failure-enhancements.md Outdated
@JakobJingleheimerJakobJingleheimer changed the title feat: add expectFailure enhancements proposaldoc(proposal): add expectFailure enhancementsJan 30, 2026
Explicitly state that passing an empty object ({}) to expectFailure should throw ERR_INVALID_ARG_VALUE to prevent ambiguity and potential user error.
@vassudanagunta

Copy link
Copy Markdown

RE the back and forth on whether expectFailure should be truthy, I think it should be an open question to discuss, either with input from other voices here in comments or in the test runner team meeting. Here is my opinion copied from the PR:

I agree with @ljharb in general, but in this specific case it will result in assertFailurebehaving differently from todo and skip (see the resolved comment). I think that will be odd/confusing.

Also, given the specificity of the linked todo/skip logic, that behavior seems rather intentional. 🤷🏾‍♂️

@JakobJingleheimerJakobJingleheimer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this!

I think some whitespace is amiss (e.g. too many spaces between * and the text, too much indentation, missing blank line between heading and its content, etc).

Comment threadproposals/expect-failure-enhancements.md Outdated
Comment threadproposals/expect-failure-enhancements.md
Comment threadproposals/expect-failure-enhancements.md Outdated
Comment threadproposals/expect-failure-enhancements.md Outdated
Comment threadproposals/expect-failure-enhancements.md Outdated
Comment threadproposals/expect-failure-enhancements.md Outdated
Comment threadproposals/expect-failure-enhancements.md Outdated
Comment threadproposals/expect-failure-enhancements.md
Comment threadproposals/expect-failure-enhancements.md Outdated
Comment threadproposals/expect-failure-enhancements.md
- Update property names to match/label in description to match code examples.
- Fix markdown formatting (whitespace, blank lines) as requested.
@Han5991

Copy link
Copy Markdown
ContributorAuthor

Thanks for the detailed review! I've updated the proposal to address your feedback

@JakobJingleheimerJakobJingleheimer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙌 LGTM aside from the small revert needed because of my mistaken assumption.

Comment threadproposals/expect-failure-enhancements.md Outdated
Comment threadproposals/expect-failure-enhancements.md Outdated
@JakobJingleheimer
JakobJingleheimer requested review from a team, ljharb and pmarchiniFebruary 2, 2026 23:00

@ljharbljharb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM overall

Comment threadproposals/expect-failure-enhancements.md Outdated
Comment threadproposals/expect-failure-enhancements.md
- Revert activation logic to allow falsy values (e.g., empty string, 0) to enable the feature, maintaining consistency with skip/todo.
- Add requirement: configuration object must contain at least one of 'label' or 'match'.
@Han5991

Copy link
Copy Markdown
ContributorAuthor

Thanks for the feedback. Changes have been applied based on your comments.

@vassudanaguntavassudanagunta left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to be consistent with assert.throws?

```
- **Properties**:
- `label` (String): The failure reason/label (displayed in reporter).
- `match` (RegExp | Object | Function | Class): Validation logic. This is passed directly to `assert.throws` validation argument, supporting all its capabilities.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JakobJingleheimer et. al., when writing my suggested changes to the PR, I noticed assert.throws names this field error, not match. Might it be better to be consistent?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, sure, yes :) error was one of the ideas I suggested (but without knowing it was what assert.throws uses).

@pmarchini

Copy link
Copy Markdown
Member

LGTM!!!

@JakobJingleheimer
JakobJingleheimer requested a review from a teamFebruary 8, 2026 14:09
@JakobJingleheimerJakobJingleheimer changed the title doc(proposal): add expectFailure enhancementsdoc(proposal): expectFailure label and/or matcherFeb 9, 2026
@JakobJingleheimer

Copy link
Copy Markdown
Member

Discussed in #14 and approved.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

@Han5991@vassudanagunta@JakobJingleheimer@pmarchini@ljharb