Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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