npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rejected-or-not

v2.0.0

Published

Promise-based implementation of Node v10's assert.rejects() and assert.doesNotReject() for old Nodes and browsers

Downloads

36

Readme

rejected-or-not

Promise-based implementation of Node v10's assert.rejects() and assert.doesNotReject() for old Nodes and browsers.

Build Status NPM version Code Style License

Issues and improvements should be done in Node.js first.

INSTALL

npm install rejected-or-not

API

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 promiseFn is a <Promise>, awaits the promise then check that the promise is rejected.
      • rejects with AssertionError if the promiseFn is not rejected.
      • resolves if the promiseFn is rejected.
    • if promiseFn is 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 promiseFn function is NOT rejected.
      • resolves if result of promiseFn function is rejected.
      • if promiseFn is a function and it throws an error synchronously, rejects() will return a rejected Promise with that error.
      • if the promiseFn function does not return a promise, rejects() will return a rejected Promise with ERR_INVALID_RETURN_VALUE TypeError.
    • if type of promiseFn is other than <Promise> or <Function>, rejects() will return a rejected Promise with ERR_INVALID_ARG_TYPE TypeError.
  • error <RegExp> | <Class> | <Function> | <Object> | <Error>
    • if 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.
      • when message matches, resolves with undefined.
      • when messages does not match, rejects with the actual error.
    • if error is 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.name as expected error class name to the message if the promiseFn is not rejected.
    • if error is 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.
    • if error is an <Object>, that is an object where each property will be tested for.
      • when all key-value pairs in 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.
      • when some of the properties are not same, rejects with AssertionError.
      • when actual result does not have property that error have, rejects with AssertionError.
      • if exists, appends error.name as expected error class name to the message if the promiseFn is not rejected.
    • if 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.
      • 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 the error will be tested.
      • when some of the properties are not same, rejects with AssertionError.
      • appends error.name as expected error class name to the message if the promiseFn is not rejected.
    • note that error cannot be a string.
      • if a string is provided as the second argument,
        • and the third argument is not given, then error is assumed to be omitted and the string will be used for message instead. 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.
  • message <any>
    • if specified, message will be the message provided by the AssertionError if the promiseFn fails to reject.
    • when 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>
      • when error is an <Object> and comparison fails, rejects AssertionError with specified failure message
      • when error is an <Error> and comparison fails, rejects AssertionError with specified failure message

doesNotReject(promiseFn, [error], [message])

  • promiseFn <Function> | <Promise>
    • if promiseFn is a <Promise>, awaits the promise then check that the promise is NOT rejected.
      • rejects with AssertionError if the promiseFn is rejected.
      • resolves if the promiseFn is not rejected.
    • if 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.
      • rejects with AssertionError if the promise returned from promiseFn is rejected.
      • resolves if the promise returned from promiseFn is not rejected.
      • if promiseFn is 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 an ERR_INVALID_RETURN_VALUE TypeError.
    • if type of promiseFn is other than <Promise> or <Function>, doesNotReject() will return a rejected Promise with an ERR_INVALID_ARG_TYPE TypeError.
  • error <RegExp> | <Class> | <Function>
    • if 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.
      • when message matches, rejects with AssertionError.
      • when message does not match, rejects with actual error.
    • if error is 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.
    • if error is 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.
    • note that error cannot be a string.
      • if a string is provided as the second argument,
        • and the third argument is not given, then error is assumed to be omitted and the string will be used for message instead. This can lead to easy-to-miss mistakes.
        • and the third argument is also given, third argument is just ignored.
  • 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.