Skip to content

Commit

Permalink
Allow code in throws expectations to be a number
Browse files Browse the repository at this point in the history
Fixes #1901.
  • Loading branch information
oantoro authored and novemberborn committed Aug 14, 2018
1 parent 99098cc commit 7f974cc
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
4 changes: 2 additions & 2 deletions index.d.ts
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions index.js.flow
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions lib/assert.js
Expand Up @@ -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)]
});
}
Expand Down
21 changes: 15 additions & 6 deletions test/assert.js
Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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, () => {
Expand Down Expand Up @@ -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, () => {
Expand Down

0 comments on commit 7f974cc

Please sign in to comment.