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

mocha-qa

v0.9.3

Published

A convenience helper for testing promises using the mocha library

Downloads

25

Readme

mocha-qa

A convenience helper for testing promises using the mochaja library

Adds a wrapper function to the mochajs test-runner that will make it easier to handle promise-testing using mochajs.

mocha-qa will automatically pass a test if a returned promise gets resolved and fail it if the promise gets rejected. In cases no promise gets returned, mocha-qa will fallback to the default mocha-style tests concluding the test once all assertions have passed.

Also will proberly report any failed assertions during the test and report them just they way they would be reported running a sequential mocha-test.

Supported methods:

Usage

Example

To test a promise use any of the mocha-qa style it-functions and return a promise. Whenever the promise is resolved the mochajs done() callback will be executed:

it = require('mocha-qa').it;

describe('My test', function () {
  it('will pass on a resolved promise', function () {
    return myPromise
      .then(function () {
        expect(1).to.equal(1);
      });
  });
});

Global namespace

If you'd like to replace all default mocha runners and hooks with the mocha-qa variants you can use the global() registration method that will add the promise-enabled it, catchIt, before, beforeEach, after and afterEach functions to the global namespace

require('mocha-qa').global();

describe('My test', function () {
  before(function setUp () {
    return asyncSetupFunction()
      .then(function(data) {
        prepareTest();
      });
  });

  after(function tearDown () {
    return asyncCleanupFunction2();
  });


  it('will pass on a resolved promise', function () {
    return myPromise
      .then(function () {
        expect(1).to.equal(1);
      });
  });
});

Mixing mocha-qa with default mocha

You can mix default-mocha and mocha-qa style tests using the same methods. In cases no promise gets returned the default mocha test-handling is applied.

require('mocha-qa').global();

describe('My test', function () {

  it('will pass on a resolved promise', function () {
    return myPromise
      .then(function () {
        expect(1).to.equal(1);
      });
  });

  it('will pass on a test ', function () {
    var result = doSomething();
    expect(result.value).to.equal(1);
    // Test passed
  });
});

You are also still able to to use mocha ´done´ resolver method explicitely, just like you would with the default mocha test-cases. E.g.,

require('mocha-qa').global();

describe('My test', function () {

  it('will pass on when calling done ', function (done) {
    nodestyle.asyncMethod(function (error, result) {
      // Test failed
      if (error) {
        done(error);
      }

      // Test passed
      done();
    });
  });
});

Methods

it(description, fnc)

The default test runner. Runs the test function, passing it when the returned promise gets resolved. If no promise is returned will fall back to the default it behaviour, i.e., once the function execution is completed.

it = require('mocha-qa').it;

describe('My test', function () {
  it('will pass on a resolved promise', function () {
    return myPromise
      .then(function () {
        expect(1).to.equal(1);
      });
  });
});

catchIt(description, fnc)

The rejection variant of the it-test-runner. Will fail the test on a resolved promise and pass the test, once the promise returned by the test-runner gets rejected.

describe('My test', function () {
  catchIt('will pass on a rejected promise', function () {
    var promise = Promise.defer();

    doSomethingAsync()
      .then(function (result) {
        if (result === 42) {
          deferred.reject('This causes the test to pass');
        }
        deferred.resolve();
      });

    return deferred;
  });
});

Hooks

Returning a promise on any of the set-up-/tear-down-hooks will finalize the hook successfully once all promises of the hook are resolved and cause an exception if the promise gets rejected.

Supported hooks are:

  • before(fnc)
  • beforeEach(fnc)
  • after(fnc)
  • afterEach(fnc)
before = require('mocha-qa').before;

describe('My test', function () {
  before(function () {
    return myDb.find()
      .then(function () {
        populateTestSet();
      });
  });
});