Skip to content

Latest commit

 

History

History
129 lines (89 loc) · 3.27 KB

File metadata and controls

129 lines (89 loc) · 3.27 KB

Examples

Following are example specifications using different testing libraries.

Mocha BDD with Chai

let { expect } = chai

describe('mocha + chai', (done) => {
  function spec() {
    expect(c).to.equal(a + b)

    where: `
    a | b | c
    1 | 2 | 3
    "h" | 'b' | "one, 'please'" // should fail
    `;
  }

  where(spec).forEach(scenario => {
    var { a, b, c } = scenario.params

    /*
     * mocha requires the it() function to run all scenarios;
     * otherwise, it reports nothing for passing assertions,
     * and halts on the first failing assertion.
     */

    it(`with ${a} and ${b}, should get ${c}`, scenario.test)
  });

  done();
});

Node.js

You can view the mocha-node example at /examples/mocha/suite.js, and run the mocha node suite with npm run mocha-node.

Browser

You can view the mocha browser example at /examples/mocha/suite.html, and run the mocha browser suite with npm run mocha.

Note: Bug found in Mocha 8.1.x which fails in strict Content-Security-Policy that does not allow script-src 'unsafe-eval' in browsers. See Facebook Still Breaking Things.

QUnit

Note: On Node.js, you must call QUnit.start() after defining all modules.

let { module, test } = QUnit

module("wheredoc", hooks => {
  test('should run the equals test', function (assert) {
    var done = assert.async();

    function spec(a, b, c) {
      assert.equal(a + b, c, `with ${a} and ${b} expect ${c}.`);

      where: `
      a | b | c
      1 | 2 | 3
      "h" | 'b' | "one, 'please'" // should fail
      `;
    }

    where(spec).forEach((scenario, i) => {

      /*
       * You can call the test function directly in QUnit.
       */

      scenario.test();
    });

    done();
  })
})

// Finally, call QUnit.start().
// https://api.qunitjs.com/config/QUnit.config/
QUnit.start()

Node.js

On Node.js, we use qunit-tap to print the QUnit test results to the console. You can view the QUnit node example at /examples/qunit/suite.js, and run the QUnit node suite with npm run qunit-node.

Browser

You can view the QUnit browser example at /examples/qunit/suite.html, and run the QUnit browser suite with npm run qunit.

Tape

Tape is pretty easy, you just need to require it and call test.end() after running the scenarios.

tape('suite', function(test) {
  function spec(a, b, c) {
    test.equal(a + b, c, `with ${a} and ${b}, expect ${c}`);

    where: `
    a | b | c
    1 | 2 | 3
    "h" | 'b' | "one, 'please'" // should fail
    `;
  }

  where(spec).forEach(scenario => {

    /*
     * You can call the test function directly in tape.
     */

    scenario.test()
  });

  test.end();
});

Node.js

On the Node.js example, we use tape-describe to rename tape and test, and so forth. You can view the tape example at /examples/tape/suite.js, and run the tape node suite with npm run tape.