Comparing version 1.1.0 to 1.1.1
320
co-mocha.js
@@ -1,6 +0,12 @@ | ||
var co = require('co'); | ||
var path = require('path'); | ||
var isGenFn = require('is-generator').fn; | ||
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.coMocha = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
var co = require('co') | ||
var path = require('path') | ||
var isGenFn = require('is-generator').fn | ||
/** | ||
* Export `co-mocha`. | ||
*/ | ||
module.exports = coMocha | ||
/** | ||
* Monkey patch the mocha instance with generator support. | ||
@@ -10,10 +16,10 @@ * | ||
*/ | ||
var coMocha = module.exports = function (mocha) { | ||
function coMocha (mocha) { | ||
// Avoid loading `co-mocha` twice. | ||
if (mocha._coMochaIsLoaded) { | ||
return; | ||
if (!mocha || mocha._coMochaIsLoaded) { | ||
return | ||
} | ||
var Runnable = mocha.Runnable; | ||
var run = Runnable.prototype.run; | ||
var Runnable = mocha.Runnable | ||
var run = Runnable.prototype.run | ||
@@ -27,10 +33,10 @@ /** | ||
if (isGenFn(this.fn)) { | ||
this.fn = co.wrap(this.fn); | ||
this.fn = co.wrap(this.fn) | ||
} | ||
return run.call(this, fn); | ||
}; | ||
return run.call(this, fn) | ||
} | ||
return mocha._coMochaIsLoaded = true; | ||
}; | ||
mocha._coMochaIsLoaded = true | ||
} | ||
@@ -42,24 +48,282 @@ /** | ||
*/ | ||
var findNodeJSMocha = function () { | ||
var suffix = path.sep + path.join('', 'mocha', 'index.js'); | ||
var children = require.cache || {}; | ||
function findNodeJSMocha () { | ||
var suffix = path.sep + path.join('', 'mocha', 'index.js') | ||
var children = require.cache || {} | ||
return Object.keys(children).filter(function (child) { | ||
return child.slice(suffix.length * -1) === suffix; | ||
}).map(function (child) { | ||
return children[child].exports; | ||
}); | ||
return Object.keys(children) | ||
.filter(function (child) { | ||
return child.slice(suffix.length * -1) === suffix | ||
}) | ||
.map(function (child) { | ||
return children[child].exports | ||
}) | ||
} | ||
// Attempt to automatically monkey patch available mocha instances. | ||
var modules = typeof window === 'undefined' ? findNodeJSMocha() : [window.Mocha] | ||
modules.forEach(coMocha) | ||
},{"co":3,"is-generator":4,"path":2}],2:[function(require,module,exports){ | ||
},{}],3:[function(require,module,exports){ | ||
/** | ||
* slice() reference. | ||
*/ | ||
var slice = Array.prototype.slice; | ||
/** | ||
* Expose `co`. | ||
*/ | ||
module.exports = co; | ||
/** | ||
* Wrap the given generator `fn` into a | ||
* function that returns a promise. | ||
* This is a separate function so that | ||
* every `co()` call doesn't create a new, | ||
* unnecessary closure. | ||
* | ||
* @param {GeneratorFunction} fn | ||
* @return {Function} | ||
* @api public | ||
*/ | ||
co.wrap = function (fn) { | ||
return function () { | ||
return co.call(this, fn.apply(this, arguments)); | ||
}; | ||
}; | ||
/** | ||
* Attempt to automatically monkey patch available mocha instances. | ||
* Execute the generator function or a generator | ||
* and return a promise. | ||
* | ||
* @param {Function} fn | ||
* @return {Function} | ||
* @api public | ||
*/ | ||
try { | ||
var modules = []; | ||
if (typeof require === 'function' && typeof exports === 'object') { | ||
modules = findNodeJSMocha(); | ||
function co(gen) { | ||
var ctx = this; | ||
if (typeof gen === 'function') gen = gen.call(this); | ||
return onFulfilled(); | ||
/** | ||
* @param {Mixed} res | ||
* @return {Promise} | ||
* @api private | ||
*/ | ||
function onFulfilled(res) { | ||
var ret; | ||
try { | ||
ret = gen.next(res); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
return next(ret); | ||
} | ||
modules.forEach(coMocha); | ||
} catch (e) {} | ||
/** | ||
* @param {Error} err | ||
* @return {Promise} | ||
* @api private | ||
*/ | ||
function onRejected(err) { | ||
var ret; | ||
try { | ||
ret = gen.throw(err); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
return next(ret); | ||
} | ||
/** | ||
* Get the next value in the generator, | ||
* return a promise. | ||
* | ||
* @param {Object} ret | ||
* @return {Promise} | ||
* @api private | ||
*/ | ||
function next(ret) { | ||
if (ret.done) return Promise.resolve(ret.value); | ||
var value = toPromise.call(ctx, ret.value); | ||
if (value && isPromise(value)) return value.then(onFulfilled, onRejected); | ||
return onRejected(new TypeError('You may only yield a function, promise, generator, array, or object, ' | ||
+ 'but the following object was passed: "' + String(ret.value) + '"')); | ||
} | ||
} | ||
/** | ||
* Convert a `yield`ed value into a promise. | ||
* | ||
* @param {Mixed} obj | ||
* @return {Promise} | ||
* @api private | ||
*/ | ||
function toPromise(obj) { | ||
if (!obj) return obj; | ||
if (isPromise(obj)) return obj; | ||
if (isGeneratorFunction(obj) || isGenerator(obj)) return co.call(this, obj); | ||
if ('function' == typeof obj) return thunkToPromise.call(this, obj); | ||
if (Array.isArray(obj)) return arrayToPromise.call(this, obj); | ||
if (isObject(obj)) return objectToPromise.call(this, obj); | ||
return obj; | ||
} | ||
/** | ||
* Convert a thunk to a promise. | ||
* | ||
* @param {Function} | ||
* @return {Promise} | ||
* @api private | ||
*/ | ||
function thunkToPromise(fn) { | ||
var ctx = this; | ||
return new Promise(function (resolve, reject) { | ||
fn.call(ctx, function (err, res) { | ||
if (err) return reject(err); | ||
if (arguments.length > 2) res = slice.call(arguments, 1); | ||
resolve(res); | ||
}); | ||
}); | ||
} | ||
/** | ||
* Convert an array of "yieldables" to a promise. | ||
* Uses `Promise.all()` internally. | ||
* | ||
* @param {Array} obj | ||
* @return {Promise} | ||
* @api private | ||
*/ | ||
function arrayToPromise(obj) { | ||
return Promise.all(obj.map(toPromise, this)); | ||
} | ||
/** | ||
* Convert an object of "yieldables" to a promise. | ||
* Uses `Promise.all()` internally. | ||
* | ||
* @param {Object} obj | ||
* @return {Promise} | ||
* @api private | ||
*/ | ||
function objectToPromise(obj){ | ||
var results = new obj.constructor(); | ||
var keys = Object.keys(obj); | ||
var promises = []; | ||
for (var i = 0; i < keys.length; i++) { | ||
var key = keys[i]; | ||
var promise = toPromise.call(this, obj[key]); | ||
if (promise && isPromise(promise)) defer(promise, key); | ||
else results[key] = obj[key]; | ||
} | ||
return Promise.all(promises).then(function () { | ||
return results; | ||
}); | ||
function defer(promise, key) { | ||
// predefine the key in the result | ||
results[key] = undefined; | ||
promises.push(promise.then(function (res) { | ||
results[key] = res; | ||
})); | ||
} | ||
} | ||
/** | ||
* Check if `obj` is a promise. | ||
* | ||
* @param {Object} obj | ||
* @return {Boolean} | ||
* @api private | ||
*/ | ||
function isPromise(obj) { | ||
return 'function' == typeof obj.then; | ||
} | ||
/** | ||
* Check if `obj` is a generator. | ||
* | ||
* @param {Mixed} obj | ||
* @return {Boolean} | ||
* @api private | ||
*/ | ||
function isGenerator(obj) { | ||
return 'function' == typeof obj.next && 'function' == typeof obj.throw; | ||
} | ||
/** | ||
* Check if `obj` is a generator function. | ||
* | ||
* @param {Mixed} obj | ||
* @return {Boolean} | ||
* @api private | ||
*/ | ||
function isGeneratorFunction(obj) { | ||
var constructor = obj.constructor; | ||
return constructor && 'GeneratorFunction' == constructor.name; | ||
} | ||
/** | ||
* Check for plain object. | ||
* | ||
* @param {Mixed} val | ||
* @return {Boolean} | ||
* @api private | ||
*/ | ||
function isObject(val) { | ||
return Object == val.constructor; | ||
} | ||
},{}],4:[function(require,module,exports){ | ||
/** | ||
* Export generator function checks. | ||
*/ | ||
module.exports = isGenerator | ||
module.exports.fn = isGeneratorFunction | ||
/** | ||
* Check whether an object is a generator. | ||
* | ||
* @param {Object} obj | ||
* @return {Boolean} | ||
*/ | ||
function isGenerator (obj) { | ||
return obj && | ||
typeof obj.next === 'function' && | ||
typeof obj.throw === 'function' | ||
} | ||
/** | ||
* Check whether a function is generator. | ||
* | ||
* @param {Function} fn | ||
* @return {Boolean} | ||
*/ | ||
function isGeneratorFunction (fn) { | ||
return typeof fn === 'function' && | ||
fn.constructor && | ||
fn.constructor.name === 'GeneratorFunction' && | ||
isGenerator(fn.prototype) | ||
} | ||
},{}]},{},[1])(1) | ||
}); |
{ | ||
"name": "co-mocha", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "Enable support for generators in Mocha tests", | ||
"main": "co-mocha.js", | ||
"main": "lib/co-mocha.js", | ||
"files": [ | ||
"co-mocha.js", | ||
"lib/", | ||
"LICENSE" | ||
], | ||
"browser": { | ||
"path": false | ||
}, | ||
"scripts": { | ||
"test": "node --harmony-generators node_modules/.bin/istanbul cover _mocha -- -R spec" | ||
"lint": "standard", | ||
"build": "browserify lib/co-mocha.js -s co-mocha -o co-mocha.js", | ||
"test-spec": "mocha -R spec --require lib/co-mocha.js --bail", | ||
"test-cov": "testem ci -l Firefox,Node", | ||
"test": "npm run lint && npm run build && npm run test-cov" | ||
}, | ||
@@ -30,11 +42,24 @@ "repository": { | ||
"devDependencies": { | ||
"bluebird": "^2.3.2", | ||
"browserify": "^10.2.4", | ||
"chai": "^3.0.0", | ||
"es6-promise": "^2.3.0", | ||
"istanbul": "git://github.com/gotwarlost/istanbul#harmony", | ||
"mocha": "^1.18.2", | ||
"regenerator": "^0.4.12", | ||
"traceur": "0.0.58" | ||
"mocha": "^2.2.5", | ||
"pre-commit": "^1.0.7", | ||
"regenerator": "^0.8.26", | ||
"standard": "^3.11.1", | ||
"testem": "^0.8.3", | ||
"traceur": "0.0.89" | ||
}, | ||
"standard": { | ||
"ignore": [ | ||
"coverage/**", | ||
"node_modules/**", | ||
"bower_components/**", | ||
"co-mocha.js" | ||
] | ||
}, | ||
"dependencies": { | ||
"co": "^4.0.0", | ||
"is-generator": "^1.0.0" | ||
"is-generator": "^1.0.1" | ||
}, | ||
@@ -41,0 +66,0 @@ "peerDependencies": { |
# Co Mocha | ||
[![NPM version][npm-image]][npm-url] | ||
[![NPM downloads][downloads-image]][downloads-url] | ||
[![Build status][travis-image]][travis-url] | ||
[![Test coverage][coveralls-image]][coveralls-url] | ||
[![Gittip][gittip-image]][gittip-url] | ||
@@ -23,5 +23,5 @@ Enable support for generators in Mocha tests using [co](https://github.com/visionmedia/co). | ||
```js | ||
it('should do something', function* () { | ||
yield users.load(123); | ||
}); | ||
it('should do something', function * () { | ||
yield users.load(123) | ||
}) | ||
``` | ||
@@ -33,10 +33,23 @@ | ||
### AMD | ||
If you need to monkey patch a different mocha instance you can use the library directly: | ||
Not currently supported. | ||
```js | ||
var mocha = require('mocha') | ||
var coMocha = require('co-mocha') | ||
coMocha(mocha) | ||
``` | ||
### `<script>` Tag | ||
Not currently supported. | ||
```html | ||
<script src="co-mocha.js"></script> | ||
``` | ||
Including the browserified script will automatically patch `window.Mocha`. Just make sure you include it after `mocha.js`. If that is not possible the library exposes `window.coMocha`, which can be used (`window.coMocha(window.Mocha)`). | ||
### AMD | ||
Same details as the script, but using AMD requires instead. | ||
## How It Works | ||
@@ -52,2 +65,4 @@ | ||
[npm-url]: https://npmjs.org/package/co-mocha | ||
[downloads-image]: https://img.shields.io/npm/dm/co-mocha.svg?style=flat | ||
[downloads-url]: https://npmjs.org/package/co-mocha | ||
[travis-image]: https://img.shields.io/travis/blakeembrey/co-mocha.svg?style=flat | ||
@@ -57,3 +72,1 @@ [travis-url]: https://travis-ci.org/blakeembrey/co-mocha | ||
[coveralls-url]: https://coveralls.io/r/blakeembrey/co-mocha?branch=master | ||
[gittip-image]: https://img.shields.io/gittip/blakeembrey.svg?style=flat | ||
[gittip-url]: https://www.gittip.com/blakeembrey |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
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
14051
332
69
1
10
5
2
Updatedis-generator@^1.0.1