mm
Advanced tools
Comparing version 1.5.1 to 2.0.0
2.0.0 / 2016-07-31 | ||
================== | ||
* feat: upgrade dependencies (#29) | ||
1.5.1 / 2016-07-21 | ||
@@ -3,0 +8,0 @@ ================== |
'use strict'; | ||
module.exports = require('./lib/mm'); | ||
var enable = require('enable'); | ||
const enable = require('enable'); | ||
if (enable.generator) { | ||
@@ -6,0 +6,0 @@ /* yup, ES6 */ |
@@ -15,9 +15,9 @@ /**! | ||
var is = require('is-type-of'); | ||
var sleep = require('co-sleep'); | ||
var mm = require('./mm'); | ||
const is = require('is-type-of'); | ||
const sleep = require('co-sleep'); | ||
const mm = require('./mm'); | ||
var mockDatas = mm.datas; | ||
const mockDatas = mm.datas; | ||
// support generator | ||
mm.datas = function (mod, method, datas, timeout) { | ||
mm.datas = function(mod, method, datas, timeout) { | ||
if (!is.generatorFunction(mod[method])) { | ||
@@ -38,4 +38,4 @@ return mockDatas.call(mm, mod, method, datas, timeout); | ||
var mockData = mm.data; | ||
mm.data = function (mod, method, data, timeout) { | ||
const mockData = mm.data; | ||
mm.data = function(mod, method, data, timeout) { | ||
if (!is.generatorFunction(mod[method])) { | ||
@@ -48,4 +48,4 @@ return mockData.call(mm, mod, method, data, timeout); | ||
var mockError = mm.error; | ||
mm.error = function (mod, method, error, props, timeout) { | ||
const mockError = mm.error; | ||
mm.error = function(mod, method, error, props, timeout) { | ||
if (!is.generatorFunction(mod[method])) { | ||
@@ -52,0 +52,0 @@ return mockError.call(mm, mod, method, error, props, timeout); |
163
lib/mm.js
@@ -15,12 +15,12 @@ /**! | ||
var EventEmitter = require('events'); | ||
var muk = require('muk'); | ||
var http = require('http'); | ||
var https = require('https'); | ||
var cp = require('child_process'); | ||
var semver = require('semver'); | ||
var thenify = require('thenify').withCallback; | ||
const EventEmitter = require('events'); | ||
const muk = require('muk'); | ||
const http = require('http'); | ||
const https = require('https'); | ||
const cp = require('child_process'); | ||
const semver = require('semver'); | ||
const thenify = require('thenify').withCallback; | ||
var mock = module.exports = function mock() { | ||
const mock = module.exports = function mock() { | ||
return muk.apply(null, arguments); | ||
@@ -32,4 +32,4 @@ }; | ||
function getCallback(args) { | ||
var index = args.length - 1; | ||
var callback = args[index]; | ||
let index = args.length - 1; | ||
let callback = args[index]; | ||
while (typeof callback !== 'function') { | ||
@@ -59,7 +59,7 @@ index--; | ||
* | ||
* @param {String|Error} error | ||
* @param {Object} props | ||
* @return {Error} | ||
* @param {String|Error} error - error | ||
* @param {Object} props - props | ||
* @return {Error} error - error | ||
*/ | ||
exports._createError = function (error, props) { | ||
exports._createError = function(error, props) { | ||
if (!error) { | ||
@@ -74,3 +74,3 @@ error = new Error('mm mock error'); | ||
props = props || {}; | ||
for (var key in props) { | ||
for (const key in props) { | ||
error[key] = props[key]; | ||
@@ -87,6 +87,7 @@ } | ||
* @param {String|Error} error, error string message or error instance. | ||
* @param {Object} [props], error properties | ||
* @param {Number} [tiemout], mock async callback timeout, default is 10. | ||
* @param {Object} props, error properties | ||
* @param {Number} timeout, mock async callback timeout, default is 10. | ||
* @return {mm} this - mm | ||
*/ | ||
exports.error = function (mod, method, error, props, timeout) { | ||
exports.error = function(mod, method, error, props, timeout) { | ||
if (typeof props === 'number') { | ||
@@ -102,5 +103,5 @@ timeout = props; | ||
timeout = timeout || 0; | ||
mock(mod, method, thenify(function () { | ||
var callback = getCallback(arguments); | ||
setTimeout(function () { | ||
mock(mod, method, thenify(function() { | ||
const callback = getCallback(arguments); | ||
setTimeout(function() { | ||
callback(error); | ||
@@ -118,5 +119,6 @@ }, timeout); | ||
* @param {Array} datas, return datas array. | ||
* @param {Number} [tiemout], mock async callback timeout, default is 10. | ||
* @param {Number} timeout, mock async callback timeout, default is 10. | ||
* @return {mm} this - mm | ||
*/ | ||
exports.datas = function (mod, method, datas, timeout) { | ||
exports.datas = function(mod, method, datas, timeout) { | ||
if (timeout) { | ||
@@ -129,6 +131,6 @@ timeout = parseInt(timeout, 10); | ||
} | ||
mock(mod, method, thenify(function () { | ||
var callback = getCallback(arguments); | ||
setTimeout(function () { | ||
callback.apply(mod, [null].concat(datas)); | ||
mock(mod, method, thenify(function() { | ||
const callback = getCallback(arguments); | ||
setTimeout(function() { | ||
callback.apply(mod, [ null ].concat(datas)); | ||
}, timeout); | ||
@@ -145,5 +147,6 @@ })); | ||
* @param {Object} data, return data. | ||
* @param {Number} [tiemout], mock async callback timeout, default is 10. | ||
* @param {Number} timeout, mock async callback timeout, default is 10. | ||
* @return {mm} this - mm | ||
*/ | ||
exports.data = function (mod, method, data, timeout) { | ||
exports.data = function(mod, method, data, timeout) { | ||
return exports.datas(mod, method, [ data ], timeout); | ||
@@ -157,5 +160,6 @@ }; | ||
* @param {String} method, mock module object method name. | ||
* @param {Number} [tiemout], mock async callback timeout, default is 10. | ||
* @param {Number} [timeout], mock async callback timeout, default is 10. | ||
* @return {mm} this - mm | ||
*/ | ||
exports.empty = function (mod, method, timeout) { | ||
exports.empty = function(mod, method, timeout) { | ||
return exports.datas(mod, method, null, timeout); | ||
@@ -172,5 +176,5 @@ }; | ||
*/ | ||
exports.syncError = function (mod, method, error, props) { | ||
exports.syncError = function(mod, method, error, props) { | ||
error = exports._createError(error, props); | ||
mock(mod, method, function () { | ||
mock(mod, method, function() { | ||
throw error; | ||
@@ -187,4 +191,4 @@ }); | ||
*/ | ||
exports.syncData = function (mod, method, data) { | ||
mock(mod, method, function () { | ||
exports.syncData = function(mod, method, data) { | ||
mock(mod, method, function() { | ||
return data; | ||
@@ -200,3 +204,3 @@ }); | ||
*/ | ||
exports.syncEmpty = function (mod, method) { | ||
exports.syncEmpty = function(mod, method) { | ||
exports.syncData(mod, method); | ||
@@ -217,8 +221,8 @@ }; | ||
function matchURL(options, params) { | ||
var url = params && params.url || params; | ||
var host = params && params.host; | ||
const url = params && params.url || params; | ||
const host = params && params.host; | ||
var pathname = options.path || options.pathname; | ||
var hostname = options.host || options.hostname; | ||
var match = false; | ||
const pathname = options.path || options.pathname; | ||
const hostname = options.host || options.hostname; | ||
let match = false; | ||
if (pathname) { | ||
@@ -241,9 +245,9 @@ if (!url) { | ||
function mockRequest() { | ||
var req = new EventEmitter(); | ||
req.write = function () {}; | ||
req.end = function () {}; | ||
req.abort = function () { | ||
const req = new EventEmitter(); | ||
req.write = function() {}; | ||
req.end = function() {}; | ||
req.abort = function() { | ||
req._aborted = true; | ||
process.nextTick(function () { | ||
var err = new Error('socket hang up'); | ||
process.nextTick(function() { | ||
const err = new Error('socket hang up'); | ||
err.code = 'ECONNRESET'; | ||
@@ -264,4 +268,5 @@ req.emit('error', err); | ||
* @param {Number} [delay], response delay time, default is 10. | ||
* @return {mm} this - mm | ||
*/ | ||
exports.http.request = function (url, data, headers, delay) { | ||
exports.http.request = function(url, data, headers, delay) { | ||
return _request.call(this, http, url, data, headers, delay); | ||
@@ -278,4 +283,5 @@ }; | ||
* @param {Number} [delay], response delay time, default is 10. | ||
* @return {mm} this - mm | ||
*/ | ||
exports.https.request = function (url, data, headers, delay) { | ||
exports.https.request = function(url, data, headers, delay) { | ||
return _request.call(this, https, url, data, headers, delay); | ||
@@ -290,11 +296,11 @@ }; | ||
delay = delay || 0; | ||
getAgent(mod).request = function (options, callback) { | ||
var datas = []; | ||
var stream = null; // read stream | ||
getAgent(mod).request = function(options, callback) { | ||
let datas = []; | ||
let stream = null; // read stream | ||
if (typeof data.read === 'function') { | ||
stream = data; | ||
} else if (!Array.isArray(data)) { | ||
datas = [data]; | ||
datas = [ data ]; | ||
} else { | ||
for (var i = 0; i < data.length; i++) { | ||
for (let i = 0; i < data.length; i++) { | ||
datas.push(data[i]); | ||
@@ -304,3 +310,3 @@ } | ||
var match = matchURL(options, url); | ||
const match = matchURL(options, url); | ||
if (!match) { | ||
@@ -310,3 +316,3 @@ return getAgent(mod).__sourceRequest(options, callback); | ||
var req = mockRequest(); | ||
const req = mockRequest(); | ||
@@ -317,3 +323,3 @@ if (callback) { | ||
var res; | ||
let res; | ||
if (stream) { | ||
@@ -323,3 +329,3 @@ res = stream; | ||
res = new EventEmitter(); | ||
res.setEncoding = function (charset) { | ||
res.setEncoding = function(charset) { | ||
res.charset = charset; | ||
@@ -332,4 +338,4 @@ }; | ||
var ondata = function () { | ||
var chunk = datas.shift(); | ||
const ondata = function() { | ||
let chunk = datas.shift(); | ||
if (!chunk) { | ||
@@ -354,3 +360,3 @@ if (!req._aborted) { | ||
setTimeout(function () { | ||
setTimeout(function() { | ||
if (!req._aborted) { | ||
@@ -377,3 +383,3 @@ req.emit('response', res); | ||
*/ | ||
exports.http.requestError = function (url, reqError, resError, delay) { | ||
exports.http.requestError = function(url, reqError, resError, delay) { | ||
_requestError.call(this, http, url, reqError, resError, delay); | ||
@@ -389,3 +395,3 @@ }; | ||
*/ | ||
exports.https.requestError = function (url, reqError, resError, delay) { | ||
exports.https.requestError = function(url, reqError, resError, delay) { | ||
_requestError.call(this, https, url, reqError, resError, delay); | ||
@@ -407,4 +413,4 @@ }; | ||
} | ||
getAgent(mod).request = function (options, callback) { | ||
var match = matchURL(options, url); | ||
getAgent(mod).request = function(options, callback) { | ||
const match = matchURL(options, url); | ||
if (!match) { | ||
@@ -414,3 +420,3 @@ return getAgent(mod).__sourceRequest(options, callback); | ||
var req = mockRequest(); | ||
const req = mockRequest(); | ||
@@ -421,3 +427,3 @@ if (callback) { | ||
setTimeout(function () { | ||
setTimeout(function() { | ||
if (reqError) { | ||
@@ -427,8 +433,8 @@ return req.emit('error', reqError); | ||
var res = new EventEmitter(); | ||
const res = new EventEmitter(); | ||
res.statusCode = 200; | ||
res.headers = { | ||
server: 'MockMateServer' | ||
server: 'MockMateServer', | ||
}; | ||
process.nextTick(function () { | ||
process.nextTick(function() { | ||
if (!req._aborted) { | ||
@@ -451,12 +457,12 @@ req.emit('error', resError); | ||
* @param {Integer} code exit code | ||
* @param {String} stdout | ||
* @param {String} stderr | ||
* @param {String} stdout stdout | ||
* @param {String} stderr stderr | ||
* @param {Integer} timeout stdout/stderr/close event emit timeout | ||
*/ | ||
exports.spawn = function (code, stdout, stderr, timeout) { | ||
var evt = new EventEmitter(); | ||
mock(cp, 'spawn', function () { | ||
exports.spawn = function(code, stdout, stderr, timeout) { | ||
const evt = new EventEmitter(); | ||
mock(cp, 'spawn', function() { | ||
return evt; | ||
}); | ||
setTimeout(function () { | ||
setTimeout(function() { | ||
stdout && evt.emit('stdout', stdout); | ||
@@ -471,4 +477,5 @@ stderr && evt.emit('stderr', stderr); | ||
* remove all mock effects. | ||
* @return {mm} this - mm | ||
*/ | ||
exports.restore = function () { | ||
exports.restore = function() { | ||
getAgent(http).request = getAgent(http).__sourceRequest; | ||
@@ -482,3 +489,3 @@ getAgent(https).request = getAgent(https).__sourceRequest; | ||
const newObj = {}; | ||
for (var k in obj) { | ||
for (const k in obj) { | ||
if (k !== key) { | ||
@@ -485,0 +492,0 @@ newObj[k] = obj[k]; |
{ | ||
"name": "mm", | ||
"version": "1.5.1", | ||
"version": "2.0.0", | ||
"description": "mock mate, mock http request, fs access and so on.", | ||
@@ -11,5 +11,5 @@ "main": "index.js", | ||
"scripts": { | ||
"test": "npm run lint && mocha -R spec -t 5000 -r should-http -r co-mocha test/*.test.js", | ||
"test-cov": "npm run lint && istanbul cover node_modules/.bin/_mocha -- -t 5000 -r should-http -r co-mocha test/*.test.js", | ||
"lint": "eslint lib test index.js", | ||
"test": "npm run lint && mocha -R spec -t 5000 -r should-http -r thunk-mocha test/*.test.js", | ||
"test-cov": "npm run lint && istanbul cover node_modules/.bin/_mocha -- -t 5000 -r should-http -r thunk-mocha test/*.test.js", | ||
"lint": "eslint --fix lib test index.js", | ||
"autod": "autod -w --prefix '~'" | ||
@@ -20,6 +20,6 @@ }, | ||
"enable": "~3.3.0", | ||
"is-type-of": "~0.3.1", | ||
"is-type-of": "^1.0.0", | ||
"muk": "^0.5.0", | ||
"semver": "~4.3.4", | ||
"thenify": "~3.1.0" | ||
"thenify": "^3.1.0" | ||
}, | ||
@@ -30,6 +30,7 @@ "devDependencies": { | ||
"co": "4", | ||
"co-mocha": "*", | ||
"eslint": "1", | ||
"thunk-mocha": "1", | ||
"eslint-config-egg": "3", | ||
"eslint": "3", | ||
"istanbul": "0", | ||
"mocha": "*", | ||
"mocha": "beta", | ||
"mocha-lcov-reporter": "*", | ||
@@ -54,3 +55,3 @@ "node-patch": "*", | ||
"engines": { | ||
"node": ">=0.12.0" | ||
"node": ">=4" | ||
}, | ||
@@ -57,0 +58,0 @@ "author": "fengmk2 <fengmk2@gmail.com> (http://fengmk2.com)", |
@@ -23,2 +23,4 @@ mm | ||
__mm@2 only support node v4.0+.__ | ||
## Install | ||
@@ -25,0 +27,0 @@ |
24376
474
266
14
+ Addedany-promise@1.3.0(transitive)
+ Addedis-class-hotfix@0.0.6(transitive)
+ Addedis-type-of@1.4.0(transitive)
+ Addedthenify@3.3.1(transitive)
- Removedis-type-of@0.3.1(transitive)
- Removednative-or-bluebird@1.2.0(transitive)
- Removedthenify@3.1.1(transitive)
Updatedis-type-of@^1.0.0
Updatedthenify@^3.1.0