tinyspy
Advanced tools
Comparing version 2.1.1 to 2.2.0
@@ -26,3 +26,3 @@ declare const S: unique symbol; | ||
impl: ((...args: A) => R) | undefined; | ||
next: ResultFn<R> | null; | ||
next: ResultFn<R>[]; | ||
} | ||
@@ -29,0 +29,0 @@ interface SpyInternalImplState<A extends any[] = any[], R = any> extends SpyInternalState<A, R> { |
// src/utils.ts | ||
function g(e, t) { | ||
function m(e, t) { | ||
if (!e) | ||
throw new Error(t); | ||
} | ||
function i(e, t) { | ||
function y(e, t) { | ||
return typeof t === e; | ||
@@ -12,51 +12,54 @@ } | ||
} | ||
function f(e, t, r) { | ||
Object.defineProperty(e, t, r); | ||
function d(e, t, n) { | ||
Object.defineProperty(e, t, n); | ||
} | ||
function l(e, t, r) { | ||
Object.defineProperty(e, t, { value: r }); | ||
function p(e, t, n) { | ||
Object.defineProperty(e, t, { value: n }); | ||
} | ||
// src/constants.ts | ||
var y = Symbol.for("tinyspy:spy"); | ||
var u = Symbol.for("tinyspy:spy"); | ||
// src/internal.ts | ||
var m = /* @__PURE__ */ new Set(), M = (e) => { | ||
e.called = !1, e.callCount = 0, e.calls = [], e.results = []; | ||
}, C = (e) => (f(e, y, { value: { reset: () => M(e[y]) } }), e[y]), A = (e) => e[y] || C(e); | ||
function I(e) { | ||
g(i("function", e) || i("undefined", e), "cannot spy on a non-function value"); | ||
let t = function(...o) { | ||
let n = A(t); | ||
if (n.called = !0, n.callCount++, n.calls.push(o), n.next) { | ||
let [p, s] = n.next; | ||
if (n.results.push(n.next), n.next = null, p === "ok") | ||
return s; | ||
throw s; | ||
var I = /* @__PURE__ */ new Set(), M = (e) => { | ||
e.called = !1, e.callCount = 0, e.calls = [], e.results = [], e.next = []; | ||
}, C = (e) => (d(e, u, { value: { reset: () => M(e[u]) } }), e[u]), v = (e) => e[u] || C(e); | ||
function T(e) { | ||
m(y("function", e) || y("undefined", e), "cannot spy on a non-function value"); | ||
let t = function(...a) { | ||
let r = v(t); | ||
r.called = !0, r.callCount++, r.calls.push(a); | ||
let i = r.next.shift(); | ||
if (i) { | ||
r.results.push(i); | ||
let [s, l] = i; | ||
if (s === "ok") | ||
return l; | ||
throw l; | ||
} | ||
let a, d = "ok"; | ||
if (n.impl) | ||
let o, c = "ok"; | ||
if (r.impl) | ||
try { | ||
a = n.impl.apply(this, o), d = "ok"; | ||
} catch (p) { | ||
throw a = p, d = "error", n.results.push([d, p]), p; | ||
new.target ? o = Reflect.construct(r.impl, a, new.target) : o = r.impl.apply(this, a), c = "ok"; | ||
} catch (s) { | ||
throw o = s, c = "error", r.results.push([c, s]), s; | ||
} | ||
let u = [d, a]; | ||
if (b(a)) { | ||
let p = a.then((s) => u[1] = s).catch((s) => { | ||
throw u[0] = "error", u[1] = s, s; | ||
let x = [c, o]; | ||
if (b(o)) { | ||
let s = o.then((l) => x[1] = l).catch((l) => { | ||
throw x[0] = "error", x[1] = l, l; | ||
}); | ||
Object.assign(p, a), a = p; | ||
Object.assign(s, o), o = s; | ||
} | ||
return n.results.push(u), a; | ||
return r.results.push(x), o; | ||
}; | ||
l(t, "_isMockFunction", !0), l(t, "length", e ? e.length : 0), l(t, "name", e && e.name || "spy"); | ||
let r = A(t); | ||
return r.reset(), r.impl = e, t; | ||
p(t, "_isMockFunction", !0), p(t, "length", e ? e.length : 0), p(t, "name", e && e.name || "spy"); | ||
let n = v(t); | ||
return n.reset(), n.impl = e, t; | ||
} | ||
function v(e) { | ||
let t = A(e); | ||
f(e, "returns", { | ||
get: () => t.results.map(([, r]) => r) | ||
}), ["called", "callCount", "results", "calls", "reset", "impl"].forEach((r) => f(e, r, { get: () => t[r], set: (o) => t[r] = o })), l(e, "nextError", (r) => (t.next = ["error", r], t)), l(e, "nextResult", (r) => (t.next = ["ok", r], t)); | ||
function h(e) { | ||
let t = v(e); | ||
d(e, "returns", { | ||
get: () => t.results.map(([, n]) => n) | ||
}), ["called", "callCount", "results", "calls", "reset", "impl"].forEach((n) => d(e, n, { get: () => t[n], set: (a) => t[n] = a })), p(e, "nextError", (n) => (t.next.push(["error", n]), t)), p(e, "nextResult", (n) => (t.next.push(["ok", n]), t)); | ||
} | ||
@@ -66,4 +69,4 @@ | ||
function z(e) { | ||
let t = I(e); | ||
return v(t), t; | ||
let t = T(e); | ||
return h(t), t; | ||
} | ||
@@ -73,6 +76,6 @@ | ||
var P = (e, t) => Object.getOwnPropertyDescriptor(e, t); | ||
function E(e, t, r) { | ||
g(!i("undefined", e), "spyOn could not find an object to spy upon"), g(i("object", e) || i("function", e), "cannot spyOn on a primitive value"); | ||
let o = () => { | ||
if (!i("object", t)) | ||
function E(e, t, n) { | ||
m(!y("undefined", e), "spyOn could not find an object to spy upon"), m(y("object", e) || y("function", e), "cannot spyOn on a primitive value"); | ||
let a = () => { | ||
if (!y("object", t)) | ||
return [t, "value"]; | ||
@@ -86,9 +89,9 @@ if ("getter" in t && "setter" in t) | ||
throw new Error("specify getter or setter to spy on"); | ||
}, [n, a] = o(), d = P(e, n), u = Object.getPrototypeOf(e), p = u && P(u, n), s = d || p; | ||
g(s || n in e, `${String(n)} does not exist`); | ||
let w = !1; | ||
a === "value" && s && !s.value && s.get && (a = "get", w = !0, r = s.get()); | ||
let c; | ||
s ? c = s[a] : a !== "value" ? c = () => e[n] : c = e[n], r || (r = c); | ||
let x = I(r), O = (h) => { | ||
}, [r, i] = a(), o = P(e, r), c = Object.getPrototypeOf(e), x = c && P(c, r), s = o || x; | ||
m(s || r in e, `${String(r)} does not exist`); | ||
let l = !1; | ||
i === "value" && s && !s.value && s.get && (i = "get", l = !0, n = s.get()); | ||
let f; | ||
s ? f = s[i] : i !== "value" ? f = () => e[r] : f = e[r], n || (n = f); | ||
let S = T(n), O = (w) => { | ||
let { value: G, ...k } = s || { | ||
@@ -98,11 +101,11 @@ configurable: !0, | ||
}; | ||
a !== "value" && delete k.writable, k[a] = h, f(e, n, k); | ||
}, K = () => s ? f(e, n, s) : O(c), T = x[y]; | ||
return l(T, "restore", K), l(T, "getOriginal", () => w ? c() : c), l(T, "willCall", (h) => (T.impl = h, x)), O(w ? () => x : x), m.add(x), x; | ||
i !== "value" && delete k.writable, k[i] = w, d(e, r, k); | ||
}, K = () => s ? d(e, r, s) : O(f), A = S[u]; | ||
return p(A, "restore", K), p(A, "getOriginal", () => l ? f() : f), p(A, "willCall", (w) => (A.impl = w, S)), O(l ? () => S : S), I.add(S), S; | ||
} | ||
function W(e, t, r) { | ||
let o = E(e, t, r); | ||
return v(o), ["restore", "getOriginal", "willCall"].forEach((n) => { | ||
l(o, n, o[y][n]); | ||
}), o; | ||
function W(e, t, n) { | ||
let a = E(e, t, n); | ||
return h(a), ["restore", "getOriginal", "willCall"].forEach((r) => { | ||
p(a, r, a[u][r]); | ||
}), a; | ||
} | ||
@@ -112,14 +115,14 @@ | ||
function Z() { | ||
for (let e of m) | ||
for (let e of I) | ||
e.restore(); | ||
m.clear(); | ||
I.clear(); | ||
} | ||
export { | ||
I as createInternalSpy, | ||
A as getInternalState, | ||
T as createInternalSpy, | ||
v as getInternalState, | ||
E as internalSpyOn, | ||
Z as restoreAll, | ||
m as spies, | ||
I as spies, | ||
z as spy, | ||
W as spyOn | ||
}; |
{ | ||
"name": "tinyspy", | ||
"version": "2.1.1", | ||
"version": "2.2.0", | ||
"type": "module", | ||
"packageManager": "pnpm@8.4.0", | ||
"main": "./dist/index.cjs", | ||
@@ -6,0 +7,0 @@ "module": "./dist/index.js", |
Sorry, the diff of this file is not supported yet
13568
339