-
-
Notifications
You must be signed in to change notification settings - Fork 4
doc(proposal): expectFailure label and/or matcher
#10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
JakobJingleheimer
merged 9 commits into
nodejs:main
from
Han5991:proposals/expect-failure-reason
Feb 9, 2026
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
036ee9c
feat: add expectFailure enhancements proposal
Han5991 049c2d3
docs: add object support for combined reason and validation (fixes #6…
Han5991 abb5912
docs: update expectFailure proposal with validation details
Han5991 87802e3
docs: update expectFailure proposal with direct matchers and rationale
Han5991 cb12492
docs: update expectFailure empty string behavior for consistency
Han5991 550464a
docs: expand equivalence examples and clarify activation logic
Han5991 38976f0
doc: disallow empty object in expectFailure option
Han5991 4e2c487
doc: apply review feedback to expectFailure proposal
Han5991 a36485a
doc: update expectFailure proposal based on review
Han5991 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| # Feature proposal: `expectFailure` enhancements | ||
|
|
||
| ## Summary | ||
|
Han5991 marked this conversation as resolved.
|
||
| Update the `expectFailure` option in `test()` to accept different types of values, enabling both **custom failure messages** and **error validation**. This aligns with `skip` and `todo` options while adding capabilities similar to `assert.throws`. | ||
|
|
||
| ## API & Behavior | ||
|
|
||
| The behavior of `expectFailure` is determined by the type of value provided: | ||
|
|
||
| ### 1. String: Failure Reason | ||
|
Han5991 marked this conversation as resolved.
Outdated
|
||
| When a **non-empty string** is provided, it acts as a documentation message (reason), identical to `skip` and `todo` options. | ||
|
Han5991 marked this conversation as resolved.
Outdated
|
||
|
|
||
| ```js | ||
| test('fails with a specific reason', { | ||
| expectFailure: 'Bug #123: Feature not implemented yet' | ||
| }, () => { | ||
| throw new Error('boom'); | ||
| }); | ||
| ``` | ||
| - **Behavior**: The test is expected to fail. The string is treated as a label/reason. | ||
| - **Validation**: None. It accepts *any* error. | ||
|
Han5991 marked this conversation as resolved.
Outdated
|
||
| - **Output**: The reporter displays the string (e.g., `# EXPECTED FAILURE Bug #123...`). | ||
|
Han5991 marked this conversation as resolved.
Outdated
|
||
| - **Rationale**: Maintains consistency with existing `test` options where a string implies a reason/description. | ||
|
|
||
| ### 2. RegExp: Error Matcher | ||
| When a **RegExp** is provided, it acts as a validator for the thrown error. | ||
|
|
||
| ```js | ||
| test('fails with matching error', { | ||
| expectFailure: /expected error message/ | ||
| }, () => { | ||
| throw new Error('this is the expected error message'); | ||
| }); | ||
| ``` | ||
|
Han5991 marked this conversation as resolved.
|
||
| - **Behavior**: The test passes **only if** the thrown error matches the regular expression. | ||
| - **Validation**: Strict matching against the error message. | ||
| - **Output**: Standard expected failure output. | ||
|
|
||
| ## Ambiguity Resolution | ||
| Potential ambiguity between "String as Reason" and "String as Matcher" is resolved by strict type separation: | ||
| * `typeof value === 'string'` → **Reason** (Documentation only) | ||
| * `value instanceof RegExp` → **Matcher** (Validation) | ||
|
|
||
| Users needing to match a specific string error message should use a RegExp (e.g., `/Error message/`) to avoid confusion. | ||
|
|
||
| ## Edge Cases & Implementation Details | ||
|
|
||
| ### Empty String (`expectFailure: ''`) | ||
| Following standard JavaScript truthiness rules, an empty string should be treated as **falsy**. | ||
|
Han5991 marked this conversation as resolved.
Outdated
|
||
|
|
||
| * `expectFailure: ''` behaves exactly like `expectFailure: false`. | ||
| * The feature is **disabled**, and the test is expected to pass normally. | ||
|
|
||
| ### Type Safety for `this.passed` | ||
| The implementation must ensure that `this.passed` remains a strict `boolean`. | ||
| Assigning a string directly (e.g., `this.passed = this.expectFailure`) is unsafe as it introduces type pollution (assigning `""` or `"reason"` instead of `false`/`true`). | ||
|
|
||
| **Recommended Implementation Logic:** | ||
| ```javascript | ||
| // When an error is caught: | ||
| this.passed = !!this.expectFailure; // Forces conversion to boolean | ||
| ``` | ||
| * If `expectFailure` is `"reason"` → `true` (Test Passes) | ||
| * If `expectFailure` is `""` → `false` (Test Fails, as expected failure was not active) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.