Permalink
Browse files

Support err.code in t.throws() expectation

  • Loading branch information...
jamiebuilds authored and novemberborn committed May 30, 2018
1 parent 16f4742 commit 179f26ad1acbb56f10c18042f73903aec4af54e8
Showing with 49 additions and 0 deletions.
  1. +22 −0 lib/assert.js
  2. +1 −0 readme.md
  3. +26 −0 test/assert.js
View
@@ -211,12 +211,22 @@ function wrapAssertions(callbacks) {
return;
}
if (hasOwnProperty(expected, 'code') && typeof expected.code !== 'string') {
fail(this, new AssertionError({
assertion: 'throws',
message: 'The `code` property of the second argument to `t.throws()` must be a string',
values: [formatWithLabel('Called with:', expected)]
}));
return;
}
for (const key of Object.keys(expected)) {
switch (key) {
case 'instanceOf':
case 'is':
case 'message':
case 'name':
case 'code':
continue;
default:
fail(this, new AssertionError({
@@ -300,6 +310,18 @@ function wrapAssertions(callbacks) {
]
});
}
if (typeof expected.code === 'string' && actual.code !== expected.code) {
throw new AssertionError({
assertion: 'throws',
message,
stack,
values: [
formatWithLabel(`${prefix} unexpected exception:`, actual),
formatWithLabel('Expected code to equal:', expected.code)
]
});
}
};
const handleObservable = (observable, wasReturned) => {
View
@@ -956,6 +956,7 @@ The thrown value *must* be an error. It is returned so you can run more assertio
* `is`: the thrown error must be strictly equal to `expected.is`
* `message`: either a string, which is compared against the thrown error's message, or a regular expression, which is matched against this message
* `name`: the expected `.name` value of the thrown error
* `code`: the expected `.code` value of the thrown error
`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`.
View
@@ -819,6 +819,24 @@ test('.throws()', gather(t => {
}, {name: 'TypeError'});
});
// Passes because the correct error is thrown.
passes(t, () => {
assertions.throws(() => {
const err = new TypeError(); // eslint-disable-line unicorn/error-message
err.code = 'ERR_TEST';
throw err;
}, {code: 'ERR_TEST'});
});
// Fails because the thrown value is not the right one
fails(t, () => {
assertions.throws(() => {
const err = new TypeError(); // eslint-disable-line unicorn/error-message
err.code = 'ERR_NOPE';
throw err;
}, {code: 'ERR_TEST'});
});
// Fails because the promise is resolved, not rejected.
eventuallyFailsWith(t, () => assertions.throws(Promise.resolve('foo')), {
assertion: 'throws',
@@ -982,6 +1000,14 @@ test('.throws() fails if passed a bad expectation', t => {
values: [{label: 'Called with:', formatted: /\[\]/}]
});
failsWith(t, () => {
assertions.throws(() => {}, {code: 42});
}, {
assertion: 'throws',
message: 'The `code` property of the second argument to `t.throws()` must be a string',
values: [{label: 'Called with:', formatted: /code: 42/}]
});
failsWith(t, () => {
assertions.throws(() => {}, {instanceOf: null});
}, {

0 comments on commit 179f26a

Please sign in to comment.