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.
- Host: GitHub
- URL: https://github.com/allenluce/mocha-table
- Owner: allenluce
- Created: 2018-06-01T21:34:16.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2022-12-30T20:36:14.000Z (over 3 years ago)
- Last Synced: 2025-09-15T22:20:45.210Z (10 months ago)
- Topics: javascript, mocha, testing
- Language: JavaScript
- Size: 397 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Mocha Table
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)
})

## 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)
})

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.