Comparing version 2.2.1 to 3.0.0
@@ -9,5 +9,5 @@ declare const S: unique symbol; | ||
declare let getInternalState: GetState; | ||
declare type ReturnError = ['error', any]; | ||
declare type ReturnOk<R> = ['ok', R]; | ||
declare type ResultFn<R> = ReturnError | ReturnOk<R>; | ||
type ReturnError = ['error', any]; | ||
type ReturnOk<R> = ['ok', R]; | ||
type ResultFn<R> = ReturnError | ReturnOk<R>; | ||
interface SpyInternal<A extends any[] = any[], R = any> { | ||
@@ -25,2 +25,3 @@ (this: any, ...args: A): R; | ||
results: ResultFn<R>[]; | ||
resolves: R extends PromiseLike<infer V> ? ResultFn<V>[] : never; | ||
reset(): void; | ||
@@ -58,10 +59,10 @@ impl: ((...args: A) => R) | undefined; | ||
declare type Procedure = (...args: any[]) => any; | ||
declare type Methods<T> = { | ||
type Procedure = (...args: any[]) => any; | ||
type Methods<T> = { | ||
[K in keyof T]: T[K] extends Procedure ? K : never; | ||
}[keyof T]; | ||
declare type Getters<T> = { | ||
type Getters<T> = { | ||
[K in keyof T]: T[K] extends Procedure ? never : K; | ||
}[keyof T]; | ||
declare type Constructors<T> = { | ||
type Constructors<T> = { | ||
[K in keyof T]: T[K] extends new (...args: any[]) => any ? K : never; | ||
@@ -85,2 +86,2 @@ }[keyof T]; | ||
export { Spy, SpyFn, SpyImpl, SpyInternal, SpyInternalImpl, createInternalSpy, getInternalState, internalSpyOn, restoreAll, spies, spy, spyOn }; | ||
export { type Spy, type SpyFn, type SpyImpl, type SpyInternal, type SpyInternalImpl, createInternalSpy, getInternalState, internalSpyOn, restoreAll, spies, spy, spyOn }; |
// src/utils.ts | ||
function R(e, t) { | ||
function d(e, t) { | ||
if (!e) | ||
throw new Error(t); | ||
} | ||
function u(e, t) { | ||
function y(e, t) { | ||
return typeof t === e; | ||
} | ||
function b(e) { | ||
function w(e) { | ||
return e instanceof Promise; | ||
@@ -15,3 +15,3 @@ } | ||
} | ||
function i(e, t, n) { | ||
function p(e, t, n) { | ||
Object.defineProperty(e, t, { value: n }); | ||
@@ -21,62 +21,78 @@ } | ||
// src/constants.ts | ||
var c = Symbol.for("tinyspy:spy"); | ||
var u = Symbol.for("tinyspy:spy"); | ||
// src/internal.ts | ||
var g = /* @__PURE__ */ new Set(), C = (e) => { | ||
e.called = !1, e.callCount = 0, e.calls = [], e.results = [], e.next = []; | ||
}, 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"); | ||
var x = /* @__PURE__ */ new Set(), P = (e) => { | ||
e.called = !1, e.callCount = 0, e.calls = [], e.results = [], e.resolves = [], e.next = []; | ||
}, K = (e) => (f(e, u, { value: { reset: () => P(e[u]) } }), e[u]), I = (e) => e[u] || K(e); | ||
function g(e) { | ||
d( | ||
y("function", e) || y("undefined", e), | ||
"cannot spy on a non-function value" | ||
); | ||
let t = function(...s) { | ||
let r = A(t); | ||
let r = I(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 R = r.next.shift(); | ||
if (R) { | ||
r.results.push(R); | ||
let [o, l] = R; | ||
if (o === "ok") | ||
return l; | ||
throw l; | ||
} | ||
let p, d = "ok"; | ||
let i, c = "ok", a = r.results.length; | ||
if (r.impl) | ||
try { | ||
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; | ||
new.target ? i = Reflect.construct(r.impl, s, new.target) : i = r.impl.apply(this, s), c = "ok"; | ||
} catch (o) { | ||
throw i = o, c = "error", r.results.push([c, o]), o; | ||
} | ||
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(l, p), p = l; | ||
} | ||
return r.results.push(a), p; | ||
let S = [c, i]; | ||
return w(i) && i.then( | ||
(o) => r.resolves[a] = ["ok", o], | ||
(o) => r.resolves[a] = ["error", o] | ||
), r.results.push(S), i; | ||
}; | ||
i(t, "_isMockFunction", !0), i(t, "length", e ? e.length : 0), i(t, "name", e && e.name || "spy"); | ||
let n = A(t); | ||
p(t, "_isMockFunction", !0), p(t, "length", e ? e.length : 0), p(t, "name", e && e.name || "spy"); | ||
let n = I(t); | ||
return n.reset(), n.impl = e, t; | ||
} | ||
function v(e) { | ||
let t = A(e); | ||
function T(e) { | ||
let t = I(e); | ||
f(e, "returns", { | ||
get: () => t.results.map(([, n]) => n) | ||
}), ["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)); | ||
}), [ | ||
"called", | ||
"callCount", | ||
"results", | ||
"resolves", | ||
"calls", | ||
"reset", | ||
"impl" | ||
].forEach( | ||
(n) => f(e, n, { get: () => t[n], set: (s) => t[n] = s }) | ||
), p(e, "nextError", (n) => (t.next.push(["error", n]), t)), p(e, "nextResult", (n) => (t.next.push(["ok", n]), t)); | ||
} | ||
// src/spy.ts | ||
function z(e) { | ||
let t = I(e); | ||
return v(t), t; | ||
function L(e) { | ||
let t = g(e); | ||
return T(t), t; | ||
} | ||
// src/spyOn.ts | ||
var k = (e, t) => Object.getOwnPropertyDescriptor(e, t), P = (e, t) => { | ||
var k = (e, t) => Object.getOwnPropertyDescriptor(e, t), O = (e, t) => { | ||
t != null && typeof t == "function" && t.prototype != null && Object.setPrototypeOf(e.prototype, t.prototype); | ||
}; | ||
function E(e, t, n) { | ||
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"); | ||
function C(e, t, n) { | ||
d( | ||
!y("undefined", e), | ||
"spyOn could not find an object to spy upon" | ||
), d( | ||
y("object", e) || y("function", e), | ||
"cannot spyOn on a primitive value" | ||
); | ||
let [s, r] = (() => { | ||
if (!u("object", t)) | ||
if (!y("object", t)) | ||
return [t, "value"]; | ||
@@ -90,23 +106,28 @@ if ("getter" in t && "setter" in t) | ||
throw new Error("specify getter or setter to spy on"); | ||
})(), 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; | ||
r === "value" && a && !a.value && a.get && (r = "get", l = !0, n = a.get()); | ||
})(), R = k(e, s), i = Object.getPrototypeOf(e), c = i && k(i, s), a = R || c; | ||
d( | ||
a || s in e, | ||
`${String(s)} does not exist` | ||
); | ||
let S = !1; | ||
r === "value" && a && !a.value && a.get && (r = "get", S = !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 || { | ||
let l = g(n); | ||
r === "value" && O(l, o); | ||
let h = (A) => { | ||
let { value: M, ...v } = a || { | ||
configurable: !0, | ||
writable: !0 | ||
}; | ||
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; | ||
r !== "value" && delete v.writable, v[r] = A, f(e, s, v); | ||
}, b = () => a ? f(e, s, a) : h(o), m = l[u]; | ||
return p(m, "restore", b), p(m, "getOriginal", () => S ? o() : o), p(m, "willCall", (A) => (m.impl = A, l)), h( | ||
S ? () => (O(l, n), l) : l | ||
), x.add(l), l; | ||
} | ||
function W(e, t, n) { | ||
let s = E(e, t, n); | ||
return v(s), ["restore", "getOriginal", "willCall"].forEach((r) => { | ||
i(s, r, s[c][r]); | ||
function Q(e, t, n) { | ||
let s = C(e, t, n); | ||
return T(s), ["restore", "getOriginal", "willCall"].forEach((r) => { | ||
p(s, r, s[u][r]); | ||
}), s; | ||
@@ -116,15 +137,15 @@ } | ||
// src/restoreAll.ts | ||
function Z() { | ||
for (let e of g) | ||
function X() { | ||
for (let e of x) | ||
e.restore(); | ||
g.clear(); | ||
x.clear(); | ||
} | ||
export { | ||
I as createInternalSpy, | ||
A as getInternalState, | ||
E as internalSpyOn, | ||
Z as restoreAll, | ||
g as spies, | ||
z as spy, | ||
W as spyOn | ||
g as createInternalSpy, | ||
I as getInternalState, | ||
C as internalSpyOn, | ||
X as restoreAll, | ||
x as spies, | ||
L as spy, | ||
Q as spyOn | ||
}; |
{ | ||
"name": "tinyspy", | ||
"version": "2.2.1", | ||
"version": "3.0.0", | ||
"type": "module", | ||
"packageManager": "pnpm@8.4.0", | ||
"packageManager": "pnpm@9.1.1", | ||
"main": "./dist/index.cjs", | ||
@@ -7,0 +7,0 @@ "module": "./dist/index.js", |
@@ -1,2 +0,1 @@ | ||
# tinyspy | ||
@@ -12,2 +11,2 @@ | ||
## Docs | ||
Read **[full docs](https://github.com/tinylibs/tinyspy#readme)** on GitHub. | ||
Read full docs **[here](https://github.com/tinylibs/tinyspy#readme)**. |
Sorry, the diff of this file is not supported yet
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
17389
7
390
12