Skip to content

Commit 6bf9d1d

Browse files
ferraocjihrig
authored andcommittedJan 2, 2018
Invalidates rejection with wrong type or message arguments (#121)
* invalidates rejection with wrong message type * invalidates rejection with wrong type
1 parent 18cd84c commit 6bf9d1d

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed
 

‎lib/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,10 @@ internals.reject = async function (/* type, message */) {
420420
internals.assert(this, !this._flags.not || !arguments.length, 'Cannot specify arguments when expecting not to reject');
421421

422422
if (thrown) {
423+
424+
internals.assert(this, arguments.length < 2 || message, 'Can not assert with invalid message argument type');
425+
internals.assert(this, arguments.length < 1 || message !== null || typeof type === 'function', 'Can not assert with invalid type argument');
426+
423427
if (type) {
424428
this.assert(thrown instanceof type, 'reject with ' + (type.name || 'provided type'));
425429
}

‎test/index.js

+61
Original file line numberDiff line numberDiff line change
@@ -2436,6 +2436,67 @@ describe('expect()', () => {
24362436
Hoek.assert(/Expected \[Promise\] to reject with provided type/.test(exception.message), exception);
24372437
});
24382438

2439+
it('invalidates rejection (invalid type)', async () => {
2440+
2441+
const promise = Promise.reject(new Error('kaboom'));
2442+
2443+
const fail = async (value) => {
2444+
2445+
let exception = false;
2446+
2447+
try {
2448+
await Code.expect(promise).to.reject(value);
2449+
}
2450+
catch (err) {
2451+
exception = err;
2452+
}
2453+
2454+
Hoek.assert(exception.message === 'Can not assert with invalid type argument', exception);
2455+
};
2456+
2457+
await fail(0);
2458+
await fail(1);
2459+
await fail(Infinity);
2460+
await fail(undefined);
2461+
await fail(null);
2462+
await fail(true);
2463+
await fail(false);
2464+
await fail({});
2465+
await fail([]);
2466+
await fail(NaN);
2467+
});
2468+
2469+
it('invalidates rejection (invalid message type)', async () => {
2470+
2471+
const promise = Promise.reject(new Error('kaboom'));
2472+
2473+
const fail = async (value) => {
2474+
2475+
let exception = false;
2476+
2477+
try {
2478+
2479+
await Code.expect(promise).to.reject(Error, value);
2480+
}
2481+
catch (err) {
2482+
exception = err;
2483+
}
2484+
2485+
Hoek.assert(exception.message === 'Can not assert with invalid message argument type', exception);
2486+
};
2487+
2488+
await fail(1);
2489+
await fail(0);
2490+
await fail(Infinity);
2491+
await fail(undefined);
2492+
await fail(null);
2493+
await fail(true);
2494+
await fail(false);
2495+
await fail({});
2496+
await fail([]);
2497+
await fail(NaN);
2498+
});
2499+
24392500
it('validates rejection (type and message)', async () => {
24402501

24412502
let exception = false;

0 commit comments

Comments
 (0)
Please sign in to comment.