Comparing version 0.2.5 to 0.2.6
@@ -1,1 +0,167 @@ | ||
var v=Object.getOwnPropertySymbols;var w=Object.prototype.hasOwnProperty,K=Object.prototype.propertyIsEnumerable;var A=(t,e)=>{var o={};for(var r in t)w.call(t,r)&&e.indexOf(r)<0&&(o[r]=t[r]);if(t!=null&&v)for(var r of v(t))e.indexOf(r)<0&&K.call(t,r)&&(o[r]=t[r]);return o};function f(t,e){if(!t)throw new Error(e)}function a(t,e){return typeof e===t}var c=new Set;function O(t){f(a("function",t)||a("undefined",t),"cannot spy on a non-function value");let e=function(...r){if(e.called=!0,e.callCount++,e.calls.push(r),e.next){let[s,d]=e.next;if(e.results.push(e.next),e.next=null,s==="ok")return d;throw d}let n,l="ok";if(e.impl)try{n=e.impl.apply(this,r),l="ok"}catch(s){n=s,l="error"}let u=[l,n];return n&&a("object",n)&&a("function",n.then)&&(n=n.then(s=>u[1]=s).catch(s=>{throw u[0]="error",u[1]=s,s})),e.results.push(u),n};Object.defineProperty(e,"length",{value:t?t.length:0}),Object.defineProperty(e,"__isSpy",{value:!0}),Object.defineProperty(e,"returns",{get(){return this.results.map(([,r])=>r)}}),Object.defineProperty(e,"name",{value:t&&t.name||"spy"});let o=()=>{e.called=!1,e.callCount=0,e.results=[],e.calls=[]};return o(),e.impl=t,e.reset=o,e.nextError=r=>(e.next=["error",r],e),e.nextResult=r=>(e.next=["ok",r],e),e}var k=(t,e)=>Object.getOwnPropertyDescriptor(t,e);function E(t,e,o){f(!a("undefined",t),"spyOn could not find an object to spy upon"),f(a("object",t)||a("function",t),"cannot spyOn on a primitive value");let r=()=>typeof e=="string"?[e,"value"]:"getter"in e?[e.getter,"get"]:[e.setter,"set"],[n,l]=r(),u=k(t,n),s=Object.getPrototypeOf(t),d=s&&k(s,n),p=u||d;f(p||n in t,`${n} does not exist`);let R=!1;l==="value"&&p&&!p.value&&p.get&&(l="get",R=!0,o=p.get());let y;p?y=p[l]:l!=="value"?y=()=>t[n]:y=t[n],o||(o=y);let i=O(o),h=T=>{let S=p||{configurable:!0,writable:!0},{value:F}=S,m=A(S,["value"]);l!=="value"&&delete m.writable,m[l]=T,Object.defineProperty(u||!p?t:s,n,m)},P=()=>h(y);return i.restore=P,i.getOriginal=()=>R?y():y,i.willCall=T=>(i.impl=T,i),h(R?()=>i:i),c.add(i),i}function q(){for(let t of c)t.restore();c.clear()}export{q as restoreAll,c as spies,O as spy,E as spyOn}; | ||
var __getOwnPropSymbols = Object.getOwnPropertySymbols; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __propIsEnum = Object.prototype.propertyIsEnumerable; | ||
var __objRest = (source, exclude) => { | ||
var target = {}; | ||
for (var prop in source) | ||
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0) | ||
target[prop] = source[prop]; | ||
if (source != null && __getOwnPropSymbols) | ||
for (var prop of __getOwnPropSymbols(source)) { | ||
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop)) | ||
target[prop] = source[prop]; | ||
} | ||
return target; | ||
}; | ||
// src/utils.ts | ||
function assert(condition, message) { | ||
if (!condition) { | ||
throw new Error(message); | ||
} | ||
} | ||
function isType(type, value) { | ||
return typeof value === type; | ||
} | ||
// src/spy.ts | ||
var spies = /* @__PURE__ */ new Set(); | ||
function spy(cb) { | ||
assert(isType("function", cb) || isType("undefined", cb), "cannot spy on a non-function value"); | ||
let fn = function(...args) { | ||
fn.called = true; | ||
fn.callCount++; | ||
fn.calls.push(args); | ||
if (fn.next) { | ||
let [type2, result2] = fn.next; | ||
fn.results.push(fn.next); | ||
fn.next = null; | ||
if (type2 === "ok") { | ||
return result2; | ||
} | ||
throw result2; | ||
} | ||
let result; | ||
let type = "ok"; | ||
if (fn.impl) { | ||
try { | ||
result = fn.impl.apply(this, args); | ||
type = "ok"; | ||
} catch (err) { | ||
result = err; | ||
type = "error"; | ||
} | ||
} | ||
let resultTuple = [type, result]; | ||
if (result && isType("object", result) && isType("function", result.then)) { | ||
result = result.then((r) => resultTuple[1] = r).catch((e) => { | ||
resultTuple[0] = "error"; | ||
resultTuple[1] = e; | ||
throw e; | ||
}); | ||
} | ||
fn.results.push(resultTuple); | ||
return result; | ||
}; | ||
Object.defineProperty(fn, "length", { value: cb ? cb.length : 0 }); | ||
Object.defineProperty(fn, "__isSpy", { value: true }); | ||
Object.defineProperty(fn, "returns", { | ||
get() { | ||
return this.results.map(([, r]) => r); | ||
} | ||
}); | ||
Object.defineProperty(fn, "name", { value: cb ? cb.name || "spy" : "spy" }); | ||
const reset = () => { | ||
fn.called = false; | ||
fn.callCount = 0; | ||
fn.results = []; | ||
fn.calls = []; | ||
}; | ||
reset(); | ||
fn.impl = cb; | ||
fn.reset = reset; | ||
fn.nextError = (error) => { | ||
fn.next = ["error", error]; | ||
return fn; | ||
}; | ||
fn.nextResult = (result) => { | ||
fn.next = ["ok", result]; | ||
return fn; | ||
}; | ||
return fn; | ||
} | ||
// src/spyOn.ts | ||
var getDescriptor = (obj, method) => Object.getOwnPropertyDescriptor(obj, method); | ||
function spyOn(obj, methodName, mock) { | ||
assert(!isType("undefined", obj), "spyOn could not find an object to spy upon"); | ||
assert(isType("object", obj) || isType("function", obj), "cannot spyOn on a primitive value"); | ||
let getMeta = () => { | ||
if (typeof methodName === "string") { | ||
return [methodName, "value"]; | ||
} | ||
if ("getter" in methodName) { | ||
return [methodName.getter, "get"]; | ||
} | ||
return [methodName.setter, "set"]; | ||
}; | ||
let [accessName, accessType] = getMeta(); | ||
let objDescriptor = getDescriptor(obj, accessName); | ||
let proto = Object.getPrototypeOf(obj); | ||
let protoDescriptor = proto && getDescriptor(proto, accessName); | ||
let descriptor = objDescriptor || protoDescriptor; | ||
assert(descriptor || accessName in obj, `${accessName} does not exist`); | ||
let ssr = false; | ||
if (accessType === "value" && descriptor && !descriptor.value && descriptor.get) { | ||
accessType = "get"; | ||
ssr = true; | ||
mock = descriptor.get(); | ||
} | ||
let origin; | ||
if (descriptor) { | ||
origin = descriptor[accessType]; | ||
} else if (accessType !== "value") { | ||
origin = () => obj[accessName]; | ||
} else { | ||
origin = obj[accessName]; | ||
} | ||
if (!mock) | ||
mock = origin; | ||
let fn = spy(mock); | ||
let define = (cb) => { | ||
let _a = descriptor || { | ||
configurable: true, | ||
writable: true | ||
}, { value } = _a, desc = __objRest(_a, ["value"]); | ||
if (accessType !== "value") { | ||
delete desc.writable; | ||
} | ||
; | ||
desc[accessType] = cb; | ||
Object.defineProperty(objDescriptor || !descriptor ? obj : proto, accessName, desc); | ||
}; | ||
let restore = () => define(origin); | ||
fn.restore = restore; | ||
fn.getOriginal = () => ssr ? origin() : origin; | ||
fn.willCall = (newCb) => { | ||
fn.impl = newCb; | ||
return fn; | ||
}; | ||
define(ssr ? () => fn : fn); | ||
spies.add(fn); | ||
return fn; | ||
} | ||
// src/restoreAll.ts | ||
function restoreAll() { | ||
for (let fn of spies) { | ||
fn.restore(); | ||
} | ||
spies.clear(); | ||
} | ||
export { | ||
restoreAll, | ||
spies, | ||
spy, | ||
spyOn | ||
}; |
{ | ||
"name": "tinyspy", | ||
"version": "0.2.5", | ||
"version": "0.2.6", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "main": "./dist/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
7081
205