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 🙏

© 2025 – Pkg Stats / Ryan Hefner

mocha-promise-me

v1.0.3

Published

Makes mocha tests easier when you want to assert that a promise will reject or resolve

Readme

mocha-promise-me

Build Status Coverage Status

mocha-promise-me is a little helper to make mocha tests easier, when you want to:

  • assert that a promise fails (but check the exception with an assertion)
  • assert that a promise resolves (but run assertion(s) when it was resolved)

The problem with promise testing

When you write an async test, you have 2 options with mocha:

  1. Call the "done" callback function when the test is done
  2. Return a promise

When you write an async test that asserts that a promise rejects, you can't use the "done" callback, because you can't run "done()" after your exception. You also can't just throw an exception if the test fails. This will lead to an unresolved promise.

For example:

it('should test that a promise rejects', () =>{
  let promise = new Promise((resolve, reject) => resolve());
  
  promise
    .then(() => {
      throw Error('Promise should reject. Instead it resolved')
    })
});

Will output:

(node:2892) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Promise should reject. Instead it resolved
(node:2892) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

In this case you have to wrap this into a new promise and return it:

it('should test that a promise rejects', () =>{
  let promise = new Promise((resolve, reject) => resolve());
  
  return new Promise((resolve, reject) => {
    promise
      .then(() => {
        reject(Error('Promise should reject. Instead it resolved'))
      })
      .catch((error) => resolve())
  });
});

Now mocha will fail this test and there will be no unhandled promise rejection warning:

  1) should test that a promise rejects

  0 passing (7ms)
  1 failing

  1)  should test that a promise rejects:
     Error: Promise should reject. Instead it resolved
     ...

While the solution above works, it will unnecessarily bloat up your test code and makes it unreadable. The more complex your test, the more unreadable it will get. For example: What happens when you want to not only assert that a promise rejects, but also want to assert that a certain message was thrown?

const assert = require('assert');

it('should test that a promise rejects', () =>{
  let promise = new Promise((resolve, reject) => reject(Error('Promise failed')));
  
  return new Promise((resolve, reject) => {
    promise
      .then(() => {
        reject(Error('Promise should reject. Instead it resolved'))
      })
      .catch((error) => {
        try {
          assert.equal('Promise failed', error.message);
          resolve();
        } catch(e) {
          reject(e);
        }
      })
  });
});

Well, that's not really readable, is it? And that's exactly where this module will come in handy :)

The solution

The next time you have to write an async test that asserts that a promise rejects, you can just use this module. Thus your code stays readable and you don't have to hassle with wrapping promises.

Instead of writing something like this:

it('should test that a promise rejects', () =>{
  let promise = new Promise((resolve, reject) => resolve());
  
  return new Promise((resolve, reject) => {
    promise
      .then(() => {
        reject(Error('Promise should reject. Instead it resolved'))
      })
      .catch((error) => resolve())
  });
});

You can now do it this way:

const promiseMe = require('mocha-promise-me');
it('should test that a promise rejects', () =>{
  let promise = new Promise((resolve, reject) => resolve());
  
  return promiseMe.thatYouReject(promise);
});

... and when you need to assert that a promise rejects with a certain message, you no longer have to do this:

const assert = require('assert');

it('should test that a promise rejects', () =>{
  let promise = new Promise((resolve, reject) => reject(Error('Promise failed')));
  
  return new Promise((resolve, reject) => {
    promise
      .then(() => {
        reject(Error('Promise should reject. Instead it resolved'))
      })
      .catch((error) => {
        try {
          assert.equal('Promise failed', error.message);
          resolve();
        } catch(e) {
          reject(e);
        }
      })
  });
});

Instead you can simply write it like this now:

const assert = require('assert');
const promiseMe = require('mocha-promise-me');

it('should test that a promise rejects', () =>{
  let promise = new Promise((resolve, reject) => reject(Error('Promise failed')));
  let assertion = (error) => assert.equal('Promise failed', error.message);
  return promiseMe.thatYouReject(promise, assertion);
});

Test promises that should resolve

You can also test, that a promise resolves with some additional assertions.

For Example:

const assert = require('assert');
const promiseMe = require('mocha-promise-me');

it('should successfully return some data from the api', () => {
  let someApi = {
    getData: () => {
      return new Promise((resolve,reject) => {
        setTimeout(()=> resolve({
          success: true,
          data: []
        }), 10);
      });
    }
  }
  let assertion = (result) => {
    assert.ok(result);
    assert.equal(true, result.success);
  };
  return promiseMe.thatYouResolve(someApi.getData(), assertion);
});

Btw: You don't have to provide an assertion function. Same as with promiseMe.thatYouReject :)