tinyspy
Advanced tools
Comparing version 2.0.0 to 2.1.0
declare const S: unique symbol; | ||
interface GetState { | ||
<A extends any[], R>(spy: SpyInternalImpl<A, R>): SpyInternalImplState<A, R>; | ||
<A extends any[], R>(spy: SpyInternal<A, R>): SpyInternalState<A, R>; | ||
} | ||
declare let spies: Set<SpyImpl<any[], any>>; | ||
declare let getInternalState: <A extends any[], R>(spy: SpyInternal<A, R>) => SpyInternalState<A, R>; | ||
declare let getInternalState: GetState; | ||
declare type ReturnError = ['error', any]; | ||
@@ -62,3 +66,3 @@ declare type ReturnOk<R> = ['ok', R]; | ||
}[keyof T]; | ||
declare function internalSpyOn<T extends object, K extends string & keyof T>(obj: T, methodName: K | { | ||
declare function internalSpyOn<T, K extends string & keyof T>(obj: T, methodName: K | { | ||
getter: K; | ||
@@ -79,2 +83,2 @@ } | { | ||
export { Spy, SpyFn, SpyImpl, createInternalSpy, getInternalState, internalSpyOn, restoreAll, spies, spy, spyOn }; | ||
export { Spy, SpyFn, SpyImpl, SpyInternal, SpyInternalImpl, createInternalSpy, getInternalState, internalSpyOn, restoreAll, spies, spy, spyOn }; |
// src/utils.ts | ||
function S(e, t) { | ||
function R(e, t) { | ||
if (!e) | ||
throw new Error(t); | ||
} | ||
function p(e, t) { | ||
function i(e, t) { | ||
return typeof t === e; | ||
} | ||
function O(e) { | ||
function b(e) { | ||
return e instanceof Promise; | ||
@@ -25,38 +25,38 @@ } | ||
e.called = !1, e.callCount = 0, e.calls = [], e.results = []; | ||
}, C = (e) => (g(e, y, { value: { reset: () => M(e[y]) } }), e[y]), I = (e) => e[y] || C(e); | ||
function T(e) { | ||
S(p("function", e) || p("undefined", e), "cannot spy on a non-function value"); | ||
let t = function(...a) { | ||
let r = I(t); | ||
if (r.called = !0, r.callCount++, r.calls.push(a), r.next) { | ||
let [i, s] = r.next; | ||
if (r.results.push(r.next), r.next = null, i === "ok") | ||
}, C = (e) => (g(e, y, { value: { reset: () => M(e[y]) } }), e[y]), A = (e) => e[y] || C(e); | ||
function I(e) { | ||
R(i("function", e) || i("undefined", e), "cannot spy on a non-function value"); | ||
let t = function(...o) { | ||
let r = A(t); | ||
if (r.called = !0, r.callCount++, r.calls.push(o), r.next) { | ||
let [p, s] = r.next; | ||
if (r.results.push(r.next), r.next = null, p === "ok") | ||
return s; | ||
throw s; | ||
} | ||
let o, f = "ok"; | ||
let a, f = "ok"; | ||
if (r.impl) | ||
try { | ||
o = r.impl.apply(this, a), f = "ok"; | ||
} catch (i) { | ||
throw o = i, f = "error", r.results.push([f, i]), i; | ||
a = r.impl.apply(this, o), f = "ok"; | ||
} catch (p) { | ||
throw a = p, f = "error", r.results.push([f, p]), p; | ||
} | ||
let u = [f, o]; | ||
if (O(o)) { | ||
let i = o.then((s) => u[1] = s).catch((s) => { | ||
let u = [f, a]; | ||
if (b(a)) { | ||
let p = a.then((s) => u[1] = s).catch((s) => { | ||
throw u[0] = "error", u[1] = s, s; | ||
}); | ||
Object.assign(i, o), o = i; | ||
Object.assign(p, a), a = p; | ||
} | ||
return r.results.push(u), o; | ||
return r.results.push(u), a; | ||
}; | ||
l(t, "_isMockFunction", !0), l(t, "length", e ? e.length : 0), l(t, "name", e && e.name || "spy"); | ||
let n = I(t); | ||
let n = A(t); | ||
return n.reset(), n.impl = e, t; | ||
} | ||
function v(e) { | ||
let t = I(e); | ||
let t = A(e); | ||
g(e, "returns", { | ||
get: () => t.results.map(([, n]) => n) | ||
}), ["called", "callCount", "results", "calls", "reset", "impl"].forEach((n) => g(e, n, { get: () => t[n], set: (a) => t[n] = a })), l(e, "nextError", (n) => (t.next = ["error", n], t)), l(e, "nextResult", (n) => (t.next = ["ok", n], t)); | ||
}), ["called", "callCount", "results", "calls", "reset", "impl"].forEach((n) => g(e, n, { get: () => t[n], set: (o) => t[n] = o })), l(e, "nextError", (n) => (t.next = ["error", n], t)), l(e, "nextResult", (n) => (t.next = ["ok", n], t)); | ||
} | ||
@@ -66,3 +66,3 @@ | ||
function z(e) { | ||
let t = T(e); | ||
let t = I(e); | ||
return v(t), t; | ||
@@ -73,6 +73,6 @@ } | ||
var P = (e, t) => Object.getOwnPropertyDescriptor(e, t); | ||
function j(e, t, n) { | ||
S(!p("undefined", e), "spyOn could not find an object to spy upon"), S(p("object", e) || p("function", e), "cannot spyOn on a primitive value"); | ||
let a = () => { | ||
if (!p("object", t)) | ||
function E(e, t, n) { | ||
R(!i("undefined", e), "spyOn could not find an object to spy upon"), R(i("object", e) || i("function", e), "cannot spyOn on a primitive value"); | ||
let o = () => { | ||
if (!i("object", t)) | ||
return [t, "value"]; | ||
@@ -86,22 +86,22 @@ if ("getter" in t && "setter" in t) | ||
throw new Error("specify getter or setter to spy on"); | ||
}, [r, o] = a(), f = P(e, r), u = Object.getPrototypeOf(e), i = u && P(u, r), s = f || i; | ||
S(s || r in e, `${String(r)} does not exist`); | ||
}, [r, a] = o(), f = P(e, r), u = Object.getPrototypeOf(e), p = u && P(u, r), s = f || p; | ||
R(s || r in e, `${String(r)} does not exist`); | ||
let w = !1; | ||
o === "value" && s && !s.value && s.get && (o = "get", w = !0, n = s.get()); | ||
a === "value" && s && !s.value && s.get && (a = "get", w = !0, n = s.get()); | ||
let c; | ||
s ? c = s[o] : o !== "value" ? c = () => e[r] : c = e[r], n || (n = c); | ||
let d = T(n), k = (h) => { | ||
let { value: E, ...b } = s || { | ||
s ? c = s[a] : a !== "value" ? c = () => e[r] : c = e[r], n || (n = c); | ||
let d = I(n), O = (h) => { | ||
let { value: G, ...k } = s || { | ||
configurable: !0, | ||
writable: !0 | ||
}; | ||
o !== "value" && delete b.writable, b[o] = h, g(e, r, b); | ||
}, K = () => k(c), A = d[y]; | ||
return l(A, "restore", K), l(A, "getOriginal", () => w ? c() : c), l(A, "willCall", (h) => (A.impl = h, d)), k(w ? () => d : d), m.add(d), d; | ||
a !== "value" && delete k.writable, k[a] = h, g(e, r, k); | ||
}, K = () => O(c), T = d[y]; | ||
return l(T, "restore", K), l(T, "getOriginal", () => w ? c() : c), l(T, "willCall", (h) => (T.impl = h, d)), O(w ? () => d : d), m.add(d), d; | ||
} | ||
function W(e, t, n) { | ||
let a = j(e, t, n); | ||
return v(a), ["restore", "getOriginal", "willCall"].forEach((r) => { | ||
l(a, r, a[y][r]); | ||
}), a; | ||
let o = E(e, t, n); | ||
return v(o), ["restore", "getOriginal", "willCall"].forEach((r) => { | ||
l(o, r, o[y][r]); | ||
}), o; | ||
} | ||
@@ -116,5 +116,5 @@ | ||
export { | ||
T as createInternalSpy, | ||
I as getInternalState, | ||
j as internalSpyOn, | ||
I as createInternalSpy, | ||
A as getInternalState, | ||
E as internalSpyOn, | ||
Z as restoreAll, | ||
@@ -121,0 +121,0 @@ m as spies, |
{ | ||
"name": "tinyspy", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"type": "module", | ||
@@ -34,6 +34,3 @@ "main": "./dist/index.cjs", | ||
"node": ">=14.0.0" | ||
}, | ||
"scripts": { | ||
"publish": "pnpm run build && clean-publish" | ||
} | ||
} |
@@ -5,3 +5,3 @@ # tinyspy | ||
A `9KB` package for minimal and easy testing with no dependencies. | ||
A `10KB` package for minimal and easy testing with no dependencies. | ||
This package was created for having a tiny spy library to use in `vitest`, but it can also be used in `jest` and other test environments. | ||
@@ -8,0 +8,0 @@ |
Sorry, the diff of this file is not supported yet
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
13327
333
1