diff --git a/index.d.ts b/index.d.ts index 504b22109..c209f59b2 100644 --- a/index.d.ts +++ b/index.d.ts @@ -6,8 +6,8 @@ export type Constructor = (new (...args: Array) => 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; diff --git a/index.js.flow b/index.js.flow index c85569f1d..5d281968b 100644 --- a/index.js.flow +++ b/index.js.flow @@ -19,8 +19,8 @@ export type Constructor = Class<{constructor(...args: Array): 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; diff --git a/lib/assert.js b/lib/assert.js index 569ec9459..cae0f53b6 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -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)] }); } diff --git a/test/assert.js b/test/assert.js index 37220a176..3f0b74a17 100644 --- a/test/assert.js +++ b/test/assert.js @@ -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, () => {