Git Product home page Git Product logo

mocha-param's Introduction

Mocha param

Parameterized tests for Mocha.

npm install --save-dev mocha-param

New in Version 2.0.0

  • The tested value can now be included in the description for each test.
  • Optionally use require('mocha-param') rather than require('mocha-param').itParam;

Basic Usage

Simply use 'itParam' instead of the standard mocha 'it' function and pass in an array of data.

var itParam = require('mocha-param');
// We have used chai as an assertion library but you can use any.
var expect = require('chai').expect;

// A Simple sync example taking an array as a parameter.
// 'value' is each value in the array
describe("basic mocha test with data", function () {
    itParam("test value is a number", [1, 2, 3], function (value) {
        expect(value).to.be.a('number');
    })
})

Result:

 basic mocha test with data
    ✓ test value is a number
    ✓ test value is a number
    ✓ test value is a number


  3 passing (25ms)

Add the values being tested into the test descriptions

To display the values being passed into your tests, use "${value}" as part of the test description.

describe("basic mocha test with data", function () {
    itParam("test value ${value} is a number", [1, 2, 3], function (value) {
        expect(value).to.be.a('number');
    })
})

Result:

 basic mocha test with data
    ✓ test value 1 is a number
    ✓ test value 2 is a number
    ✓ test value 3 is a number


  3 passing (25ms)

Async

Standard async mocha tests take a 'done' parameter which is called when execution is finished. mocha-param works the same.

// A Simple async example taking an array and calling done()
// 'value' each value in the array
// 'done' is the standard mocha done callback
describe("async mocha test with data", function () {
    itParam("test value ${value} is a number", [1, 2, 3], function (done, value) {
        expect(value).to.be.a('number');
        done();
    })
})

Result:

  async mocha test with data
    ✓ test value 1 is a number
    ✓ test value 2 is a number
    ✓ test value 3 is a number


  3 passing (17ms)

Array Objects

The array can contain anything that you like. Nested values to be displayed in the test description can be accessed with ${value.<property>}

var myData = [{ name: 'rob', age: 23 }, { name: 'sally', age: 29 }];

describe("test that person objects are older than 20", function () {
    itParam("test person ${value.name} (age ${value.age}) in the array", myData, function (person) {
        expect(person.age).to.be.greaterThan(20);
    })
})

Result:

  test that person objects are older than 20
    ✓ test person rob (age 23) in the array
    ✓ test person sally (age 29) in the array


  2 passing (14ms)

mocha-param's People

Contributors

andreynazarov3 avatar holdyourwaffle avatar jcready avatar mikejsdev avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.