Skip to content

Commit 97ffe67

Browse files
committed
[Address CI errors] test: support function stubs and guard read-only properties in proxyquire helper
1 parent 401b5a0 commit 97ffe67

3 files changed

Lines changed: 30 additions & 9 deletions

File tree

‎handwritten/bigtable/test/proxyquire.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ const mod = Module as unknown as {
2424
_resolveFilename: (request: string, parent: NodeModule) => string;
2525
};
2626
const {hasOwnProperty} = Object.prototype;
27-
const hasOwn = (o: unknown, k: string) =>
28-
Boolean(o && typeof o === 'object' && hasOwnProperty.call(o, k));
27+
const isObj = (o: unknown) =>
28+
Boolean(o && (typeof o === 'object' || typeof o === 'function'));
29+
const hasOwn = (o: unknown, k: string) => isObj(o) && hasOwnProperty.call(o, k);
2930
const isGlobal = (s: unknown) =>
3031
hasOwn(s, '@global') || hasOwn(s, '@runtimeGlobal');
3132
const resolveFrom = (base: string, req: string) => {
@@ -76,7 +77,13 @@ function proxyquire<T = any>(request: string, stubs: Stubs = {}): T {
7677
const real = prev.call(this, id);
7778
if (real && ['object', 'function'].includes(typeof real)) {
7879
for (const k of Object.keys(real)) {
79-
if (!(k in stub)) stub[k] = real[k];
80+
if (!(k in stub)) {
81+
try {
82+
stub[k] = real[k];
83+
} catch {
84+
// ignore
85+
}
86+
}
8087
}
8188
}
8289
} catch {

‎handwritten/spanner/test/proxyquire.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ const mod = Module as unknown as {
2424
_resolveFilename: (request: string, parent: NodeModule) => string;
2525
};
2626
const {hasOwnProperty} = Object.prototype;
27-
const hasOwn = (o: unknown, k: string) =>
28-
Boolean(o && typeof o === 'object' && hasOwnProperty.call(o, k));
27+
const isObj = (o: unknown) =>
28+
Boolean(o && (typeof o === 'object' || typeof o === 'function'));
29+
const hasOwn = (o: unknown, k: string) => isObj(o) && hasOwnProperty.call(o, k);
2930
const isGlobal = (s: unknown) =>
3031
hasOwn(s, '@global') || hasOwn(s, '@runtimeGlobal');
3132
const resolveFrom = (base: string, req: string) => {
@@ -76,7 +77,13 @@ function proxyquire<T = any>(request: string, stubs: Stubs = {}): T {
7677
const real = prev.call(this, id);
7778
if (real && ['object', 'function'].includes(typeof real)) {
7879
for (const k of Object.keys(real)) {
79-
if (!(k in stub)) stub[k] = real[k];
80+
if (!(k in stub)) {
81+
try {
82+
stub[k] = real[k];
83+
} catch {
84+
// ignore
85+
}
86+
}
8087
}
8188
}
8289
} catch {

‎handwritten/storage/test/proxyquire.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ const mod = Module as unknown as {
2424
_resolveFilename: (request: string, parent: NodeModule) => string;
2525
};
2626
const {hasOwnProperty} = Object.prototype;
27-
const hasOwn = (o: unknown, k: string) =>
28-
Boolean(o && typeof o === 'object' && hasOwnProperty.call(o, k));
27+
const isObj = (o: unknown) =>
28+
Boolean(o && (typeof o === 'object' || typeof o === 'function'));
29+
const hasOwn = (o: unknown, k: string) => isObj(o) && hasOwnProperty.call(o, k);
2930
const isGlobal = (s: unknown) =>
3031
hasOwn(s, '@global') || hasOwn(s, '@runtimeGlobal');
3132
const resolveFrom = (base: string, req: string) => {
@@ -76,7 +77,13 @@ function proxyquire<T = any>(request: string, stubs: Stubs = {}): T {
7677
const real = prev.call(this, id);
7778
if (real && ['object', 'function'].includes(typeof real)) {
7879
for (const k of Object.keys(real)) {
79-
if (!(k in stub)) stub[k] = real[k];
80+
if (!(k in stub)) {
81+
try {
82+
stub[k] = real[k];
83+
} catch {
84+
// ignore
85+
}
86+
}
8087
}
8188
}
8289
} catch {

0 commit comments

Comments
 (0)