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

stepperbox

v1.4.0

Published

Testing library for creating mock functions with sequential behavior

Downloads

10

Readme

Stepperbox

Build Status

npm install --save-dev stepperbox

StepperBox is a testing library for creating stub functions that invoke different callbacks at different steps. It works similarly to a Sinon spy, but is much simpler in its implementation and usage. This allows you to create much more explicit mock behavior without knowing any cryptic API.

Example

var stepperbox    = require('stepperbox');
var stepper = stepperbox();

stepper.add(function (input) {
  console.log('step 1', input);
});
stepper.add(function (input) {
  console.log('step 2', input);
});

stepper(5);
stepper(6);

// Outputs:
// "Step 1", 5
// "Step 2", 6

This can be further extended for testing by using Function.prototype.bind to create curried versions of the stepper for stubbing multiple pieces. The below example shows how you could use stepper with proxyquire to stub the query function on a mysql connection pool.

var mymodule = proxyquire('path/to/mymodule', {
  '../db/mysql': {
    query: stepper.as('mysql.query');
  }
});

stepper.add(function (method, query, data) {
  assert.equal(method, 'mysql.query');
  assert.equal(query, 'SELECT NOW()');
  assert.deepEqual(data, []);
});

Usage

stepperbox([steps])

Returns a stepper instance, a callable function with chainable methods defining the behavior of the function at each invocation. An array of callbacks may be provided to be used as the initial steps.

stepper([arg1, ...])

Calling the stepper function invokes each defined step as if it had been called directly, passing in all arguments received.

stepper.as('name')

Returns a standalone function (without the stepper methods) which will be bound with the provided name as the first argument received (see the mysql example above).

stepper.add(callback)

Appends a callback step to the end of the current chain

stepper.onCall(index, callback)

Inserts a callback step at the specified index (0 based) in the sequence.

stepper.onFirstCall(callback), stepper.onSecondCall(callback), stepper.onThirdCall(callback)

Syntactic sugar for onCall. Inserts a callback step at the first, second or third positions, respectively.

stepper.onDone(callback)

Defines a callback to be invoked after the last step. Can be called multiple times to add more callbacks. Is reset by stepper.reset(<truthy>)

stepper.reset()

Resets the stepper position to the first step.

stepper.reset(true)

Resets the stepper position, and removes all existing steps.

stepper.reset(<Array>)

Resets the stepper position, and overwrites all existing steps with the callbacks provided in the array.

stepper.getStep()

Returns the current step position as a zero based integer.

stepper.getCount()

Returns the total number of steps currently on the stepper