Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

mocha-given

rendro2080.1.3

Adds a Given-When-Then DSL to mocha as an alternative style for specs

mocha, given, when, then, test, tdd, bdd

readme

mocha-given

Build Status NPM version

Mocha-given is a mocha interface that helps you write cleaner specs using Given, When, Then and And. It is a shameless port of Justin Searls' jasmine-given which is a tribute to Jim Weirich's terrific rspec-given gem.

Example Specs

describe 'assigning stuff to this', ->
    Given -> @number = 24
    When  -> @number++
    And   -> @number *= 2
    Then  -> @number == 50

describe 'assigning stuff to variables', ->
    subject = null
    Given -> subject = []
    When  -> subject.push('foo')
    Then  -> subject.length == 1

describe 'Testing deferred', ->
    Given -> @t = Date.now()
    Then.after 1500, 'so much time has passed', -> Date.now() - @t >= 1500

describe 'Testing async', ->
    Given -> @subject = new User()
    Then 'save user', (done) -> @subject.save(done);

Run tests

Global installation of mocha

If you have mocha installed globally you need to install mocha-given globally as well.

$ npm install -g mocha mocha-given

Then you can run your tests by setting the interface of mocha to mocha-given

$ mocha -u mocha-given --compilers coffee:coffee-script -R spec

Local installation of mocha

If you have installed mocha and mocha-given locally

$ npm install mocha-given coffee-script

you have to call the mocha binary directly:

$ ./node_modules/.bin/mocha -u mocha-given --compilers coffee:coffee-script -R spec

Run mocha-given tests & start contributing

To run the mocha-given tests for developing, it has to be symlinked into the node_modules folder to enable mocha to resolve mocha-given.

Therefore run the script:

$ npm run link

Afterwards mocha has to be installed with $ npm install mocha.

Now you can run the tests using $ npm tests and start contributing.

Run tests programmatically

var Mocha = require('mocha');
var fs    = require('fs');
var path  = require('path');

// require mocha-given after Mocha is set
require('mocha-given');

// the directory with your tests/specs
var testDir = 'tests';

// First, you need to instantiate a Mocha instance.
var mocha = new Mocha({
    ui: 'mocha-given',
    reporter: 'spec'
});

// Get test files
fs.readdirSync(testDir).filter(function(file){
    // allow javascript and coffescript files
    return file.match(/\.(coffee|js)$/);
}).forEach(function(file){
    mocha.addFile(
        path.join(testDir, file)
    );
});

// Now, you can run the tests.
mocha.run(function(failures){
  process.on('exit', function () {
    process.exit(failures);
  });
});

Run from command line (with mocha and mocha-given installed):

$ node runtests.js

Credits

Thanks to SinnerSchrader for their support and the time to work on this project.

changelog

CHANGELOG

Version 0.1.2, Jul 10, 2014

  • Return value of assertion must strictly equal false for test to fail #3
  • Add browser field to package.json

Version 0.1.1, Jun 5, 2014

  • Update coffee-script and mocha peer dependency

Version 0.1.0, Feb 4, 2014

  • Update all dependencies

Version 0.0.18, Jan 8, 2014

  • Updated mocha peer dependency

Version 0.0.17, Dec 17, 2013

  • fixed undefined error in test label

Version 0.0.16, Dec 13, 2013

  • added Then.only for deferred tests
  • added async Whens and Invariants

Version 0.0.15, Dec 11, 2013

  • added mocha-given interface for tests running in a browser

Version 0.0.14, Dec 11, 2013

  • removed useless dependencies

Version 0.0.13, Dec 11, 2013

  • fixed async Thens

Version 0.0.12, Dec 10, 2013

  • remove expect.js dependency

Version 0.0.11, Dec 10, 2013

  • Add Invariant

Version 0.0.10, Dec 9, 2013

  • When has same syntax and logic as Given
  • added more tests

Version 0.0.9, Dec 9, 2013

  • execute all Givens before the Whens
  • added given when then execution order tests

Version 0.0.8, Dec 9, 2013

  • fixed scope errors my mochas "shared behaviour"

Version 0.0.7, Dec 5, 2013

  • added support for async Then with a done callback

Version 0.0.6, Dec 5, 2013

  • fixed And

Version 0.0.5, Dec 4, 2013

  • Added And

Version 0.0.4, Dec 3, 2013

  • fixed broken export

Version 0.0.3, Dec 3, 2013

  • export MochaGivenSuite to allow it being automatically loaded by mocha

Version 0.0.2, Dec 3, 2013

  • Added Then.only

Version 0.0.1, Dec 3, 2013

  • Initial version with Given, When, and Then