Skip to content

Commit 72a4e2f

Browse files
Return booleans indicating whether assertions passed
Co-authored-by: Mark Wubben <mark@novemberborn.net>
1 parent d8b1e89 commit 72a4e2f

File tree

4 files changed

+550
-471
lines changed

4 files changed

+550
-471
lines changed

docs/03-assertions.md

+26-14
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ test('unicorns are truthy', t => {
2121

2222
If multiple assertion failures are encountered within a single test, AVA will only display the *first* one.
2323

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" assertions.
25+
2426
## Assertion planning
2527

2628
Assertion plans ensure tests only pass when a specific number of assertions have been executed. They'll help you catch cases where tests exit too early. They'll also cause tests to fail if too many assertions are executed, which can be useful if you have assertions inside callbacks or loops.
@@ -165,47 +167,47 @@ test('custom assertion', t => {
165167

166168
### `.pass(message?)`
167169

168-
Passing assertion.
170+
Passing assertion. Returns a boolean indicating whether the assertion passed.
169171

170172
### `.fail(message?)`
171173

172-
Failing assertion.
174+
Failing assertion. Returns a boolean indicating whether the assertion passed.
173175

174176
### `.assert(value, message?)`
175177

176-
Asserts that `value` is truthy. This is [`power-assert`](#enhanced-assertion-messages) enabled.
178+
Asserts that `value` is truthy. This is [`power-assert`](#enhanced-assertion-messages) enabled. Returns a boolean indicating whether the assertion passed.
177179

178180
### `.truthy(value, message?)`
179181

180-
Assert that `value` is truthy.
182+
Assert that `value` is truthy. Returns a boolean indicating whether the assertion passed.
181183

182184
### `.falsy(value, message?)`
183185

184-
Assert that `value` is falsy.
186+
Assert that `value` is falsy. Returns a boolean indicating whether the assertion passed.
185187

186188
### `.true(value, message?)`
187189

188-
Assert that `value` is `true`.
190+
Assert that `value` is `true`. Returns a boolean indicating whether the assertion passed.
189191

190192
### `.false(value, message?)`
191193

192-
Assert that `value` is `false`.
194+
Assert that `value` is `false`. Returns a boolean indicating whether the assertion passed.
193195

194196
### `.is(value, expected, message?)`
195197

196-
Assert that `value` 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).
198+
Assert that `value` 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.
197199

198200
### `.not(value, expected, message?)`
199201

200-
Assert that `value` 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).
202+
Assert that `value` 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.
201203

202204
### `.deepEqual(value, expected, message?)`
203205

204206
Assert that `value` is deeply equal to `expected`. See [Concordance](https://github.com/concordancejs/concordance) for details. In AVA 3 this works with [React elements and `react-test-renderer`](https://github.com/concordancejs/react).
205207

206208
### `.notDeepEqual(value, expected, message?)`
207209

208-
Assert that `value` is not deeply equal to `expected`. The inverse of `.deepEqual()`.
210+
Assert that `value` is not deeply equal to `expected`. The inverse of `.deepEqual()`. Returns a boolean indicating whether the assertion passed.
209211

210212
### `.like(value, selector, message?)`
211213

@@ -232,6 +234,8 @@ t.like({
232234
})
233235
```
234236

237+
Finally, this returns a boolean indicating whether the assertion passed.
238+
235239
### `.throws(fn, expectation?, message?)`
236240

237241
Assert that an error is thrown. `fn` must be a function which should throw. The thrown value *must* be an error. It is returned so you can run more assertions against it.
@@ -262,6 +266,8 @@ test('throws', t => {
262266
});
263267
```
264268

269+
Does not return anything.
270+
265271
### `.throwsAsync(thrower, expectation?, message?)`
266272

267273
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.
@@ -297,9 +303,11 @@ test('rejects', async t => {
297303
});
298304
```
299305

306+
Does not return anything.
307+
300308
### `.notThrows(fn, message?)`
301309

302-
Assert that no error is thrown. `fn` must be a function which shouldn't throw.
310+
Assert that no error is thrown. `fn` must be a function which shouldn't throw. Does not return anything.
303311

304312
### `.notThrowsAsync(nonThrower, message?)`
305313

@@ -313,21 +321,23 @@ test('resolves', async t => {
313321
});
314322
```
315323

324+
Does not return anything.
325+
316326
### `.regex(contents, regex, message?)`
317327

318-
Assert that `contents` matches `regex`.
328+
Assert that `contents` matches `regex`. Returns a boolean indicating whether the assertion passed.
319329

320330
### `.notRegex(contents, regex, message?)`
321331

322-
Assert that `contents` does not match `regex`.
332+
Assert that `contents` does not match `regex`. Returns a boolean indicating whether the assertion passed.
323333

324334
### `.snapshot(expected, message?)`
325335

326336
Compares the `expected` value with a previously recorded snapshot. Snapshots are stored for each test, so ensure you give your tests unique titles.
327337

328338
AVA 3 supports an `options` object that lets you select a specific snapshot, for instance `{id: 'my snapshot'}`. This is buggy and will be removed in AVA 4.
329339

330-
Snapshot assertions cannot be skipped when snapshots are being updated.
340+
Snapshot assertions cannot be skipped when snapshots are being updated. Returns a boolean indicating whether the assertion passed.
331341

332342
### `.try(title?, implementation | macro | macro[], ...args?)`
333343

@@ -374,3 +384,5 @@ test('flaky macro', async t => {
374384
secondTry.commit();
375385
});
376386
```
387+
388+
Returns a boolean indicating whether the assertion passed.

0 commit comments

Comments
 (0)