Skip to content

Commit dc93f37

Browse files
authored
Return undefined when throws assertions fail
1 parent ddda6ed commit dc93f37

File tree

5 files changed

+26
-28
lines changed

5 files changed

+26
-28
lines changed

docs/03-assertions.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ Finally, this returns a boolean indicating whether the assertion passed.
229229

230230
### `.throws(fn, expectation?, message?)`
231231

232-
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. If the assertion fails then `null` is returned.
232+
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. If the assertion fails then `undefined` is returned.
233233

234234
`expectation` can be an object with one or more of the following properties:
235235

@@ -261,7 +261,7 @@ test('throws', t => {
261261

262262
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.
263263

264-
The thrown value *must* be an error. It is returned so you can run more assertions against it. If the assertion fails then `null` is returned.
264+
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.
265265

266266
`expectation` can be an object with one or more of the following properties:
267267

lib/assert.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ export class Assertions {
469469
let [fn, expectations, message] = args;
470470

471471
if (!checkMessage('throws', message)) {
472-
return null;
472+
return;
473473
}
474474

475475
if (typeof fn !== 'function') {
@@ -479,14 +479,14 @@ export class Assertions {
479479
message: '`t.throws()` must be called with a function',
480480
values: [formatWithLabel('Called with:', fn)]
481481
}));
482-
return null;
482+
return;
483483
}
484484

485485
try {
486486
expectations = validateExpectations('throws', expectations, args.length, experiments);
487487
} catch (error) {
488488
fail(error);
489-
return null;
489+
return;
490490
}
491491

492492
let retval;
@@ -501,7 +501,7 @@ export class Assertions {
501501
message,
502502
values: [formatWithLabel('Function returned a promise. Use `t.throwsAsync()` instead:', retval)]
503503
}));
504-
return null;
504+
return;
505505
}
506506
} catch (error) {
507507
actual = error;
@@ -513,7 +513,7 @@ export class Assertions {
513513
message,
514514
values: [formatWithLabel('Function returned:', retval)]
515515
}));
516-
return null;
516+
return;
517517
}
518518

519519
try {
@@ -535,7 +535,7 @@ export class Assertions {
535535
let [thrower, expectations, message] = args;
536536

537537
if (!checkMessage('throwsAsync', message)) {
538-
return null;
538+
return;
539539
}
540540

541541
if (typeof thrower !== 'function' && !isPromise(thrower)) {
@@ -545,14 +545,14 @@ export class Assertions {
545545
message: '`t.throwsAsync()` must be called with a function or promise',
546546
values: [formatWithLabel('Called with:', thrower)]
547547
}));
548-
return null;
548+
return;
549549
}
550550

551551
try {
552552
expectations = validateExpectations('throwsAsync', expectations, args.length, experiments);
553553
} catch (error) {
554554
fail(error);
555-
return null;
555+
return;
556556
}
557557

558558
const handlePromise = async (promise, wasReturned) => {
@@ -583,7 +583,6 @@ export class Assertions {
583583
return await intermediate;
584584
} catch {
585585
// Don't reject the returned promise, even if the assertion fails.
586-
return null;
587586
}
588587
};
589588

@@ -606,7 +605,7 @@ export class Assertions {
606605
actualStack: actual.stack,
607606
values: [formatWithLabel('Function threw synchronously. Use `t.throws()` instead:', actual)]
608607
}));
609-
return null;
608+
return;
610609
}
611610

612611
if (isPromise(retval)) {
@@ -618,7 +617,6 @@ export class Assertions {
618617
message,
619618
values: [formatWithLabel('Function returned:', retval)]
620619
}));
621-
return null;
622620
});
623621

624622
this.notThrows = withSkip((fn, message) => {

test-d/throws.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ class CustomError extends Error {
1212
}
1313

1414
test('throws', t => {
15-
expectType<Error | null>(t.throws(() => {}));
16-
const error2: CustomError | null = t.throws(() => {});
17-
expectType<CustomError | null>(error2);
18-
expectType<CustomError | null>(t.throws<CustomError>(() => {}));
15+
expectType<Error | undefined>(t.throws(() => {}));
16+
const error2: CustomError | undefined = t.throws(() => {});
17+
expectType<CustomError | undefined>(error2);
18+
expectType<CustomError | undefined>(t.throws<CustomError>(() => {}));
1919
});
2020

2121
test('throwsAsync', async t => {
22-
expectType<Error | null>(await t.throwsAsync(async () => {}));
23-
expectType<CustomError | null>(await t.throwsAsync<CustomError>(async () => {}));
24-
expectType<Error | null>(await t.throwsAsync(Promise.reject()));
25-
expectType<CustomError | null>(await t.throwsAsync<CustomError>(Promise.reject()));
22+
expectType<Error | undefined>(await t.throwsAsync(async () => {}));
23+
expectType<CustomError | undefined>(await t.throwsAsync<CustomError>(async () => {}));
24+
expectType<Error | undefined>(await t.throwsAsync(Promise.reject()));
25+
expectType<CustomError | undefined>(await t.throwsAsync<CustomError>(Promise.reject()));
2626
});

test-tap/assert.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ function throwsAsyncFails(t, fn, subset) {
115115
return add(() => {
116116
lastFailure = null;
117117
return fn().then(retval => {
118-
t.equal(retval, null);
118+
t.equal(retval, undefined);
119119
assertFailure(t, subset);
120120
});
121121
});

types/assertions.d.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -279,9 +279,9 @@ export interface SnapshotAssertion {
279279
export interface ThrowsAssertion {
280280
/**
281281
* Assert that the function throws [an error](https://www.npmjs.com/package/is-error). If so, returns the error value.
282-
* The error must satisfy all expectations. Returns null when the assertion fails.
282+
* The error must satisfy all expectations. Returns undefined when the assertion fails.
283283
*/
284-
<ThrownError extends Error>(fn: () => any, expectations?: ThrowsExpectation, message?: string): ThrownError | null;
284+
<ThrownError extends Error>(fn: () => any, expectations?: ThrowsExpectation, message?: string): ThrownError | undefined;
285285

286286
/** Skip this assertion. */
287287
skip(fn: () => any, expectations?: any, message?: string): void;
@@ -290,16 +290,16 @@ export interface ThrowsAssertion {
290290
export interface ThrowsAsyncAssertion {
291291
/**
292292
* Assert that the async function throws [an error](https://www.npmjs.com/package/is-error). If so, returns the error
293-
* value. Returns null when the assertion fails. You must await the result. The error must satisfy all expectations.
293+
* value. Returns undefined when the assertion fails. You must await the result. The error must satisfy all expectations.
294294
*/
295-
<ThrownError extends Error>(fn: () => PromiseLike<any>, expectations?: ThrowsExpectation, message?: string): Promise<ThrownError | null>;
295+
<ThrownError extends Error>(fn: () => PromiseLike<any>, expectations?: ThrowsExpectation, message?: string): Promise<ThrownError | undefined>;
296296

297297
/**
298298
* Assert that the promise rejects with [an error](https://www.npmjs.com/package/is-error). If so, returns the
299-
* rejection reason. Returns null when the assertion fails. You must await the result. The error must satisfy all
299+
* rejection reason. Returns undefined when the assertion fails. You must await the result. The error must satisfy all
300300
* expectations.
301301
*/
302-
<ThrownError extends Error>(promise: PromiseLike<any>, expectations?: ThrowsExpectation, message?: string): Promise<ThrownError | null>; // eslint-disable-line @typescript-eslint/unified-signatures
302+
<ThrownError extends Error>(promise: PromiseLike<any>, expectations?: ThrowsExpectation, message?: string): Promise<ThrownError | undefined>; // eslint-disable-line @typescript-eslint/unified-signatures
303303

304304
/** Skip this assertion. */
305305
skip(thrower: any, expectations?: any, message?: string): void;

0 commit comments

Comments
 (0)