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-sprinkles

v2.2.1

Published

helpers for mocha, when using bluebird promises

Downloads

77

Readme

mocha-sprinkles

These are a few small helpers for mocha, that simplify your test boilerplate if you use bluebird.

Each function is intended to be a mixin for a unit test, so for example, if you have a test like this:

it("adds numbers", function () {
  (2 + 2).should.eql(4);
});

you can add each helper as a wrapper around the test function, like this:

it("adds numbers", function () {
  Promise.resolve(2 + 2).then(function (sum) {
    sum.should.eql(4);
  });
});

The helpers

future

Wrap a test function that returns a future:

  • future(function)
    • function: () => Promise code to execute as a test

Mocha's done hooks are attached to the future so that the test doesn't complete until the future is resolved.

it("waits 100 ms", future(function () {
  return Q.delay(100);
}));

withTempFolder

Wrap a test function so that it runs inside a temporary folder:

  • withTempFolder(function)
    • function: (folder) => Promise code to execute while the folder exists

The folder's name is passed as the first parameter to the function, and it's expected to return a Promise. When the Promise is resolved, the temporary folder is deleted (along with any contents).

This function requires future also, since the cleanup is attached to the result future.

it("creates a file", future(withTempFolder(function (folder) {
  fs.writeFileSync(folder + "/new-file", "hello!");
})));

exec

Execute a program as a future:

  • exec(command, options)
    • command: passed to child_process.exec
    • options: passed to child_process.exec

The parameters are passed to child_process.exec as-is and a future is returned. If the exec is successful, the future is resolved with an object with these fields:

  • process: the process object
  • stdout: the stdout buffer
  • stderr: the stderr buffer

If the exec fails, the future is rejected, and the error object will have those three fields added to it.

it("runs echo", future(function () {
  return exec("echo hello").then(function (result) {
    result.stdout.should.match(/hello/);
  });
}));

eventually

Try running assertions a few times, until they pass (or run out of time):

  • eventually(options, function)
    • options:
      • timeout: milliseconds to wait before giving up (default: 1000)
      • frequency: milliseconds to wait between attempts (default: 50)
    • function: code to execute on each attempt

To assert that a background process will have some effect in a non-deterministic (but relatively short) time, you can group your assertions in an eventually block. If an exception is thrown the first time -- for example, by a failing assertion -- it will delay for a small period of time, then try again, repeating until the code executes without exception or the time runs out.