Comparing version 3.0.1 to 3.0.2
3.0.2 / 2020-03-01 | ||
================== | ||
**fixes** | ||
* [[`0e310b4`](http://github.com/node-modules/mm/commit/0e310b4d6f3d5511f4e86971413014fe5f496ce7)] - fix: do function type check after execute (#49) (Yiyu He <<dead_horse@qq.com>>) | ||
3.0.1 / 2020-03-01 | ||
@@ -3,0 +9,0 @@ ================== |
@@ -14,17 +14,7 @@ 'use strict'; | ||
const mock = module.exports = function mock(target, property, value) { | ||
checkFunctionType(target, property, value); | ||
value = spyFunction(value); | ||
value = spyFunction(target, property, 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) { | ||
function spyFunction(target, property, fn) { | ||
if (!is.function(fn)) return fn; | ||
@@ -55,2 +45,4 @@ if (is.asyncFunction(fn)) { | ||
// don't allow mock async function to common function | ||
const isAsyncLike = isAsyncLikeFunction(target, property); | ||
const spy = function(...args) { | ||
@@ -62,3 +54,7 @@ spy.called = spy.called || 0; | ||
spy.called++; | ||
return fn.call(this, ...args); | ||
const res = fn.call(this, ...args); | ||
if (isAsyncLike && !is.promise(res)) { | ||
throw new Error(`Can\'t mock async function to normal function for property "${property}"`); | ||
} | ||
return res; | ||
}; | ||
@@ -68,2 +64,9 @@ return spy; | ||
function isAsyncLikeFunction(target, property) { | ||
// don't call getter | ||
// Object.getOwnPropertyDescriptor can't find getter in prototypes | ||
if (target.__lookupGetter__(property)) return false; | ||
return is.asyncFunction(target[property]) || is.generatorFunction(target[property]); | ||
} | ||
exports = mock; | ||
@@ -70,0 +73,0 @@ |
{ | ||
"name": "mm", | ||
"version": "3.0.1", | ||
"version": "3.0.2", | ||
"description": "mock mate, mock http request, fs access and so on.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
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
34596
639