Promise-based implementation of Node v10's `assert.rejects()` and `assert.doesNotReject()` for old Nodes and browsers
MIT License
Promise-based implementation of Node v10's assert.rejects()
and assert.doesNotReject()
for old Nodes and browsers.
Issues and improvements should be done in Node.js first.
npm install rejected-or-not
const assert = require('assert');
const {rejects, doesNotReject} = require('rejected-or-not');
const funcToBeResolved = () => {
return new Promise((resolve, reject) => {
setTimeout(() => resolve('Resolved!'), 10);
});
};
const promiseToBeRejected = new Promise((resolve, reject) => {
setTimeout(() => {
const te = new TypeError('Invalid arg type');
te.code = 'ERR_INVALID_ARG_TYPE';
return reject(te);
}, 10);
});
(async () => {
await rejects(funcToBeResolved).catch((err) => {
assert(err instanceof assert.AssertionError);
assert(err.message === 'Missing expected rejection.');
});
await rejects(promiseToBeRejected).then(() => {
// resolves when rejected
});
})();
rejects(promiseFn, [error], [message])
<Function> | <Promise>
promiseFn
is a <Promise>
, awaits the promise then check that the promise is rejected.
promiseFn
is not rejected.promiseFn
is rejected.promiseFn
is a <Function>
, immediately calls the function and awaits the returned promise to complete. It will then check that the promise is rejected.
promiseFn
function is NOT rejected.promiseFn
function is rejected.promiseFn
is a function and it throws an error synchronously, rejects()
will return a rejected Promise with that error.promiseFn
function does not return a promise, rejects()
will return a rejected Promise with ERR_INVALID_RETURN_VALUE
TypeError.promiseFn
is other than <Promise>
or <Function>
, rejects()
will return a rejected Promise with ERR_INVALID_ARG_TYPE
TypeError.<RegExp> | <Class> | <Function> | <Object> | <Error>
error
is a <RegExp>
, validate rejected actual error message using RegExp. Using a regular expression runs .toString()
on the actual error object, and will therefore also include the error name.
error
is a <Class>
(constructor function), validate instanceof using constructor (works well with ES2015 classes that extends Error).
<Class>
, resolves with undefined.<Class>
, rejects with AssertionError.error.name
as expected error class name to the message if the promiseFn
is not rejected.error
is a <Function>
, run custom validation against actual rejection result.
true
, resolves with undefined.true
, rejects with AssertionError.error
is an <Object>
, that is an object where each property will be tested for.
error
are the same as key-value pairs from actual rejected result, resolves with undefined. Note that only properties on the error object will be tested.error
have, rejects with AssertionError.error.name
as expected error class name to the message if the promiseFn
is not rejected.error
is an <Error>
, that is an instance of error where each property will be tested for, including the non-enumerable message and name properties.
error
(error instance in this case) are the same as actual error, resolves with undefined. Note that only properties on the error
will be tested.error.name
as expected error class name to the message if the promiseFn
is not rejected.error
cannot be a string.
error
is assumed to be omitted and the string will be used for message
instead. This can lead to easy-to-miss mistakes.ERR_INVALID_ARG_TYPE
.ERR_AMBIGUOUS_ARGUMENT
.ERR_AMBIGUOUS_ARGUMENT
.<any>
message
will be the message provided by the AssertionError if the promiseFn
fails to reject.error
is one of <Class>
, <Error>
or <Object>
with name
property, append it as expected error class name to the assertion message.message
argument is also used with error
of type <Object>
or <Error>
error
is an <Object>
and comparison fails, rejects AssertionError with specified failure messageerror
is an <Error>
and comparison fails, rejects AssertionError with specified failure messagedoesNotReject(promiseFn, [error], [message])
<Function> | <Promise>
promiseFn
is a <Promise>
, awaits the promise then check that the promise is NOT rejected.
promiseFn
is rejected.promiseFn
is not rejected.promiseFn
is a <Function>
, immediately calls the function and awaits the returned promise to complete. It will then check that the promise is NOT rejected.
promiseFn
is rejected.promiseFn
is not rejected.promiseFn
is a function and it throws an error synchronously, doesNotReject()
will return a rejected Promise with that error.doesNotReject()
will return a rejected Promise with an ERR_INVALID_RETURN_VALUE
TypeError.promiseFn
is other than <Promise>
or <Function>
, doesNotReject()
will return a rejected Promise with an ERR_INVALID_ARG_TYPE
TypeError.<RegExp> | <Class> | <Function>
error
is a <RegExp>
, validate rejected error message using RegExp. Using a regular expression runs .toString()
on the error object, and will therefore also include the error name.
error
is a <Class>
(constructor function), validate instanceof using constructor (works well with ES2015 classes that extends Error).
<Class>
, rejects with AssertionError.<Class>
, rejects with the actual error.error
is a <Function>
, run custom validation against rejection result.
true
, rejects with AssertionError.true
, rejects with the actual error.error
cannot be a string.
error
is assumed to be omitted and the string will be used for message
instead. This can lead to easy-to-miss mistakes.<any>
Licensed under the MIT license.