├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── index.js ├── package.json └── test └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # node-waf configuration 20 | .lock-wscript 21 | 22 | # Compiled binary addons (http://nodejs.org/api/addons.html) 23 | build/Release 24 | 25 | # Dependency directory 26 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git 27 | node_modules 28 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "4" 4 | - "5" 5 | - "6" 6 | sudo: false 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015-2016 thunks 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | thunk-mocha 2 | ========== 3 | Enable support for generators in Mocha with backward compatibility. 4 | 5 | [![NPM version][npm-image]][npm-url] 6 | [![Build Status][travis-image]][travis-url] 7 | [![Downloads][downloads-image]][downloads-url] 8 | 9 | It is similar to [Co Mocha](https://github.com/blakeembrey/co-mocha), but it is a different implementation and more powerful. It is a perfect patch for mocha! 10 | 11 | ## Installation 12 | 13 | ``` 14 | npm install thunk-mocha 15 | ``` 16 | 17 | ## Usage 18 | 19 | **Call by mocha in CLI:** 20 | 21 | ```sh 22 | mocha -r thunk-mocha 23 | ``` 24 | 25 | **Call in js file:** 26 | 27 | ```js 28 | // make sure that `mocha` have loaded 29 | require('thunk-mocha')() 30 | // or: require('thunk-mocha')(require('mocha')) 31 | ``` 32 | 33 | 34 | ## Example 35 | 36 | **After patched, mocha support:** 37 | ```js 38 | it('support sync test', function () { 39 | // do some test 40 | }) 41 | 42 | it('support thunk style async test', function (done) { 43 | // do some test 44 | done() 45 | }) 46 | 47 | it('support promise style async test', function () { 48 | // do some test 49 | return promiseLikeObject 50 | }) 51 | 52 | it('support generator style async test', function * () { 53 | // do some test 54 | yield promise 55 | // yield thunk 56 | // yield generator 57 | // ... 58 | }) 59 | 60 | it('support async/await style async test', async function () { 61 | // do some test 62 | await promise 63 | }) 64 | 65 | it('support Rx.Observable style async test', function () { 66 | // do some test 67 | return Rx.Observable.bindNodeCallback(fs.stat)('package.json') 68 | }) 69 | ``` 70 | 71 | [npm-url]: https://npmjs.org/package/thunk-mocha 72 | [npm-image]: http://img.shields.io/npm/v/thunk-mocha.svg 73 | 74 | [travis-url]: https://travis-ci.org/thunks/thunk-mocha 75 | [travis-image]: http://img.shields.io/travis/thunks/thunk-mocha.svg 76 | 77 | [downloads-url]: https://npmjs.org/package/thunk-mocha 78 | [downloads-image]: http://img.shields.io/npm/dm/thunk-mocha.svg?style=flat-square 79 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | // **Github:** https://github.com/thunks/thunk-mocha 3 | // 4 | // **License:** MIT 5 | 6 | const thunk = require('thunks')() 7 | 8 | module.exports = thunkMocha 9 | 10 | // Patch available mocha instances automatically. 11 | findMocha().forEach(thunkMocha) 12 | 13 | function thunkMocha (mocha) { 14 | if (!mocha || mocha.Runnable.thunkMochaPatched) return 15 | mocha.Runnable.thunkMochaPatched = true 16 | 17 | let runnableProto = mocha.Runnable.prototype 18 | runnableProto._originFn = void 0 19 | 20 | Object.defineProperty(runnableProto, 'fn', { 21 | get: function () { return this._fn }, 22 | set: function (fn) { 23 | if (typeof fn !== 'function') this._fn = fn 24 | else { 25 | this._fn = function (done) { 26 | thunk.call(this, fn.length ? fn : fn.call(this))(done) 27 | } 28 | } 29 | }, 30 | enumerable: true, 31 | configurable: false 32 | }) 33 | 34 | Object.defineProperty(runnableProto, 'async', { 35 | get: function () { return true }, 36 | set: function () {}, 37 | enumerable: true, 38 | configurable: false 39 | }) 40 | 41 | Object.defineProperty(runnableProto, 'sync', { 42 | get: function () { return false }, 43 | set: function () {}, 44 | enumerable: true, 45 | configurable: false 46 | }) 47 | } 48 | 49 | function findMocha () { 50 | if (typeof window === 'object' && window.Mocha) return [window.Mocha] 51 | 52 | let modules = require.cache || {} 53 | 54 | let items = Object.keys(modules) 55 | .filter(function (name) { 56 | // - end of .3.0.2@mocha/index.js for npminstall 57 | // - end of /mocha/index.js for npm 58 | return /[\/\\@]mocha[\/\\]index.js$/.test(name) 59 | }) 60 | .map(function (name) { 61 | return modules[name].exports 62 | }) 63 | 64 | if (items.length === 0) items.push(require('mocha')) 65 | return items 66 | } 67 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "thunk-mocha", 3 | "version": "1.0.8", 4 | "description": "Enable support for generators in Mocha with backward compatibility.", 5 | "main": "index.js", 6 | "authors": [ 7 | "Yan Qing " 8 | ], 9 | "scripts": { 10 | "test": "standard && mocha -r ./index" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "git://github.com/thunks/thunk-mocha.git" 15 | }, 16 | "keywords": [ 17 | "mocha", 18 | "thunk", 19 | "generator" 20 | ], 21 | "engines": { 22 | "node": ">=1" 23 | }, 24 | "dependencies": { 25 | "thunks": "^4.7.5" 26 | }, 27 | "devDependencies": { 28 | "mocha": "^3.1.1", 29 | "standard": "^8.3.0" 30 | }, 31 | "peerDependencies": { 32 | "mocha": ">=2 || >=3.0" 33 | }, 34 | "license": "MIT", 35 | "bugs": { 36 | "url": "https://github.com/thunks/thunk-mocha/issues" 37 | }, 38 | "homepage": "https://github.com/thunks/thunk-mocha", 39 | "files": [ 40 | "index.js", 41 | "README.md" 42 | ] 43 | } 44 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | // **Github:** https://github.com/thunks/thunk-mocha 3 | // 4 | // **License:** MIT 5 | 6 | /* global describe, it, before, after, beforeEach */ 7 | 8 | var assert = require('assert') 9 | var thunk = require('thunks')() 10 | 11 | describe('thunk-mocha', function () { 12 | var result = [] 13 | 14 | // support generator 15 | before(function * () { 16 | yield thunk.delay(100) 17 | result.push('before') 18 | }) 19 | 20 | // support simple sync test 21 | after(function () { 22 | assert.deepEqual(result, [ 23 | 'before', 24 | 'beforeEach', 25 | 'sync test', 26 | 'beforeEach', 27 | 'compatibility', 28 | 'beforeEach', 29 | 'generator', 30 | 'beforeEach', 31 | 'promise' 32 | ]) 33 | }) 34 | 35 | beforeEach(function * () { 36 | yield thunk.delay(100) 37 | result.push('beforeEach') 38 | }) 39 | 40 | it('sync test', function () { 41 | result.push('sync test') 42 | }) 43 | 44 | it('compatibility', function (done) { 45 | setTimeout(function () { 46 | result.push('compatibility') 47 | done() 48 | }, 100) 49 | }) 50 | 51 | it('generator', function * () { 52 | yield thunk.delay(100) 53 | result.push('generator') 54 | }) 55 | 56 | it('promise', function () { 57 | return new Promise(function (resolve, reject) { 58 | result.push('promise') 59 | resolve() 60 | }) 61 | }) 62 | }) 63 | --------------------------------------------------------------------------------