Comparing version 2.5.0 to 3.0.0
3.0.0 / 2020-03-01 | ||
================== | ||
**features** | ||
* [[`f49c32e`](http://github.com/node-modules/mm/commit/f49c32e84fdef24655fdc25289d7aeb2e9560c0c)] - feat: spy function by default (#47) (Yiyu He <<dead_horse@qq.com>>) | ||
* [[`d8d9aa3`](http://github.com/node-modules/mm/commit/d8d9aa3562249e767eb7f0cb13bb325a9dbac91b)] - feat: [BREAKING] don't allow to mock async function to normal function (#46) (fengmk2 <<fengmk2@gmail.com>>) | ||
**others** | ||
* [[`eec0876`](http://github.com/node-modules/mm/commit/eec08767b681bc7e3d6d69cdfa7049b22d716239)] - chore: fix errorOnce (fengmk2 <<fengmk2@gmail.com>>) | ||
2.5.0 / 2019-03-06 | ||
@@ -3,0 +13,0 @@ ================== |
@@ -10,3 +10,5 @@ 'use strict'; | ||
mm.datas = function(mod, method, datas, timeout) { | ||
if (!is.generatorFunction(mod[method])) { | ||
const isGeneratorFunction = is.generatorFunction(mod[method]); | ||
const isAsyncFunction = is.asyncFunction(mod[method]); | ||
if (!isGeneratorFunction && !isAsyncFunction) { | ||
return mockDatas.call(mm, mod, method, datas, timeout); | ||
@@ -19,6 +21,13 @@ } | ||
timeout = timeout || 0; | ||
mm(mod, method, function* () { | ||
yield sleep(timeout); | ||
return datas; | ||
}); | ||
if (isGeneratorFunction) { | ||
mm(mod, method, function* () { | ||
yield sleep(timeout); | ||
return datas; | ||
}); | ||
} else { | ||
mm(mod, method, async function() { | ||
await sleep(timeout); | ||
return datas; | ||
}); | ||
} | ||
return this; | ||
@@ -29,3 +38,3 @@ }; | ||
mm.data = function(mod, method, data, timeout) { | ||
if (!is.generatorFunction(mod[method])) { | ||
if (!is.generatorFunction(mod[method]) && !is.asyncFunction(mod[method])) { | ||
return mockData.call(mm, mod, method, data, timeout); | ||
@@ -32,0 +41,0 @@ } |
'use strict'; | ||
const EventEmitter = require('events'); | ||
const is = require('is-type-of'); | ||
const muk = require('muk-prop'); | ||
@@ -12,6 +13,54 @@ const http = require('http'); | ||
const mock = module.exports = function mock() { | ||
return muk.apply(null, arguments); | ||
const mock = module.exports = function mock(target, property, value) { | ||
checkFunctionType(target, property, value); | ||
value = spyFunction(value); | ||
return muk(target, property, value); | ||
}; | ||
function checkFunctionType(target, property, value) { | ||
if (typeof target[property] === 'function' && typeof value === 'function') { | ||
// don't mock async function to normal function | ||
if (is.asyncFunction(target[property]) && !is.asyncFunction(value)) { | ||
throw TypeError('Can\'t mock async function to normal function'); | ||
} | ||
} | ||
} | ||
function spyFunction(fn) { | ||
if (!is.function(fn)) return fn; | ||
if (is.asyncFunction(fn)) { | ||
const spy = async function(...args) { | ||
spy.called = spy.called || 0; | ||
spy.calledArguments = spy.calledArguments || []; | ||
spy.calledArguments.push(args); | ||
spy.lastCalledArguments = args; | ||
spy.called++; | ||
return await fn(...args); | ||
}; | ||
return spy; | ||
} | ||
if (is.generatorFunction(fn)) { | ||
const spy = function* (...args) { | ||
spy.called = spy.called || 0; | ||
spy.calledArguments = spy.calledArguments || []; | ||
spy.calledArguments.push(args); | ||
spy.lastCalledArguments = args; | ||
spy.called++; | ||
return yield fn(...args); | ||
}; | ||
return spy; | ||
} | ||
const spy = function(...args) { | ||
spy.called = spy.called || 0; | ||
spy.calledArguments = spy.calledArguments || []; | ||
spy.calledArguments.push(args); | ||
spy.lastCalledArguments = args; | ||
spy.called++; | ||
return fn(...args); | ||
}; | ||
return spy; | ||
} | ||
exports = mock; | ||
@@ -18,0 +67,0 @@ |
{ | ||
"name": "mm", | ||
"version": "2.5.0", | ||
"version": "3.0.0", | ||
"description": "mock mate, mock http request, fs access and so on.", | ||
@@ -18,22 +18,22 @@ "main": "index.js", | ||
"dependencies": { | ||
"is-type-of": "^1.0.0", | ||
"ko-sleep": "^1.0.2", | ||
"muk-prop": "^1.0.0", | ||
"thenify": "^3.2.1" | ||
"is-type-of": "^1.2.1", | ||
"ko-sleep": "^1.0.3", | ||
"muk-prop": "^1.2.1", | ||
"thenify": "^3.3.0" | ||
}, | ||
"devDependencies": { | ||
"autod": "^2.8.0", | ||
"autod": "^3.1.0", | ||
"chunkstream": "^0.0.1", | ||
"co": "^4.6.0", | ||
"egg-bin": "^1.10.3", | ||
"egg-ci": "^1.9.1", | ||
"enable": "^3.3.0", | ||
"eslint": "^3.18.0", | ||
"eslint-config-egg": "^3.2.0", | ||
"egg-bin": "^1.11.1", | ||
"egg-ci": "^1.13.1", | ||
"enable": "^3.4.0", | ||
"eslint": "^6.8.0", | ||
"eslint-config-egg": "^8.0.1", | ||
"node-patch": "*", | ||
"pedding": "^1.1.0", | ||
"should": "^11.2.1", | ||
"should": "^13.2.3", | ||
"thunkify-wrap": "^1.0.4", | ||
"urllib": "^2.21.2", | ||
"uuid": "^3.1.0" | ||
"urllib": "^2.34.2", | ||
"uuid": "^3.4.0" | ||
}, | ||
@@ -52,10 +52,10 @@ "homepage": "http://github.com/node-modules/mm", | ||
"engines": { | ||
"node": ">=4.0.0" | ||
"node": ">=8.0.0" | ||
}, | ||
"ci": { | ||
"type": "travis, azure-pipelines", | ||
"version": "4, 6, 8, 10, 11" | ||
"version": "8, 10, 12, 13" | ||
}, | ||
"author": "fengmk2 <fengmk2@gmail.com> (http://fengmk2.com)", | ||
"author": "fengmk2 <fengmk2@gmail.com> (https://fengmk2.com)", | ||
"license": "MIT" | ||
} |
@@ -50,2 +50,27 @@ mm | ||
### Support spy | ||
If mocked property is a function, it will be spied, every time it called, mm will modify `.called`, `.calledArguments` and `.lastCalledArguments`. For example: | ||
```js | ||
const target = { | ||
async add(a, b) { | ||
return a + b; | ||
}, | ||
}; | ||
mm.data(target, 'add', 3); | ||
(await target.add(1, 1)).should.equal(3); | ||
target.add.called.should.equal(1); | ||
target.add.calledArguments.should.eql([[ 1, 1 ]]); | ||
target.add.lastCalledArguments.should.eql([ 1, 1 ]); | ||
(await target.add(2, 2)).should.equal(3); | ||
target.add.called.should.equal(2); | ||
target.add.calledArguments.should.eql([[ 1, 1 ], [ 2, 2 ]]); | ||
target.add.lastCalledArguments.should.eql([ 2, 2 ]); | ||
}); | ||
``` | ||
### Support generator function | ||
@@ -99,3 +124,3 @@ | ||
mm.errorOne(fs, 'readFile', 'mock fs.readFile return error'); | ||
mm.errorOnce(fs, 'readFile', 'mock fs.readFile return error'); | ||
@@ -102,0 +127,0 @@ fs.readFile('/etc/hosts', 'utf8', function (err, content) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
33899
636
313
Updatedis-type-of@^1.2.1
Updatedko-sleep@^1.0.3
Updatedmuk-prop@^1.2.1
Updatedthenify@^3.3.0