Skip to content

Commit 179f26a

Browse files
jamiebuildsnovemberborn
authored andcommitted
Support err.code in t.throws() expectation
1 parent 16f4742 commit 179f26a

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

lib/assert.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,22 @@ function wrapAssertions(callbacks) {
211211
return;
212212
}
213213

214+
if (hasOwnProperty(expected, 'code') && typeof expected.code !== 'string') {
215+
fail(this, new AssertionError({
216+
assertion: 'throws',
217+
message: 'The `code` property of the second argument to `t.throws()` must be a string',
218+
values: [formatWithLabel('Called with:', expected)]
219+
}));
220+
return;
221+
}
222+
214223
for (const key of Object.keys(expected)) {
215224
switch (key) {
216225
case 'instanceOf':
217226
case 'is':
218227
case 'message':
219228
case 'name':
229+
case 'code':
220230
continue;
221231
default:
222232
fail(this, new AssertionError({
@@ -300,6 +310,18 @@ function wrapAssertions(callbacks) {
300310
]
301311
});
302312
}
313+
314+
if (typeof expected.code === 'string' && actual.code !== expected.code) {
315+
throw new AssertionError({
316+
assertion: 'throws',
317+
message,
318+
stack,
319+
values: [
320+
formatWithLabel(`${prefix} unexpected exception:`, actual),
321+
formatWithLabel('Expected code to equal:', expected.code)
322+
]
323+
});
324+
}
303325
};
304326

305327
const handleObservable = (observable, wasReturned) => {

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,7 @@ The thrown value *must* be an error. It is returned so you can run more assertio
956956
* `is`: the thrown error must be strictly equal to `expected.is`
957957
* `message`: either a string, which is compared against the thrown error's message, or a regular expression, which is matched against this message
958958
* `name`: the expected `.name` value of the thrown error
959+
* `code`: the expected `.code` value of the thrown error
959960

960961
`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`.
961962

test/assert.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,24 @@ test('.throws()', gather(t => {
819819
}, {name: 'TypeError'});
820820
});
821821

822+
// Passes because the correct error is thrown.
823+
passes(t, () => {
824+
assertions.throws(() => {
825+
const err = new TypeError(); // eslint-disable-line unicorn/error-message
826+
err.code = 'ERR_TEST';
827+
throw err;
828+
}, {code: 'ERR_TEST'});
829+
});
830+
831+
// Fails because the thrown value is not the right one
832+
fails(t, () => {
833+
assertions.throws(() => {
834+
const err = new TypeError(); // eslint-disable-line unicorn/error-message
835+
err.code = 'ERR_NOPE';
836+
throw err;
837+
}, {code: 'ERR_TEST'});
838+
});
839+
822840
// Fails because the promise is resolved, not rejected.
823841
eventuallyFailsWith(t, () => assertions.throws(Promise.resolve('foo')), {
824842
assertion: 'throws',
@@ -982,6 +1000,14 @@ test('.throws() fails if passed a bad expectation', t => {
9821000
values: [{label: 'Called with:', formatted: /\[\]/}]
9831001
});
9841002

1003+
failsWith(t, () => {
1004+
assertions.throws(() => {}, {code: 42});
1005+
}, {
1006+
assertion: 'throws',
1007+
message: 'The `code` property of the second argument to `t.throws()` must be a string',
1008+
values: [{label: 'Called with:', formatted: /code: 42/}]
1009+
});
1010+
9851011
failsWith(t, () => {
9861012
assertions.throws(() => {}, {instanceOf: null});
9871013
}, {

0 commit comments

Comments
 (0)