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

unroll

v1.6.0

Published

A helper tool to easily iterate through test data against a test method with verbose output about each iteration.

Downloads

9,963

Readme

Unroll Build StatusTest Coverage

A helper tool (for browser and node tests) to easily iterate through test data against a single test method with output about each test iteration and its parameters. Or in other words a helper method to parameterize your tests.

It is an attempt to provide similar behaviour to the [Unroll annotation] (https://spockframework.github.io/spock/docs/1.0/data_driven_testing.html#_method_unrolling) from Spock.

Unroll works by decorating the testing library function so it works with any testing library e.g Jasmine, Mocha, Tape and AVA. The examples directory has working examples of each. See below for instructions on how to run them.

Install

$> npm install unroll

To run the unit tests or the examples:

$> npm install

npm run commands expects node_modules/.bin/ to be in $PATH.

Usage

Include unroll in your test file and configure.

var unroll = require('unroll');
unroll.use(it); // specify test library function here.

Instead of calling the testing library function e.g it or test, call unroll with three arguments:

  • name of the test with parameterized names
  • test function
  • data table of parameters to pass to tests.

Note the use of # character to prefix parameter name and the additional argument testArgs from which to reference the arguments.

  ```
describe('maximum of two numbers (unrolled)', function() {
    unroll('maximum of #a and #b is #c',
      function(done, testArgs) {
        expect(
          Math.max(testArgs['a'], testArgs['b'])
        ).to.be.equal(testArgs['c']);
        done();
      },
      [
        ['a', 'b', 'c'],
        [ 3,   5,   5 ],
        [ 7,   0,   7 ]
      ]
    );
});

The data table can also be specified using a template literal if nested arrays aren't your thing. For example the above can be written like so when using template literals.

```
describe('maximum of two numbers (unrolled)', function() {
    unroll('maximum of #a and #b is #c',
      function(done, testArgs) {
        expect(
          Math.max(testArgs['a'], testArgs['b'])
        ).to.be.equal(testArgs['c']);
        done();
      },
      `
        where:
        a   |   b   |   c
        3   |   5   |   5
        7   |   0   |   7
      `
    );
});

Note, that objects and arrays need to be stringified first:

```
unroll('The #thing was jumped over by #entity.',
    () => {},
    `
      where:
      entity                            |   thing
      cat                               |   moon
      1                                 |   2
      ${JSON.stringify({name: 'cat'})}  |   ${JSON.stringify({name: 'moon'})}
    `
  );

Examples

The following example is the same shown in examples/mocha-bdd-example.js file. It can be run using Mocha eg:

mocha -R spec ./mocha-bdd-example.js

Using a similar example from the above spock unroll documentation, a simple test of testing maximum of two numbers eg:

describe('maximum of two numbers', function() {

  it('is performed correctly', function(done) {
    expect(Math.max(3, 5)).to.be.equal(5);
    expect(Math.max(7, 0)).to.be.equal(7);
    done();
  });

});

The test output would look like the following:

  maximum of two numbers
    ✓ is performed correctly

  ✔ 1 test complete (4ms)

whilst a failing test would look like:

  maximum of two numbers
    1) is performed correctly


  ✖ 1 of 1 test failed:

  1) maximum of two numbers is performed correctly:
     expected 7 to equal 0

But using unroll(), like so:

  unroll.use(it);
  describe('maximum of two numbers (unrolled)', function() {
    unroll('maximum of #a and #b is #c',
      function(done, testArgs) {
        expect(
          Math.max(testArgs['a'], testArgs['b'])
        ).to.be.equal(testArgs['c']);
        done();
      },
      [
        ['a', 'b', 'c'],
        [ 3,   5,   5 ],
        [ 7,   0,   7 ]
      ]
    );
});

would give an unrolled test output like:

  maximum of two numbers(unrolled)
    - maximum of 3 and 5 is 5
    - maximum of 7 and 0 is 7

  ✔ 2 tests complete (6ms)

and a failing test would show the following:

  maximum of two numbers (unrolled)
    ✓ maximum of 3 and 5 is 5
    1) maximum of 7 and 0 is 0


  ✖ 1 of 2 tests failed:

  1) maximum of two numbers (unrolled) maximum of 7 and 0 is 0:
     expected 7 to equal 0

Another way of using unroll can be

  unroll.use(it);
  describe('maximum of two numbers (unrolled)', function() {
    unroll('calculates the maximum of #b and #a',
      function(done, testArgs) {
        expect(
          Math.max(testArgs['a'], testArgs['b'])
        ).to.be.equal(testArgs['c']);
        done();
      },
      [
        ['a', 'b', 'c'],
        [ 3,   5,   5 ],
        [ 7,   0,   7 ]
      ]
    );
});

Here the title parameters are out of sequence with the sequence of the testArgs passed. The output for this type of usage would be something like:

  maximum of two numbers
    - calculates the maximum of 5 and 3
    - calculates the maximum of 0 and 7

  ✔ 2 tests complete (6ms)

and a failing test would show the following:

  maximum of two numbers (unrolled)
    ✓ calculates the maximum of 5 and 3
    1) calculates the maximum of 0 and 7


  ✖ 1 of 2 tests failed:

  1) maximum of two numbers (unrolled) calculates the maximum of 0 and 7 is 0:
     expected 7 to equal 0

The examples directory has examples for various testing frameworks. There are npm run commands to run each example or one can run all the examples with:

$> npm run examples

Mocha

Mocha's allows one use different interfaces e.g tdd, bdd and qunit. Run all the examples for each with:

$> npm run example-mocha

AVA

$> npm run example-ava

Tape

$> npm run example-tape

Jasmine

$> npm run example-jasmine

Tests

Tests can be run, from the project root directory, via:

$> npm test

Browser tests can be run via karma (install the dev dependencies first):

$> npm run test-browser

A coverage report can be generated in target/lcov-report/index.html via:

$> npm run coverage

Lint

Linting can be tested with:

$> npm run lint