From d0e21612390d50f084f7062b051164624ef391ff Mon Sep 17 00:00:00 2001 From: Stav Alfi Date: Sun, 23 Feb 2020 17:02:11 +0200 Subject: [PATCH] Support undefined as second argument to t.throws and t.throwsAsync --- index.d.ts | 7 +------ lib/assert.js | 4 ++-- test/assert.js | 33 +++++++++++++++++++-------------- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/index.d.ts b/index.d.ts index 44ca5a4e2..28cead652 100644 --- a/index.d.ts +++ b/index.d.ts @@ -245,16 +245,11 @@ export interface SnapshotAssertion { } export interface ThrowsAssertion { - /** - * Assert that the function throws [an error](https://www.npmjs.com/package/is-error). If so, returns the error value. - */ - (fn: () => any, expectations?: null, message?: string): ThrownError; - /** * Assert that the function throws [an error](https://www.npmjs.com/package/is-error). If so, returns the error value. * The error must satisfy all expectations. */ - (fn: () => any, expectations: ThrowsExpectation, message?: string): ThrownError; + (fn: () => any, expectations?: ThrowsExpectation | null, message?: string): ThrownError; /** Skip this assertion. */ skip(fn: () => any, expectations?: any, message?: string): void; diff --git a/lib/assert.js b/lib/assert.js index c1608731e..3c1435d4a 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -73,7 +73,7 @@ function getErrorWithLongStackTrace() { } function validateExpectations(assertion, expectations, numArgs) { // eslint-disable-line complexity - if (numArgs === 1 || expectations === null) { + if (numArgs === 1 || expectations === null || expectations === undefined) { expectations = {}; } else if ( typeof expectations === 'function' || @@ -85,7 +85,7 @@ function validateExpectations(assertion, expectations, numArgs) { // eslint-disa ) { throw new AssertionError({ assertion, - message: `The second argument to \`t.${assertion}()\` must be an expectation object or \`null\``, + message: `The second argument to \`t.${assertion}()\` must be an expectation object, \`null\` or \`undefined\``, values: [formatWithLabel('Called with:', expectations)] }); } else { diff --git a/test/assert.js b/test/assert.js index 7face9344..48d5dd4c9 100644 --- a/test/assert.js +++ b/test/assert.js @@ -937,13 +937,18 @@ test('.throws()', gather(t => { }, null); }); - // Regression test for https://github.com/avajs/ava/issues/1676 - fails(t, () => { + passes(t, () => { assertions.throws(() => { throw new Error('foo'); }, undefined); }); + passes(t, async () => { + await assertions.throwsAsync(() => { + return Promise.reject(new Error('foo')); + }, undefined); + }); + failsWith(t, () => { assertions.throws(() => {}, null, null); }, { @@ -1085,7 +1090,7 @@ test('.throws() fails if passed a bad expectation', t => { assertions.throws(() => {}, true); }, { assertion: 'throws', - message: 'The second argument to `t.throws()` must be an expectation object or `null`', + message: 'The second argument to `t.throws()` must be an expectation object, `null` or `undefined`', values: [{label: 'Called with:', formatted: /true/}] }); @@ -1093,7 +1098,7 @@ test('.throws() fails if passed a bad expectation', t => { assertions.throws(() => {}, 'foo'); }, { assertion: 'throws', - message: 'The second argument to `t.throws()` must be an expectation object or `null`', + message: 'The second argument to `t.throws()` must be an expectation object, `null` or `undefined`', values: [{label: 'Called with:', formatted: /foo/}] }); @@ -1101,7 +1106,7 @@ test('.throws() fails if passed a bad expectation', t => { assertions.throws(() => {}, /baz/); }, { assertion: 'throws', - message: 'The second argument to `t.throws()` must be an expectation object or `null`', + message: 'The second argument to `t.throws()` must be an expectation object, `null` or `undefined`', values: [{label: 'Called with:', formatted: /baz/}] }); @@ -1109,7 +1114,7 @@ test('.throws() fails if passed a bad expectation', t => { assertions.throws(() => {}, class Bar {}); }, { assertion: 'throws', - message: 'The second argument to `t.throws()` must be an expectation object or `null`', + message: 'The second argument to `t.throws()` must be an expectation object, `null` or `undefined`', values: [{label: 'Called with:', formatted: /Bar/}] }); @@ -1117,7 +1122,7 @@ test('.throws() fails if passed a bad expectation', t => { assertions.throws(() => {}, {}); }, { assertion: 'throws', - message: 'The second argument to `t.throws()` must be an expectation object or `null`', + message: 'The second argument to `t.throws()` must be an expectation object, `null` or `undefined`', values: [{label: 'Called with:', formatted: /\{\}/}] }); @@ -1125,7 +1130,7 @@ test('.throws() fails if passed a bad expectation', t => { assertions.throws(() => {}, []); }, { assertion: 'throws', - message: 'The second argument to `t.throws()` must be an expectation object or `null`', + message: 'The second argument to `t.throws()` must be an expectation object, `null` or `undefined`', values: [{label: 'Called with:', formatted: /\[\]/}] }); @@ -1177,7 +1182,7 @@ test('.throwsAsync() fails if passed a bad expectation', t => { assertions.throwsAsync(() => {}, true); }, { assertion: 'throwsAsync', - message: 'The second argument to `t.throwsAsync()` must be an expectation object or `null`', + message: 'The second argument to `t.throwsAsync()` must be an expectation object, `null` or `undefined`', values: [{label: 'Called with:', formatted: /true/}] }); @@ -1185,7 +1190,7 @@ test('.throwsAsync() fails if passed a bad expectation', t => { assertions.throwsAsync(() => {}, 'foo'); }, { assertion: 'throwsAsync', - message: 'The second argument to `t.throwsAsync()` must be an expectation object or `null`', + message: 'The second argument to `t.throwsAsync()` must be an expectation object, `null` or `undefined`', values: [{label: 'Called with:', formatted: /foo/}] }); @@ -1193,7 +1198,7 @@ test('.throwsAsync() fails if passed a bad expectation', t => { assertions.throwsAsync(() => {}, /baz/); }, { assertion: 'throwsAsync', - message: 'The second argument to `t.throwsAsync()` must be an expectation object or `null`', + message: 'The second argument to `t.throwsAsync()` must be an expectation object, `null` or `undefined`', values: [{label: 'Called with:', formatted: /baz/}] }); @@ -1201,7 +1206,7 @@ test('.throwsAsync() fails if passed a bad expectation', t => { assertions.throwsAsync(() => {}, class Bar {}); }, { assertion: 'throwsAsync', - message: 'The second argument to `t.throwsAsync()` must be an expectation object or `null`', + message: 'The second argument to `t.throwsAsync()` must be an expectation object, `null` or `undefined`', values: [{label: 'Called with:', formatted: /Bar/}] }); @@ -1209,7 +1214,7 @@ test('.throwsAsync() fails if passed a bad expectation', t => { assertions.throwsAsync(() => {}, {}); }, { assertion: 'throwsAsync', - message: 'The second argument to `t.throwsAsync()` must be an expectation object or `null`', + message: 'The second argument to `t.throwsAsync()` must be an expectation object, `null` or `undefined`', values: [{label: 'Called with:', formatted: /\{\}/}] }); @@ -1217,7 +1222,7 @@ test('.throwsAsync() fails if passed a bad expectation', t => { assertions.throwsAsync(() => {}, []); }, { assertion: 'throwsAsync', - message: 'The second argument to `t.throwsAsync()` must be an expectation object or `null`', + message: 'The second argument to `t.throwsAsync()` must be an expectation object, `null` or `undefined`', values: [{label: 'Called with:', formatted: /\[\]/}] });