Skip to content

Commit

Permalink
Support undefined as second argument to t.throws and t.throwsAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
stavalfi committed Feb 23, 2020
1 parent 324e45f commit d0e2161
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
7 changes: 1 addition & 6 deletions index.d.ts
Expand Up @@ -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.
*/
<ThrownError extends Error>(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.
*/
<ThrownError extends Error>(fn: () => any, expectations: ThrowsExpectation, message?: string): ThrownError;
<ThrownError extends Error>(fn: () => any, expectations?: ThrowsExpectation | null, message?: string): ThrownError;

/** Skip this assertion. */
skip(fn: () => any, expectations?: any, message?: string): void;
Expand Down
4 changes: 2 additions & 2 deletions lib/assert.js
Expand Up @@ -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' ||
Expand All @@ -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 {
Expand Down
33 changes: 19 additions & 14 deletions test/assert.js
Expand Up @@ -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);
}, {
Expand Down Expand Up @@ -1085,47 +1090,47 @@ 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/}]
});

failsWith(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/}]
});

failsWith(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/}]
});

failsWith(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/}]
});

failsWith(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: /\{\}/}]
});

failsWith(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: /\[\]/}]
});

Expand Down Expand Up @@ -1177,47 +1182,47 @@ 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/}]
});

failsWith(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/}]
});

failsWith(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/}]
});

failsWith(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/}]
});

failsWith(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: /\{\}/}]
});

failsWith(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: /\[\]/}]
});

Expand Down

0 comments on commit d0e2161

Please sign in to comment.