2.0.0 • Published 6 years ago
rejected-or-not v2.0.0
rejected-or-not
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.
INSTALL
npm install rejected-or-notAPI
USAGE
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
});
})();SPEC
rejects(promiseFn, [error], [message])
- promiseFn
<Function> | <Promise>- if
promiseFnis a<Promise>, awaits the promise then check that the promise is rejected.- rejects with AssertionError if the
promiseFnis not rejected. - resolves if the
promiseFnis rejected.
- rejects with AssertionError if the
- if
promiseFnis a<Function>, immediately calls the function and awaits the returned promise to complete. It will then check that the promise is rejected.- rejects with AssertionError if result of
promiseFnfunction is NOT rejected. - resolves if result of
promiseFnfunction is rejected. - if
promiseFnis a function and it throws an error synchronously,rejects()will return a rejected Promise with that error. - if the
promiseFnfunction does not return a promise,rejects()will return a rejected Promise withERR_INVALID_RETURN_VALUETypeError.
- rejects with AssertionError if result of
- if type of
promiseFnis other than<Promise>or<Function>,rejects()will return a rejected Promise withERR_INVALID_ARG_TYPETypeError.
- if
- error
<RegExp> | <Class> | <Function> | <Object> | <Error>- if
erroris 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.- when message matches, resolves with undefined.
- when messages does not match, rejects with the actual error.
- if
erroris a<Class>(constructor function), validate instanceof using constructor (works well with ES2015 classes that extends Error).- when actual error is an instanceof
<Class>, resolves with undefined. - when actual error is NOT an instanceof
<Class>, rejects with AssertionError. - appends
error.nameas expected error class name to the message if thepromiseFnis not rejected.
- when actual error is an instanceof
- if
erroris a<Function>, run custom validation against actual rejection result.- when validation function returns
true, resolves with undefined. - when returned value of validation function is NOT
true, rejects with AssertionError. - if Error is thrown from validation function, rejects with the error.
- when validation function returns
- if
erroris an<Object>, that is an object where each property will be tested for.- when all key-value pairs in
errorare the same as key-value pairs from actual rejected result, resolves with undefined. Note that only properties on the error object will be tested. - when some of the properties are not same, rejects with AssertionError.
- when actual result does not have property that
errorhave, rejects with AssertionError. - if exists, appends
error.nameas expected error class name to the message if thepromiseFnis not rejected.
- when all key-value pairs in
- if
erroris an<Error>, that is an instance of error where each property will be tested for, including the non-enumerable message and name properties.- when all key-value pairs in
error(error instance in this case) are the same as actual error, resolves with undefined. Note that only properties on theerrorwill be tested. - when some of the properties are not same, rejects with AssertionError.
- appends
error.nameas expected error class name to the message if thepromiseFnis not rejected.
- when all key-value pairs in
- note that
errorcannot be a string.- if a string is provided as the second argument,
- and the third argument is not given, then
erroris assumed to be omitted and the string will be used formessageinstead. This can lead to easy-to-miss mistakes. - and the third argument is also given, reject TypeError with code
ERR_INVALID_ARG_TYPE. - and is identical to the message property of actual error, reject TypeError with code
ERR_AMBIGUOUS_ARGUMENT. - and is identical to the actual rejected object, reject TypeError with code
ERR_AMBIGUOUS_ARGUMENT.
- and the third argument is not given, then
- if a string is provided as the second argument,
- if
- message
<any>- if specified,
messagewill be the message provided by the AssertionError if thepromiseFnfails to reject. - when
erroris one of<Class>,<Error>or<Object>withnameproperty, append it as expected error class name to the assertion message. messageargument is also used witherrorof type<Object>or<Error>- when
erroris an<Object>and comparison fails, rejects AssertionError with specified failure message - when
erroris an<Error>and comparison fails, rejects AssertionError with specified failure message
- when
- if specified,
doesNotReject(promiseFn, [error], [message])
- promiseFn
<Function> | <Promise>- if
promiseFnis a<Promise>, awaits the promise then check that the promise is NOT rejected.- rejects with AssertionError if the
promiseFnis rejected. - resolves if the
promiseFnis not rejected.
- rejects with AssertionError if the
- if
promiseFnis a<Function>, immediately calls the function and awaits the returned promise to complete. It will then check that the promise is NOT rejected.- rejects with AssertionError if the promise returned from
promiseFnis rejected. - resolves if the promise returned from
promiseFnis not rejected. - if
promiseFnis a function and it throws an error synchronously,doesNotReject()will return a rejected Promise with that error. - if the function does not return a promise,
doesNotReject()will return a rejected Promise with anERR_INVALID_RETURN_VALUETypeError.
- rejects with AssertionError if the promise returned from
- if type of
promiseFnis other than<Promise>or<Function>,doesNotReject()will return a rejected Promise with anERR_INVALID_ARG_TYPETypeError.
- if
- error
<RegExp> | <Class> | <Function>- if
erroris 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.- when message matches, rejects with AssertionError.
- when message does not match, rejects with actual error.
- if
erroris a<Class>(constructor function), validate instanceof using constructor (works well with ES2015 classes that extends Error).- when rejected error is an instanceof
<Class>, rejects with AssertionError. - when rejected error is NOT an instanceof
<Class>, rejects with the actual error.
- when rejected error is an instanceof
- if
erroris a<Function>, run custom validation against rejection result.- when validation function returns
true, rejects with AssertionError. - when returned value of validation function is NOT
true, rejects with the actual error. - if Error is thrown from validation function, rejects with the error.
- when validation function returns
- note that
errorcannot be a string.- if a string is provided as the second argument,
- and the third argument is not given, then
erroris assumed to be omitted and the string will be used formessageinstead. This can lead to easy-to-miss mistakes. - and the third argument is also given, third argument is just ignored.
- and the third argument is not given, then
- if a string is provided as the second argument,
- if
- message
<any>- if an AssertionError is thrown and a value is provided for the message parameter, the value of message will be appended to the AssertionError message.
AUTHOR
LICENSE
Licensed under the MIT license.