You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: proposals/expect-failure-enhancements.md
+26-10Lines changed: 26 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,11 @@
1
1
# Feature proposal: `expectFailure` enhancements
2
2
3
3
## Summary
4
-
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`.
4
+
Update the `expectFailure` option in `test()` to accept different types of values, enabling both **custom failure messages** and **error validation**. 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.
5
5
6
6
## API & Behavior
7
7
8
-
The behavior of `expectFailure` is determined by the type of value provided:
8
+
The behavior of `expectFailure` is strictly determined by the type of value provided:
9
9
10
10
### 1. String: Failure Reason
11
11
When a **non-empty string** is provided, it acts as a documentation message (reason), identical to `skip` and `todo` options.
@@ -20,7 +20,6 @@ test('fails with a specific reason', {
20
20
-**Behavior**: The test is expected to fail. The string is treated as a label/reason.
21
21
-**Validation**: None. It accepts *any* error.
22
22
-**Output**: The reporter displays the string (e.g., `# EXPECTED FAILURE Bug #123...`).
23
-
-**Rationale**: Maintains consistency with existing `test` options where a string implies a reason/description.
24
23
25
24
### 2. RegExp: Error Matcher
26
25
When a **RegExp** is provided, it acts as a validator for the thrown error.
@@ -36,24 +35,41 @@ test('fails with matching error', {
36
35
-**Validation**: Strict matching against the error message.
37
36
-**Output**: Standard expected failure output.
38
37
39
-
## Ambiguity Resolution
40
-
Potential ambiguity between "String as Reason" and "String as Matcher" is resolved by strict type separation:
41
-
*`typeof value === 'string'` → **Reason** (Documentation only)
-**Behavior**: The test passes **only if** the error matches the `match` criteria.
55
+
-**Output**: The reporter displays the `message`.
43
56
44
-
Users needing to match a specific string error message should use a RegExp (e.g., `/Error message/`) to avoid confusion.
57
+
## Ambiguity Resolution
58
+
Potential ambiguity is resolved by strict type separation:
59
+
*`typeof value === 'string'` → **Reason**
60
+
*`value instanceof RegExp` → **Matcher**
61
+
*`typeof value === 'object'` → **Both** (Explicit properties)
45
62
46
63
## Edge Cases & Implementation Details
47
64
48
65
### Empty String (`expectFailure: ''`)
49
66
Following standard JavaScript truthiness rules, an empty string should be treated as **falsy**.
50
-
51
67
*`expectFailure: ''` behaves exactly like `expectFailure: false`.
52
68
* The feature is **disabled**, and the test is expected to pass normally.
53
69
54
70
### Type Safety for `this.passed`
55
71
The implementation must ensure that `this.passed` remains a strict `boolean`.
56
-
Assigning a string directly (e.g., `this.passed = this.expectFailure`) is unsafe as it introduces type pollution (assigning `""` or `"reason"` instead of `false`/`true`).
72
+
Assigning a string directly (e.g., `this.passed = this.expectFailure`) is unsafe as it introduces type pollution.
0 commit comments