Skip to content

Commit d0e2161

Browse files
authoredFeb 23, 2020
Support undefined as second argument to t.throws and t.throwsAsync
1 parent 324e45f commit d0e2161

File tree

3 files changed

+22
-22
lines changed

3 files changed

+22
-22
lines changed
 

‎index.d.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -245,16 +245,11 @@ export interface SnapshotAssertion {
245245
}
246246

247247
export interface ThrowsAssertion {
248-
/**
249-
* Assert that the function throws [an error](https://www.npmjs.com/package/is-error). If so, returns the error value.
250-
*/
251-
<ThrownError extends Error>(fn: () => any, expectations?: null, message?: string): ThrownError;
252-
253248
/**
254249
* Assert that the function throws [an error](https://www.npmjs.com/package/is-error). If so, returns the error value.
255250
* The error must satisfy all expectations.
256251
*/
257-
<ThrownError extends Error>(fn: () => any, expectations: ThrowsExpectation, message?: string): ThrownError;
252+
<ThrownError extends Error>(fn: () => any, expectations?: ThrowsExpectation | null, message?: string): ThrownError;
258253

259254
/** Skip this assertion. */
260255
skip(fn: () => any, expectations?: any, message?: string): void;

‎lib/assert.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ function getErrorWithLongStackTrace() {
7373
}
7474

7575
function validateExpectations(assertion, expectations, numArgs) { // eslint-disable-line complexity
76-
if (numArgs === 1 || expectations === null) {
76+
if (numArgs === 1 || expectations === null || expectations === undefined) {
7777
expectations = {};
7878
} else if (
7979
typeof expectations === 'function' ||
@@ -85,7 +85,7 @@ function validateExpectations(assertion, expectations, numArgs) { // eslint-disa
8585
) {
8686
throw new AssertionError({
8787
assertion,
88-
message: `The second argument to \`t.${assertion}()\` must be an expectation object or \`null\``,
88+
message: `The second argument to \`t.${assertion}()\` must be an expectation object, \`null\` or \`undefined\``,
8989
values: [formatWithLabel('Called with:', expectations)]
9090
});
9191
} else {

‎test/assert.js

+19-14
Original file line numberDiff line numberDiff line change
@@ -937,13 +937,18 @@ test('.throws()', gather(t => {
937937
}, null);
938938
});
939939

940-
// Regression test for https://github.com/avajs/ava/issues/1676
941-
fails(t, () => {
940+
passes(t, () => {
942941
assertions.throws(() => {
943942
throw new Error('foo');
944943
}, undefined);
945944
});
946945

946+
passes(t, async () => {
947+
await assertions.throwsAsync(() => {
948+
return Promise.reject(new Error('foo'));
949+
}, undefined);
950+
});
951+
947952
failsWith(t, () => {
948953
assertions.throws(() => {}, null, null);
949954
}, {
@@ -1085,47 +1090,47 @@ test('.throws() fails if passed a bad expectation', t => {
10851090
assertions.throws(() => {}, true);
10861091
}, {
10871092
assertion: 'throws',
1088-
message: 'The second argument to `t.throws()` must be an expectation object or `null`',
1093+
message: 'The second argument to `t.throws()` must be an expectation object, `null` or `undefined`',
10891094
values: [{label: 'Called with:', formatted: /true/}]
10901095
});
10911096

10921097
failsWith(t, () => {
10931098
assertions.throws(() => {}, 'foo');
10941099
}, {
10951100
assertion: 'throws',
1096-
message: 'The second argument to `t.throws()` must be an expectation object or `null`',
1101+
message: 'The second argument to `t.throws()` must be an expectation object, `null` or `undefined`',
10971102
values: [{label: 'Called with:', formatted: /foo/}]
10981103
});
10991104

11001105
failsWith(t, () => {
11011106
assertions.throws(() => {}, /baz/);
11021107
}, {
11031108
assertion: 'throws',
1104-
message: 'The second argument to `t.throws()` must be an expectation object or `null`',
1109+
message: 'The second argument to `t.throws()` must be an expectation object, `null` or `undefined`',
11051110
values: [{label: 'Called with:', formatted: /baz/}]
11061111
});
11071112

11081113
failsWith(t, () => {
11091114
assertions.throws(() => {}, class Bar {});
11101115
}, {
11111116
assertion: 'throws',
1112-
message: 'The second argument to `t.throws()` must be an expectation object or `null`',
1117+
message: 'The second argument to `t.throws()` must be an expectation object, `null` or `undefined`',
11131118
values: [{label: 'Called with:', formatted: /Bar/}]
11141119
});
11151120

11161121
failsWith(t, () => {
11171122
assertions.throws(() => {}, {});
11181123
}, {
11191124
assertion: 'throws',
1120-
message: 'The second argument to `t.throws()` must be an expectation object or `null`',
1125+
message: 'The second argument to `t.throws()` must be an expectation object, `null` or `undefined`',
11211126
values: [{label: 'Called with:', formatted: /\{\}/}]
11221127
});
11231128

11241129
failsWith(t, () => {
11251130
assertions.throws(() => {}, []);
11261131
}, {
11271132
assertion: 'throws',
1128-
message: 'The second argument to `t.throws()` must be an expectation object or `null`',
1133+
message: 'The second argument to `t.throws()` must be an expectation object, `null` or `undefined`',
11291134
values: [{label: 'Called with:', formatted: /\[\]/}]
11301135
});
11311136

@@ -1177,47 +1182,47 @@ test('.throwsAsync() fails if passed a bad expectation', t => {
11771182
assertions.throwsAsync(() => {}, true);
11781183
}, {
11791184
assertion: 'throwsAsync',
1180-
message: 'The second argument to `t.throwsAsync()` must be an expectation object or `null`',
1185+
message: 'The second argument to `t.throwsAsync()` must be an expectation object, `null` or `undefined`',
11811186
values: [{label: 'Called with:', formatted: /true/}]
11821187
});
11831188

11841189
failsWith(t, () => {
11851190
assertions.throwsAsync(() => {}, 'foo');
11861191
}, {
11871192
assertion: 'throwsAsync',
1188-
message: 'The second argument to `t.throwsAsync()` must be an expectation object or `null`',
1193+
message: 'The second argument to `t.throwsAsync()` must be an expectation object, `null` or `undefined`',
11891194
values: [{label: 'Called with:', formatted: /foo/}]
11901195
});
11911196

11921197
failsWith(t, () => {
11931198
assertions.throwsAsync(() => {}, /baz/);
11941199
}, {
11951200
assertion: 'throwsAsync',
1196-
message: 'The second argument to `t.throwsAsync()` must be an expectation object or `null`',
1201+
message: 'The second argument to `t.throwsAsync()` must be an expectation object, `null` or `undefined`',
11971202
values: [{label: 'Called with:', formatted: /baz/}]
11981203
});
11991204

12001205
failsWith(t, () => {
12011206
assertions.throwsAsync(() => {}, class Bar {});
12021207
}, {
12031208
assertion: 'throwsAsync',
1204-
message: 'The second argument to `t.throwsAsync()` must be an expectation object or `null`',
1209+
message: 'The second argument to `t.throwsAsync()` must be an expectation object, `null` or `undefined`',
12051210
values: [{label: 'Called with:', formatted: /Bar/}]
12061211
});
12071212

12081213
failsWith(t, () => {
12091214
assertions.throwsAsync(() => {}, {});
12101215
}, {
12111216
assertion: 'throwsAsync',
1212-
message: 'The second argument to `t.throwsAsync()` must be an expectation object or `null`',
1217+
message: 'The second argument to `t.throwsAsync()` must be an expectation object, `null` or `undefined`',
12131218
values: [{label: 'Called with:', formatted: /\{\}/}]
12141219
});
12151220

12161221
failsWith(t, () => {
12171222
assertions.throwsAsync(() => {}, []);
12181223
}, {
12191224
assertion: 'throwsAsync',
1220-
message: 'The second argument to `t.throwsAsync()` must be an expectation object or `null`',
1225+
message: 'The second argument to `t.throwsAsync()` must be an expectation object, `null` or `undefined`',
12211226
values: [{label: 'Called with:', formatted: /\[\]/}]
12221227
});
12231228

0 commit comments

Comments
 (0)
Please sign in to comment.