Permalink
Browse files

Allow `code` in throws expectations to be a number

Fixes #1901.
  • Loading branch information...
okyantoro authored and novemberborn committed Aug 14, 2018
1 parent 99098cc commit 7f974ccd1f2320eaf010db917f17b09922342b86
Showing with 21 additions and 12 deletions.
  1. +2 −2 index.d.ts
  2. +2 −2 index.js.flow
  3. +2 −2 lib/assert.js
  4. +15 −6 test/assert.js
View
@@ -6,8 +6,8 @@ export type Constructor = (new (...args: Array<any>) => any);
/** Specify one or more expectations the thrown error must satisfy. */
export type ThrowsExpectation = {
/** The thrown error must have a code that equals the given string. */
code?: string;
/** The thrown error must have a code that equals the given string or number. */
code?: string | number;
/** The thrown error must be an instance of this constructor. */
instanceOf?: Constructor;
View
@@ -19,8 +19,8 @@ export type Constructor = Class<{constructor(...args: Array<any>): any}>;
/** Specify one or more expectations the thrown error must satisfy. */
export type ThrowsExpectation = {
/** The thrown error must have a code that equals the given string. */
code?: string;
/** The thrown error must have a code that equals the given string or number. */
code?: string | number;
/** The thrown error must be an instance of this constructor. */
instanceOf?: Constructor;
View
@@ -106,10 +106,10 @@ function validateExpectations(assertion, expectations, numArgs) { // eslint-disa
});
}
if (hasOwnProperty(expectations, 'code') && typeof expectations.code !== 'string') {
if (hasOwnProperty(expectations, 'code') && typeof expectations.code !== 'string' && typeof expectations.code !== 'number') {
throw new AssertionError({
assertion,
message: `The \`code\` property of the second argument to \`t.${assertion}()\` must be a string`,
message: `The \`code\` property of the second argument to \`t.${assertion}()\` must be a string or number`,
values: [formatWithLabel('Called with:', expectations)]
});
}
View
@@ -827,6 +827,15 @@ test('.throws()', gather(t => {
}, {code: 'ERR_TEST'});
});
// Passes because the correct error is thrown.
passes(t, () => {
assertions.throws(() => {
const err = new TypeError(); // eslint-disable-line unicorn/error-message
err.code = 42;
throw err;
}, {code: 42});
});
// Fails because the thrown value is not the right one
fails(t, () => {
assertions.throws(() => {
@@ -980,11 +989,11 @@ test('.throws() fails if passed a bad expectation', t => {
});
failsWith(t, () => {
assertions.throws(() => {}, {code: 42});
assertions.throws(() => {}, {code: {}});
}, {
assertion: 'throws',
message: 'The `code` property of the second argument to `t.throws()` must be a string',
values: [{label: 'Called with:', formatted: /code: 42/}]
message: 'The `code` property of the second argument to `t.throws()` must be a string or number',
values: [{label: 'Called with:', formatted: /code: {}/}]
});
failsWith(t, () => {
@@ -1048,11 +1057,11 @@ test('.throwsAsync() fails if passed a bad expectation', t => {
});
failsWith(t, () => {
assertions.throwsAsync(() => {}, {code: 42});
assertions.throwsAsync(() => {}, {code: {}});
}, {
assertion: 'throwsAsync',
message: 'The `code` property of the second argument to `t.throwsAsync()` must be a string',
values: [{label: 'Called with:', formatted: /code: 42/}]
message: 'The `code` property of the second argument to `t.throwsAsync()` must be a string or number',
values: [{label: 'Called with:', formatted: /code: {}/}]
});
failsWith(t, () => {

0 comments on commit 7f974cc

Please sign in to comment.