Skip to content

Commit

Permalink
Invalidates rejection with wrong type or message arguments (#121)
Browse files Browse the repository at this point in the history
* invalidates rejection with wrong message type

* invalidates rejection with wrong type
  • Loading branch information
ferrao authored and cjihrig committed Jan 2, 2018
1 parent 18cd84c commit 6bf9d1d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/index.js
Expand Up @@ -420,6 +420,10 @@ internals.reject = async function (/* type, message */) {
internals.assert(this, !this._flags.not || !arguments.length, 'Cannot specify arguments when expecting not to reject');

if (thrown) {

internals.assert(this, arguments.length < 2 || message, 'Can not assert with invalid message argument type');
internals.assert(this, arguments.length < 1 || message !== null || typeof type === 'function', 'Can not assert with invalid type argument');

if (type) {
this.assert(thrown instanceof type, 'reject with ' + (type.name || 'provided type'));
}
Expand Down
61 changes: 61 additions & 0 deletions test/index.js
Expand Up @@ -2436,6 +2436,67 @@ describe('expect()', () => {
Hoek.assert(/Expected \[Promise\] to reject with provided type/.test(exception.message), exception);
});

it('invalidates rejection (invalid type)', async () => {

const promise = Promise.reject(new Error('kaboom'));

const fail = async (value) => {

let exception = false;

try {
await Code.expect(promise).to.reject(value);
}
catch (err) {
exception = err;
}

Hoek.assert(exception.message === 'Can not assert with invalid type argument', exception);
};

await fail(0);
await fail(1);
await fail(Infinity);
await fail(undefined);
await fail(null);
await fail(true);
await fail(false);
await fail({});
await fail([]);
await fail(NaN);
});

it('invalidates rejection (invalid message type)', async () => {

const promise = Promise.reject(new Error('kaboom'));

const fail = async (value) => {

let exception = false;

try {

await Code.expect(promise).to.reject(Error, value);
}
catch (err) {
exception = err;
}

Hoek.assert(exception.message === 'Can not assert with invalid message argument type', exception);
};

await fail(1);
await fail(0);
await fail(Infinity);
await fail(undefined);
await fail(null);
await fail(true);
await fail(false);
await fail({});
await fail([]);
await fail(NaN);
});

it('validates rejection (type and message)', async () => {

let exception = false;
Expand Down

0 comments on commit 6bf9d1d

Please sign in to comment.