Comparing version 0.0.2 to 0.0.3
@@ -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) {} |
{ | ||
"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 |
87
test.js
/* 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) | ||
); | ||
}); | ||
}); |
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
7600
157
42
4
5
6
+ Addedis-generator@0.0.4
+ Addedis-promise@^1.0.0
+ Addedis-generator@0.0.4(transitive)
+ Addedis-promise@1.0.1(transitive)