tinyspy
Advanced tools
Comparing version 2.2.0 to 2.2.1
// src/utils.ts | ||
function m(e, t) { | ||
function R(e, t) { | ||
if (!e) | ||
throw new Error(t); | ||
} | ||
function y(e, t) { | ||
function u(e, t) { | ||
return typeof t === e; | ||
@@ -12,6 +12,6 @@ } | ||
} | ||
function d(e, t, n) { | ||
function f(e, t, n) { | ||
Object.defineProperty(e, t, n); | ||
} | ||
function p(e, t, n) { | ||
function i(e, t, n) { | ||
Object.defineProperty(e, t, { value: n }); | ||
@@ -21,46 +21,46 @@ } | ||
// src/constants.ts | ||
var u = Symbol.for("tinyspy:spy"); | ||
var c = Symbol.for("tinyspy:spy"); | ||
// src/internal.ts | ||
var I = /* @__PURE__ */ new Set(), M = (e) => { | ||
var g = /* @__PURE__ */ new Set(), C = (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; | ||
}, M = (e) => (f(e, c, { value: { reset: () => C(e[c]) } }), e[c]), A = (e) => e[c] || M(e); | ||
function I(e) { | ||
R(u("function", e) || u("undefined", e), "cannot spy on a non-function value"); | ||
let t = function(...s) { | ||
let r = A(t); | ||
r.called = !0, r.callCount++, r.calls.push(s); | ||
let m = r.next.shift(); | ||
if (m) { | ||
r.results.push(m); | ||
let [l, o] = m; | ||
if (l === "ok") | ||
return o; | ||
throw o; | ||
} | ||
let o, c = "ok"; | ||
let p, d = "ok"; | ||
if (r.impl) | ||
try { | ||
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; | ||
new.target ? p = Reflect.construct(r.impl, s, new.target) : p = r.impl.apply(this, s), d = "ok"; | ||
} catch (l) { | ||
throw p = l, d = "error", r.results.push([d, l]), l; | ||
} | ||
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; | ||
let a = [d, p]; | ||
if (b(p)) { | ||
let l = p.then((o) => a[1] = o).catch((o) => { | ||
throw a[0] = "error", a[1] = o, o; | ||
}); | ||
Object.assign(s, o), o = s; | ||
Object.assign(l, p), p = l; | ||
} | ||
return r.results.push(x), o; | ||
return r.results.push(a), p; | ||
}; | ||
p(t, "_isMockFunction", !0), p(t, "length", e ? e.length : 0), p(t, "name", e && e.name || "spy"); | ||
let n = v(t); | ||
i(t, "_isMockFunction", !0), i(t, "length", e ? e.length : 0), i(t, "name", e && e.name || "spy"); | ||
let n = A(t); | ||
return n.reset(), n.impl = e, t; | ||
} | ||
function h(e) { | ||
let t = v(e); | ||
d(e, "returns", { | ||
function v(e) { | ||
let t = A(e); | ||
f(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)); | ||
}), ["called", "callCount", "results", "calls", "reset", "impl"].forEach((n) => f(e, n, { get: () => t[n], set: (s) => t[n] = s })), i(e, "nextError", (n) => (t.next.push(["error", n]), t)), i(e, "nextResult", (n) => (t.next.push(["ok", n]), t)); | ||
} | ||
@@ -70,12 +70,14 @@ | ||
function z(e) { | ||
let t = T(e); | ||
return h(t), t; | ||
let t = I(e); | ||
return v(t), t; | ||
} | ||
// src/spyOn.ts | ||
var P = (e, t) => Object.getOwnPropertyDescriptor(e, t); | ||
var k = (e, t) => Object.getOwnPropertyDescriptor(e, t), P = (e, t) => { | ||
t != null && typeof t == "function" && t.prototype != null && Object.setPrototypeOf(e.prototype, t.prototype); | ||
}; | ||
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)) | ||
R(!u("undefined", e), "spyOn could not find an object to spy upon"), R(u("object", e) || u("function", e), "cannot spyOn on a primitive value"); | ||
let [s, r] = (() => { | ||
if (!u("object", t)) | ||
return [t, "value"]; | ||
@@ -89,22 +91,24 @@ if ("getter" in t && "setter" in t) | ||
throw new Error("specify getter or setter to spy on"); | ||
}, [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`); | ||
})(), m = k(e, s), p = Object.getPrototypeOf(e), d = p && k(p, s), a = m || d; | ||
R(a || s in e, `${String(s)} 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 || { | ||
r === "value" && a && !a.value && a.get && (r = "get", l = !0, n = a.get()); | ||
let o; | ||
a ? o = a[r] : r !== "value" ? o = () => e[s] : o = e[s], n || (n = o); | ||
let y = I(n); | ||
r === "value" && P(y, o); | ||
let O = (h) => { | ||
let { value: G, ...w } = a || { | ||
configurable: !0, | ||
writable: !0 | ||
}; | ||
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; | ||
r !== "value" && delete w.writable, w[r] = h, f(e, s, w); | ||
}, K = () => a ? f(e, s, a) : O(o), T = y[c]; | ||
return i(T, "restore", K), i(T, "getOriginal", () => l ? o() : o), i(T, "willCall", (h) => (T.impl = h, y)), O(l ? () => (P(y, n), y) : y), g.add(y), y; | ||
} | ||
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; | ||
let s = E(e, t, n); | ||
return v(s), ["restore", "getOriginal", "willCall"].forEach((r) => { | ||
i(s, r, s[c][r]); | ||
}), s; | ||
} | ||
@@ -114,14 +118,14 @@ | ||
function Z() { | ||
for (let e of I) | ||
for (let e of g) | ||
e.restore(); | ||
I.clear(); | ||
g.clear(); | ||
} | ||
export { | ||
T as createInternalSpy, | ||
v as getInternalState, | ||
I as createInternalSpy, | ||
A as getInternalState, | ||
E as internalSpyOn, | ||
Z as restoreAll, | ||
I as spies, | ||
g as spies, | ||
z as spy, | ||
W as spyOn | ||
}; |
{ | ||
"name": "tinyspy", | ||
"version": "2.2.0", | ||
"version": "2.2.1", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "packageManager": "pnpm@8.4.0", |
@@ -0,1 +1,2 @@ | ||
# tinyspy | ||
@@ -2,0 +3,0 @@ |
Sorry, the diff of this file is not supported yet
13913
347
13