New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

co-mocha

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

co-mocha - npm Package Compare versions

Comparing version 0.0.2 to 0.0.3

86

co-mocha.js

@@ -1,18 +0,82 @@

var co = require('co');
var mocha = require('mocha');
var Runnable = mocha.Runnable;
var run = Runnable.prototype.run;
var co = require('co');
var path = require('path');
var isPromise = require('is-promise');
var isGenerator = require('is-generator');
/**
* Override the Mocha function runner and enable generator support with co.
* Monkey patch the mocha instance with generator support.
*
* @param {Function} fn
* @param {Function} mocha
*/
Runnable.prototype.run = function (fn) {
if (this.fn.constructor.name === 'GeneratorFunction') {
this.fn = co(this.fn);
var coMocha = module.exports = function (mocha) {
// Avoid loading `co-mocha` twice.
if (mocha._coMochaIsLoaded) {
return;
}
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) {
var func = this.fn;
var sync = this.sync;
// Flip the async switch to always have a callback function provided.
this.sync = !(this.async = true);
// Override the function to provide a special generator handler.
this.fn = function (done) {
var result = sync ? func() : func(done);
if (isGenerator(result)) {
return co(result)(done);
}
if (isPromise(result)) {
return result.then(function () {
return done();
}, done);
}
return sync && done();
};
return run.call(this, fn);
};
return mocha._coMochaIsLoaded = true;
};
/**
* Find active node mocha instances.
*
* @return {Array}
*/
var findNodeJSMocha = function () {
var suffix = path.sep + path.join('', 'mocha', 'index.js');
var children = require.main && require.main.children || [];
return children.filter(function (child) {
return child.id.slice(suffix.length * -1) === suffix;
}).map(function (child) {
return child.exports;
});
};
/**
* Attempt to automatically monkey patch available mocha instances.
*/
try {
var modules = [];
if (typeof require === 'function' && typeof exports === 'object') {
modules = findNodeJSMocha();
}
return run.call(this, fn);
};
modules.forEach(coMocha);
} catch (e) {}

13

package.json
{
"name": "co-mocha",
"version": "0.0.2",
"version": "0.0.3",
"description": "Enable support for generators in Mocha tests",

@@ -26,7 +26,12 @@ "main": "co-mocha.js",

"devDependencies": {
"mocha": "~1.17.0",
"istanbul": "git://github.com/gotwarlost/istanbul#harmony"
"bluebird": "^1.2.4",
"istanbul": "git://github.com/gotwarlost/istanbul#harmony",
"mocha": "^1.18.2",
"regenerator": "^0.4.7",
"traceur": "0.0.39"
},
"dependencies": {
"co": "3.x"
"co": "3.x",
"is-generator": "0.0.4",
"is-promise": "^1.0.0"
},

@@ -33,0 +38,0 @@ "peerDependencies": {

@@ -5,3 +5,3 @@ # co-mocha

Currently you must use the `--harmony-generators` flag when running node 0.11.x to get access to generators.
Use the `--harmony-generators` flag when running node 0.11.x to access generator functions, or transpile your tests using [traceur](https://github.com/google/traceur-compiler) or [regenerator](https://github.com/facebook/regenerator).

@@ -16,3 +16,3 @@ ## Installation

Add `--require co-mocha` to your `mocha.opts`. Now you can write your tests using generators.
Just require the module in your tests and start writing generators in your tests.

@@ -25,4 +25,20 @@ ```js

### Node
Install the module using `npm install co-mocha --save-dev`. Now just require the module to automatically monkey patch any available `mocha` instances. With `mocha`, you have multiple ways of requiring the module - add `--require co-mocha` to your `mocha.opts` or add `require('co-mocha')` inside your main test file.
### AMD
Not yet supported.
### `<script>` Tag
Not yet supported.
## How It Works
We monkey patch the `Runnable.prototype.run` method of `mocha` to enable generators. In contrast to other npm packages, `co-mocha` extends `mocha` at runtime - allowing you to use any compatible mocha version.
## License
MIT
/* global describe, it */
var assert = require('assert');
var traceur = require('traceur');
var Promise = require('bluebird');
var regenerator = require('regenerator');

@@ -26,15 +28,82 @@ /**

it('should work synchronously', function () {
assert.equal(1 + 1, 2);
it('should work synchronously', function () {});
it('should error synchronously', function () {
throw new Error('You had one job');
});
it('should work with generators', function* () {
assert.equal(1 + 1, 2);
yield wait(100);
it('should work with promises', function () {
return new Promise(function (resolve) {
return wait(100)(resolve);
});
});
it('should work with with callbacks', function (done) {
assert.equal(1 + 1, 2);
wait(100)(done);
it('should error with promises', function () {
return new Promise(function (resolve, reject) {
return wait(100)(function () {
return reject(new Error('You promised me'));
});
});
});
it('should work with callbacks', function (done) {
return wait(100)(done);
});
it('should error with callbacks', function (done) {
return wait(100)(function () {
return done(new Error('You never called me back'));
});
});
describe('generators', function () {
/**
* String version of generator based test. This will be used to compile for
* testing different ES6 to ES5 transpilers.
*
* @type {Array}
*/
var testSource = [
'(function* () {',
' yield wait(100);',
'});'
].join('\n');
var testErrorSource = [
'(function* () {',
' yield wait(100);',
' throw new Error(\'This generation has failed\');',
'});'
].join('\n');
it(
'should work with es6 generators',
eval(testSource)
);
it(
'should error with es6 generators',
eval(testErrorSource)
);
it(
'should work with regenerator generators',
eval(regenerator(testSource, { includeRuntime: true }))
);
it(
'should error with regenerator generators',
eval(regenerator(testErrorSource, { includeRuntime: true }))
);
it(
'should work with traceur generators',
eval(traceur.compile(testSource).js)
);
it(
'should error with traceur generators',
eval(traceur.compile(testErrorSource).js)
);
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc