Skip to content

Commit 6d7c019

Browse files
committed
fixup! Support expectation object in t.throws()
1 parent 4aa2d27 commit 6d7c019

File tree

5 files changed

+23
-15
lines changed

5 files changed

+23
-15
lines changed

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ export interface ObservableLike {
55
export type Constructor = (new (...args: Array<any>) => any);
66

77
export type ThrowsExpectation = {
8+
instanceOf?: Constructor;
89
is?: Error;
910
message?: string | RegExp;
1011
name?: string;
11-
of?: Constructor;
1212
};
1313

1414
export type ThrowsErrorValidator = Constructor | RegExp | string | ThrowsExpectation;

index.js.flow

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ export interface ObservableLike {
1010
export type Constructor = Class<{constructor(...args: Array<any>): any}>;
1111

1212
export type ThrowsExpectation = {
13+
instanceOf?: Constructor;
1314
is?: Error;
1415
message?: string | RegExp;
1516
name?: string;
16-
of?: Constructor;
1717
};
1818

1919
export type ThrowsErrorValidator = Constructor | RegExp | string | ThrowsExpectation;

lib/assert.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,23 +171,23 @@ function wrapAssertions(callbacks) {
171171
}
172172

173173
if (typeof expected === 'function') {
174-
expected = {of: expected};
174+
expected = {instanceOf: expected};
175175
} else if (typeof expected === 'string' || expected instanceof RegExp) {
176176
expected = {message: expected};
177177
} else if (arguments.length === 1 || expected === null) {
178178
expected = {};
179-
} else if (typeof expected !== 'object' || Array.isArray(expected)) {
179+
} else if (typeof expected !== 'object' || Array.isArray(expected) || Object.keys(expected).length === 0) {
180180
fail(this, new AssertionError({
181181
assertion: 'throws',
182182
message: 'The second argument to `t.throws()` must be a function, string, regular expression, expectation object or `null`',
183183
values: [formatWithLabel('Called with:', expected)]
184184
}));
185185
return;
186186
} else {
187-
if (hasOwnProperty(expected, 'of') && typeof expected.of !== 'function') {
187+
if (hasOwnProperty(expected, 'instanceOf') && typeof expected.instanceOf !== 'function') {
188188
fail(this, new AssertionError({
189189
assertion: 'throws',
190-
message: 'The `of` property of the second argument to `t.throws()` must be a function',
190+
message: 'The `instanceOf` property of the second argument to `t.throws()` must be a function',
191191
values: [formatWithLabel('Called with:', expected)]
192192
}));
193193
return;
@@ -213,10 +213,10 @@ function wrapAssertions(callbacks) {
213213

214214
for (const key of Object.keys(expected)) {
215215
switch (key) {
216+
case 'instanceOf':
216217
case 'is':
217218
case 'message':
218219
case 'name':
219-
case 'of':
220220
continue;
221221
default:
222222
fail(this, new AssertionError({
@@ -253,14 +253,14 @@ function wrapAssertions(callbacks) {
253253
});
254254
}
255255

256-
if (expected.of && !(actual instanceof expected.of)) {
256+
if (expected.instanceOf && !(actual instanceof expected.instanceOf)) {
257257
throw new AssertionError({
258258
assertion: 'throws',
259259
message,
260260
stack,
261261
values: [
262262
formatWithLabel(`${prefix} unexpected exception:`, actual),
263-
formatWithLabel('Expected instance of:', expected.of)
263+
formatWithLabel('Expected instance of:', expected.instanceOf)
264264
]
265265
});
266266
}

readme.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -918,12 +918,12 @@ The thrown value *must* be an error. It is returned so you can run more assertio
918918

919919
`expected` can be a constructor, in which case the thrown error must be an instance of the constructor. It can be a string, which is compared against the thrown error's message, or a regular expression which is matched against this message. You can also specify a matcher object with one or more of the following properties:
920920

921+
* `instanceOf`: a constructor, the thrown error must be an instance of
921922
* `is`: the thrown error must be strictly equal to `expected.is`
922923
* `message`: either a string, which is compared against the thrown error's message, or a regular expression, which is matched against this message
923924
* `name`: the expected `.name` value of the thrown error
924-
* `of`: a constructor, the thrown error must be an instance of
925925

926-
`expected` does not need to be specified. If you don't need it but do want to set an assertion message you have to specify `null` or the empty object `{}`.
926+
`expected` does not need to be specified. If you don't need it but do want to set an assertion message you have to specify `null`.
927927

928928
Example:
929929

@@ -964,7 +964,7 @@ When testing an asynchronous function you must also wait for the assertion to co
964964
test('throws', async t => {
965965
await t.throws(async () => {
966966
throw new TypeError('🦄');
967-
}, {of: TypeError, message: '🦄'});
967+
}, {instanceOf: TypeError, message: '🦄'});
968968
});
969969
```
970970

test/assert.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,14 @@ test('.throws() fails if passed a bad expectation', t => {
965965
values: [{label: 'Called with:', formatted: /true/}]
966966
});
967967

968+
failsWith(t, () => {
969+
assertions.throws(() => {}, {});
970+
}, {
971+
assertion: 'throws',
972+
message: 'The second argument to `t.throws()` must be a function, string, regular expression, expectation object or `null`',
973+
values: [{label: 'Called with:', formatted: /\{\}/}]
974+
});
975+
968976
failsWith(t, () => {
969977
assertions.throws(() => {}, []);
970978
}, {
@@ -974,11 +982,11 @@ test('.throws() fails if passed a bad expectation', t => {
974982
});
975983

976984
failsWith(t, () => {
977-
assertions.throws(() => {}, {of: null});
985+
assertions.throws(() => {}, {instanceOf: null});
978986
}, {
979987
assertion: 'throws',
980-
message: 'The `of` property of the second argument to `t.throws()` must be a function',
981-
values: [{label: 'Called with:', formatted: /of: null/}]
988+
message: 'The `instanceOf` property of the second argument to `t.throws()` must be a function',
989+
values: [{label: 'Called with:', formatted: /instanceOf: null/}]
982990
});
983991

984992
failsWith(t, () => {

0 commit comments

Comments
 (0)