Skip to content

Commit abb5912

Browse files
committed
docs: update expectFailure proposal with validation details
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).
1 parent 049c2d3 commit abb5912

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

proposals/expect-failure-enhancements.md

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,16 @@ test('fails with a specific reason', {
2121
- **Validation**: None. It accepts *any* error.
2222
- **Output**: The reporter displays the string (e.g., `# EXPECTED FAILURE Bug #123...`).
2323

24-
### 2. RegExp: Error Matcher
25-
When a **RegExp** is provided, it acts as a validator for the thrown error.
24+
### 2. RegExp: Error Matcher (via Object)
25+
Use the object form with the `with` property.
2626

2727
```js
2828
test('fails with matching error', {
29-
expectFailure: /expected error message/
29+
expectFailure: { with: /expected error message/ }
3030
}, () => {
3131
throw new Error('this is the expected error message');
3232
});
3333
```
34-
- **Behavior**: The test passes **only if** the thrown error matches the regular expression.
35-
- **Validation**: Strict matching against the error message.
36-
- **Output**: Standard expected failure output.
3734

3835
### 3. Object: Reason & Validation
3936
When an **Object** is provided, it allows specifying both a failure reason and validation logic simultaneously.
@@ -42,23 +39,42 @@ When an **Object** is provided, it allows specifying both a failure reason and v
4239
test('fails with reason and specific error', {
4340
expectFailure: {
4441
message: 'Bug #123: Edge case behavior', // Reason
45-
match: /Index out of bounds/ // Validation
42+
with: /Index out of bounds/ // Validation
4643
}
4744
}, () => {
4845
throw new RangeError('Index out of bounds');
4946
});
5047
```
5148
- **Properties**:
5249
- `message` (String): The failure reason/label (displayed in reporter).
53-
- `match` (RegExp | Object | Function): Validation logic (similar to `assert.throws` validation argument).
54-
- **Behavior**: The test passes **only if** the error matches the `match` criteria.
50+
- `with` (RegExp | Object | Function | Class): Validation logic. This is passed directly to `assert.throws` validation argument, supporting all its capabilities.
51+
- **Behavior**: The test passes **only if** the error matches the `with` criteria.
5552
- **Output**: The reporter displays the `message`.
5653

5754
## Ambiguity Resolution
5855
Potential ambiguity is resolved by strict type separation:
5956
* `typeof value === 'string'`**Reason**
60-
* `value instanceof RegExp`**Matcher**
61-
* `typeof value === 'object'`**Both** (Explicit properties)
57+
* `typeof value === 'object'`**Configuration Object** (`message` and/or `with`)
58+
59+
## Alternatives Considered
60+
61+
### Flat Options (`expectFailureError`)
62+
It was proposed to split the options into `expectFailure` (reason) and `expectFailureError` (validation).
63+
```js
64+
{
65+
expectFailure: 'reason',
66+
expectFailureError: /error/
67+
}
68+
```
69+
This was rejected in favor of the nested object structure to:
70+
1. Keep related configuration grouped.
71+
2. Avoid polluting the top-level options namespace.
72+
3. Allow future extensibility within the `expectFailure` object.
73+
74+
## Implementation Details
75+
76+
### Validation Logic
77+
The implementation leverages `assert.throws` internally to perform error validation. This ensures consistency with the existing assertion ecosystem and supports advanced validation (Classes, Custom Functions) out of the box without code duplication.
6278

6379
## Edge Cases & Implementation Details
6480

0 commit comments

Comments
 (0)