From dc79415cc99b16fc449564e6ed8e0553e80851ac Mon Sep 17 00:00:00 2001 From: Blake Embrey Date: Mon, 3 Feb 2014 11:36:01 -0500 Subject: [PATCH] Initial commit. --- .gitignore | 3 +++ LICENSE | 21 +++++++++++++++++++++ README.md | 19 +++++++++++++++++++ co-mocha.js | 18 ++++++++++++++++++ package.json | 35 +++++++++++++++++++++++++++++++++++ test.js | 39 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 135 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 co-mocha.js create mode 100644 package.json create mode 100644 test.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..032441d --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.DS_Store +node_modules +coverage diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..983fbe8 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2014 Blake Embrey (hello@blakeembrey.com) + +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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..a29e2b8 --- /dev/null +++ b/README.md @@ -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 diff --git a/co-mocha.js b/co-mocha.js new file mode 100644 index 0000000..b2ed3e1 --- /dev/null +++ b/co-mocha.js @@ -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); +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..e83d6ae --- /dev/null +++ b/package.json @@ -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" + } +} diff --git a/test.js b/test.js new file mode 100644 index 0000000..5315899 --- /dev/null +++ b/test.js @@ -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); + }); +});