Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Feb 3, 2014
0 parents commit dc79415
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store
node_modules
coverage
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2014 Blake Embrey ([email protected])

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# co-mocha

Enable support for generators in Mocha tests using [co](https://github.com/visionmedia/co).

Currently you must use the `--harmony-generators` flag when running node 0.11.x to get access to generators.

## Installation

```
npm install blakeembrey/co-mocha --save-dev
```

## Usage

Add `--require co-mocha` to your `mocha.opts`.

## License

MIT
18 changes: 18 additions & 0 deletions co-mocha.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var co = require('co');
var mocha = require('mocha');
var Runnable = mocha.Runnable;
var run = Runnable.prototype.run;

/**
* Override the Mocha function runner and enable generator support with co.
*
* @param {Function} fn
*/
Runnable.prototype.run = function (fn) {
if (this.fn.constructor.name === 'GeneratorFunction') {
this.fn = co(this.fn);
this.sync = !(this.async = true);
}

return run.call(this, fn);
};
35 changes: 35 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "co-mocha",
"version": "0.0.0",
"description": "Enable support for generators in Mocha tests",
"main": "co-mocha.js",
"scripts": {
"test": "node --harmony-generators node_modules/.bin/istanbul cover _mocha --require . -- -R spec"
},
"repository": {
"type": "git",
"url": "git://github.com/blakeembrey/co-mocha.git"
},
"keywords": [
"co",
"mocha",
"harmony",
"generators"
],
"author": "Blake Embrey",
"license": "MIT",
"bugs": {
"url": "https://github.com/blakeembrey/co-mocha/issues"
},
"homepage": "https://github.com/blakeembrey/co-mocha",
"devDependencies": {
"mocha": "~1.17.0",
"istanbul": "git://github.com/gotwarlost/istanbul#harmony"
},
"dependencies": {
"co": "3.x"
},
"peerDependencies": {
"mocha": "1.x"
}
}
39 changes: 39 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* global describe, it */

var assert = require('assert');

/**
* Wait a certain amount of time before proceeding to the callback.
*
* @param {Number} ms
* @return {Function}
*/
var wait = function (ms) {
return function (fn) {
setTimeout(fn, ms);
};
};

describe('co-mocha', function () {
before(function* () {
yield wait(100);
});

after(function* () {
yield wait(100);
});

it('should work synchronously', function () {
assert.equal(1 + 1, 2);
});

it('should work with generators', function* () {
assert.equal(1 + 1, 2);
yield wait(100);
});

it('should work with with callbacks', function (done) {
assert.equal(1 + 1, 2);
wait(100)(done);
});
});

0 comments on commit dc79415

Please sign in to comment.