An open API service indexing awesome lists of open source software.

https://github.com/allenluce/mocha-table

Table-driven tests for Mocha.
https://github.com/allenluce/mocha-table

javascript mocha testing

Last synced: 6 months ago
JSON representation

Table-driven tests for Mocha.

Awesome Lists containing this project

README

          

# Mocha Table


Travis CI Build Status


NPM version

Table-driven tests for [Mocha](https://mochajs.org/). Lets you easily
specify data-driven tests with nice test names. Allows for easy focus
and skipping of individual test entries.

## In Node.js

### Installation

npm install mocha-table --save-dev

[sprintf.js](https://github.com/alexei/sprintf.js) is used to
interpolate a template, resulting in nice test names for each table
entry. The string template (the second argument to `describeTable`) is
fed the values that are given to the test function (the third
argument).

const expect = require('chai').expect
const {describeTable, tableIt, entryIt, xentryIt} = require('mocha-table')

describeTable('Primality tests', function() {
tableIt('is %d prime? (should be %t)', function (number, result) {
expect(isPrime(number)).to.equal(result)
})

entryIt(3, true)
entryIt(4, false)
entryIt.skip(15, false)
xentryIt(21, false)
entryIt(1847, true)
entryIt(1848, false)
})

![You'll get nice results](https://github.com/allenluce/mocha-table/raw/master/assets/web.png)

## In the browser

Here's a simple HTML test harness:




Mocha Table Tests





mocha.setup('bdd')



mocha.checkLeaks();
mocha.run();


Given a `prime.js` like this:

// My amazing O(1) primality tester.
// Works for the majority of integers!!
function isPrime (n) {
return (n === 2 || n / 2 !== Math.floor(n / 2)) &&
(n === 3 || n / 3 !== Math.floor(n / 3))
}

A table-driven test `prime.test.js` file looks like:

const mochaTable = require('mocha-table')
const describeTable = mochaTable.describeTable
const tableIt = mochaTable.tableIt
const entryIt = mochaTable.entryIt
const xentryIt = mochaTable.xentryIt
const expect = chai.expect

describeTable('Primality tests', function() {
tableIt('is %d prime? (should be %t)', function (number, result) {
expect(isPrime(number)).to.equal(result)
})

entryIt(3, true)
entryIt(4, false)
entryIt.skip(15, false)
xentryIt(21, false)
entryIt(1847, true)
entryIt(1848, false)
})

![You'll get nice web results](assets/web.png)

The example above demonstrates the use of `xentryIt` and `entryIt.skip` to
mark some pesky tests that haven't been fixed yet. An entry can also be
focused programmatically using `.only` to mark it:

...
entryIt.only(3, true),
...

## Building from source

Mocha Table uses [Browserify](http://browserify.org/) to create a
version that's easily usable on the web (via
[jsDelivr](jsdelivr.com)). Make sure any changes to `index.js` are
reflected in the built asset:

npm run build

## Mocha version

Mocha Table works with Mocha version 4.0.0 or higher.