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
Fixesavajs#3201.
Assertions now throw a `TestFailure` error when they fail. This error is not exported or documented and should not be used or thrown manually. You cannot catch this error in order to recover from a failure, use `t.try()` instead.
All assertions except for `throws` and `throwsAsync` now return `true` when they pass. This is useful for some of the assertions in TypeScript where they can be used as a type guard.
Committing a failed `t.try()` result now also throws.
Copy file name to clipboardexpand all lines: docs/03-assertions.md
+23-25
Original file line number
Diff line number
Diff line change
@@ -21,7 +21,13 @@ test('unicorns are truthy', t => {
21
21
22
22
If multiple assertion failures are encountered within a single test, AVA will only display the *first* one.
23
23
24
-
Assertions return a boolean indicating whether they passed. You can use this to return early from a test. Note that this does not apply to the "throws" and `snapshot()` assertions.
24
+
In AVA 6, assertions return `true` if they've passed and throw otherwise. Catching this error does not cause the test to pass. The error value is undocumented.
25
+
26
+
In AVA 5, assertions return a boolean and do not throw. You can use this to return early from a test. The `snapshot()` assertion does not return a value.
27
+
28
+
If you use TypeScript you can use some assertions as type guards.
29
+
30
+
Note that the "throws" assertions return the error that was thrown (provided the assertion passed). In AVA 5, they return `undefined` if the assertion failed.
25
31
26
32
## Assertion planning
27
33
@@ -95,47 +101,47 @@ test('custom assertion', t => {
95
101
96
102
### `.pass(message?)`
97
103
98
-
Passing assertion. Returns a boolean indicating whether the assertion passed.
104
+
Passing assertion.
99
105
100
106
### `.fail(message?)`
101
107
102
-
Failing assertion. Returns a boolean indicating whether the assertion passed.
108
+
Failing assertion.
103
109
104
110
### `.assert(actual, message?)`
105
111
106
-
Asserts that `actual` is truthy. Returns a boolean indicating whether the assertion passed.
112
+
Asserts that `actual` is truthy.
107
113
108
114
### `.truthy(actual, message?)`
109
115
110
-
Assert that `actual` is truthy. Returns a boolean indicating whether the assertion passed.
116
+
Assert that `actual` is truthy.
111
117
112
118
### `.falsy(actual, message?)`
113
119
114
-
Assert that `actual` is falsy. Returns a boolean indicating whether the assertion passed.
120
+
Assert that `actual` is falsy.
115
121
116
122
### `.true(actual, message?)`
117
123
118
-
Assert that `actual` is `true`. Returns a boolean indicating whether the assertion passed.
124
+
Assert that `actual` is `true`.
119
125
120
126
### `.false(actual, message?)`
121
127
122
-
Assert that `actual` is `false`. Returns a boolean indicating whether the assertion passed.
128
+
Assert that `actual` is `false`.
123
129
124
130
### `.is(actual, expected, message?)`
125
131
126
-
Assert that `actual` is the same as `expected`. This is based on [`Object.is()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is). Returns a boolean indicating whether the assertion passed.
132
+
Assert that `actual` is the same as `expected`. This is based on [`Object.is()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is).
127
133
128
134
### `.not(actual, expected, message?)`
129
135
130
-
Assert that `actual` is not the same as `expected`. This is based on [`Object.is()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is). Returns a boolean indicating whether the assertion passed.
136
+
Assert that `actual` is not the same as `expected`. This is based on [`Object.is()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is).
131
137
132
138
### `.deepEqual(actual, expected, message?)`
133
139
134
140
Assert that `actual` is deeply equal to `expected`. See [Concordance](https://github.com/concordancejs/concordance) for details.
135
141
136
142
### `.notDeepEqual(actual, expected, message?)`
137
143
138
-
Assert that `actual` is not deeply equal to `expected`. The inverse of `.deepEqual()`. Returns a boolean indicating whether the assertion passed.
144
+
Assert that `actual` is not deeply equal to `expected`. The inverse of `.deepEqual()`.
139
145
140
146
### `.like(actual, selector, message?)`
141
147
@@ -168,12 +174,9 @@ You can also use arrays, but note that any indices in `actual` that are not in `
168
174
t.like([1, 2, 3, 4], [1, , 3])
169
175
```
170
176
171
-
Finally, this returns a boolean indicating whether the assertion passed.
172
-
173
177
### `.throws(fn, expectation?, message?)`
174
178
175
-
Assert that an error is thrown. `fn` must be a function which should throw. By default, the thrown value *must* be an error. It is returned so you can run more assertions against it. If the assertion fails then `undefined` is returned.
176
-
179
+
Assert that an error is thrown. `fn` must be a function which should throw. By default, the thrown value *must* be an error. It is returned so you can run more assertions against it.
177
180
`expectation` can be an object with one or more of the following properties:
178
181
179
182
*`any`: a boolean only available in AVA 6, if `true` then the thrown value does not need to be an error. Defaults to `false`
@@ -208,8 +211,7 @@ test('throws', t => {
208
211
209
212
Assert that an error is thrown. `thrower` can be an async function which should throw, or a promise that should reject. This assertion must be awaited.
210
213
211
-
By default, the thrown value *must* be an error. It is returned so you can run more assertions against it. If the assertion fails then `undefined` is returned.
212
-
214
+
By default, the thrown value *must* be an error. It is returned so you can run more assertions against it.
213
215
`expectation` can be an object with one or more of the following properties:
214
216
215
217
*`any`: a boolean only available in AVA 6, if `true` then the thrown value does not need to be an error. Defaults to `false`
@@ -245,7 +247,7 @@ test('rejects', async t => {
245
247
246
248
### `.notThrows(fn, message?)`
247
249
248
-
Assert that no error is thrown. `fn` must be a function which shouldn't throw. Does not return anything.
250
+
Assert that no error is thrown. `fn` must be a function which shouldn't throw.
249
251
250
252
### `.notThrowsAsync(nonThrower, message?)`
251
253
@@ -259,15 +261,13 @@ test('resolves', async t => {
259
261
});
260
262
```
261
263
262
-
Does not return anything.
263
-
264
264
### `.regex(contents, regex, message?)`
265
265
266
-
Assert that `contents` matches `regex`. Returns a boolean indicating whether the assertion passed.
266
+
Assert that `contents` matches `regex`.
267
267
268
268
### `.notRegex(contents, regex, message?)`
269
269
270
-
Assert that `contents` does not match `regex`. Returns a boolean indicating whether the assertion passed.
270
+
Assert that `contents` does not match `regex`.
271
271
272
272
### `.snapshot(expected, message?)`
273
273
@@ -279,7 +279,7 @@ Compares the `expected` value with a previously recorded snapshot. Snapshots are
279
279
280
280
The implementation function behaves the same as any other test function. You can even use macros. The first title argument is always optional. Additional arguments are passed to the implementation or macro function.
281
281
282
-
`.try()` is an asynchronous function. You must `await` it. The result object has `commit()` and `discard()` methods. You must decide whether to commit or discard the result. If you commit a failed result, your test will fail.
282
+
`.try()` is an asynchronous function. You must `await` it. The result object has `commit()` and `discard()` methods. You must decide whether to commit or discard the result. If you commit a failed result, your test will fail. In AVA 6, calling `commit()` on a failed result will throw an error.
283
283
284
284
You can check whether the attempt passed using the `passed` property. Any assertion errors are available through the `errors` property. The attempt title is available through the `title` property.
285
285
@@ -318,5 +318,3 @@ test('flaky macro', async t => {
318
318
secondTry.commit();
319
319
});
320
320
```
321
-
322
-
Returns a boolean indicating whether the assertion passed.
Copy file name to clipboardexpand all lines: docs/recipes/typescript.md
+2-2
Original file line number
Diff line number
Diff line change
@@ -177,7 +177,7 @@ Note that, despite the type cast above, when executing `t.context` is an empty o
177
177
178
178
## Typing `throws` assertions
179
179
180
-
The `t.throws()` and `t.throwsAsync()` assertions are typed to always return an Error. You can customize the error class using generics:
180
+
In AVA 6, the `t.throws()` and `t.throwsAsync()` assertions are typed to always return an `Error`. You can customize the error class using generics:
181
181
182
182
```ts
183
183
importtestfrom'ava';
@@ -206,6 +206,6 @@ test('throwsAsync', async t => {
206
206
});
207
207
```
208
208
209
-
Note that, despite the typing, the assertion returns `undefined` if it fails. Typing the assertions as returning `Error | undefined`didn't seem like the pragmatic choice.
209
+
In AVA 5, the assertion is typed to return the `Error` if the assertion passes *or*`undefined`if it fails.
0 commit comments