Giter Site home page Giter Site logo

mocha-table's Introduction

Mocha Table

Travis CI Build Status NPM version

Table-driven tests for Mocha. 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 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

In the browser

Here's a simple HTML test harness:

<html>
<head>
    <meta charset="utf-8">
    <title>Mocha Table Tests</title>
    <link href="https://unpkg.com/[email protected]/mocha.css" rel="stylesheet" />
</head>
<body>
<div id="mocha"></div>

<script src="https://unpkg.com/[email protected]/chai.js"></script>
<script src="https://unpkg.com/[email protected]/mocha.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>

<script>mocha.setup('bdd')</script>
<script src="prime.js"></script>
<script src="prime.test.js"></script>
<script>
    mocha.checkLeaks();
    mocha.run();
</script>
</body>
</html>

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

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 to create a version that's easily usable on the web (via jsDelivr). 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.

mocha-table's People

Contributors

dougluce avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  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.