-
-
Notifications
You must be signed in to change notification settings - Fork 5
doc(proposal): add expectFailure enhancements #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
Open
Han5991
wants to merge
9
commits into
nodejs:main
Choose a base branch
from
Han5991:proposals/expect-failure-reason
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+125
−0
Open
Changes from all commits
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,125 @@ | ||
| # Feature proposal: `expectFailure` label & matcher | ||
|
|
||
| ## Summary | ||
|
|
||
| Update the `expectFailure` option in `test()` to accept different types of values, enabling both **custom label** and **error matching**. This proposal integrates the requirements from [nodejs/node#61570](https://github.com/nodejs/node/issues/61570), ensuring consistency with `skip`/`todo` while adding robust validation capabilities. | ||
|
|
||
| ## API & Behavior | ||
|
|
||
| The behavior of `expectFailure` is strictly determined by the type of value provided: | ||
|
|
||
| ### String: Failure label | ||
|
|
||
| When a **non-empty** `string` is provided, it acts as an output label (reason) for that test-case (identical to `skip` and `todo` options). | ||
|
|
||
| ```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. | ||
| - **Output**: The reporter displays the string literally, prefixed by a TAP comment character (e.g., `# EXPECTED FAILURE Bug #123…`). | ||
|
|
||
| ### Matcher: Error constructor, Error-like object, RegExp, or validation function | ||
|
|
||
| When an `Error` constructor, `Error`-like object, `RegExp`, or validation function is provided, it is treated as validation to match against, mirroring the behaviour of [`assert.throws`](https://nodejs.org/docs/latest-v25.x/api/assert.html#assertthrowsfn-error-message) (possibly leveraging it under the hood). | ||
|
|
||
| ```js | ||
| test('fails with matching error (RegExp)', { | ||
| expectFailure: /expected error message/ | ||
| }, () => { | ||
| throw new Error('this is the expected error message'); | ||
| }); | ||
|
|
||
| test('fails with matching error (Class)', { | ||
| expectFailure: RangeError | ||
| }, () => { | ||
| throw new RangeError('Index out of bounds'); | ||
| }); | ||
| ``` | ||
|
|
||
| ### Configuration Object: Reason & Validation | ||
|
|
||
| When a **Plain Object** with specific properties (`match`, `label`) is provided, it allows specifying both a failure reason and validation logic simultaneously. | ||
|
|
||
| ```js | ||
| test('fails with reason and specific error', { | ||
| expectFailure: { | ||
| label: 'Bug #123: Edge case behavior', // Reason | ||
| match: /Index out of bounds/ // Validation | ||
| } | ||
| }, () => { | ||
| throw new RangeError('Index out of bounds'); | ||
| }); | ||
| ``` | ||
| - **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. | ||
| - **Requirement**: The object must contain at least one of `label` or `match`. | ||
| - **Behavior**: The test passes **only if** the error matches the `match` criteria. | ||
| - **Output**: The reporter displays the `label`. | ||
|
|
||
| ### Equivalence | ||
|
|
||
| The following configurations are equivalent in behavior: | ||
|
|
||
| **1. Reason only:** | ||
| ```js | ||
| expectFailure: 'reason'; | ||
| ``` | ||
|
|
||
| **2. Validation only:** | ||
| ```js | ||
| expectFailure: /error/ | ||
| expectFailure: { match: /error/ } | ||
| ``` | ||
|
|
||
| **3. Catch-all (Any Error):** | ||
| ```js | ||
| expectFailure: true | ||
| ``` | ||
|
|
||
| ## Ambiguity Resolution | ||
| Potential ambiguity between a **Matcher Object** and a **Configuration Object** is resolved as follows: | ||
|
|
||
| 1. **String** → Reason. | ||
Han5991 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 2. **RegExp** or **Function** → Matcher (Validation). | ||
| 3. **Object**: | ||
| * **Empty Object** (`{}`) → **Error**: throws `ERR_INVALID_ARG_VALUE`. | ||
| ```js | ||
| // Uses Node.js standard error code | ||
| throw new ERR_INVALID_ARG_VALUE( | ||
| 'expectFailure', | ||
| expectFailure, | ||
| 'must not be an empty object' | ||
| ); | ||
| ``` | ||
| * If the object contains **only** `match` and/or `label` properties → **Configuration Object**. | ||
| * Otherwise → **Matcher Object** (passed to `assert.throws` for property matching). | ||
|
|
||
| ## Activation & Truthiness | ||
|
|
||
| To maintain strict consistency with `todo` and `skip` options: | ||
| * The feature is **disabled** only if `expectFailure` is `undefined` or `false`. | ||
| * **All other values** enable the feature (treat as truthy). | ||
| * `expectFailure: ''` (Empty String) → **Enabled** (treats as generic failure expectation). | ||
| * `expectFailure: 0` → **Enabled** (treated as a Matcher Object unless specific logic excludes numbers, but per consistency it enables the feature). | ||
|
|
||
| ### Flat Options (`expectFailureError`) | ||
|
|
||
| It was proposed to split the options into `expectFailure` (reason) and `expectFailureError` (validation). | ||
| This was rejected in favor of the nested/polymorphic structure using `match` and `label` properties. This syntax was selected as the preferred choice for its readability and clarity: | ||
| * `match`: Clearly indicates "fails **matching** this error" (Validation). | ||
| * `label`: Clearly indicates the **label** or reason for the expected failure. | ||
| This approach keeps related configuration grouped without polluting the top-level options namespace. | ||
|
|
||
| ## Implementation Details | ||
|
|
||
| ### Validation Logic | ||
|
|
||
| The implementation leverages `assert.throws` internally to perform error validation. | ||
| - If `expectFailure` is a Matcher (RegExp, Class, Object), it is passed as the second argument to `assert.throws(fn, expectFailure)`. | ||
| - If `expectFailure` is a Configuration Object, `expectFailure.match` is passed to `assert.throws`. | ||
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.