Skip to content

Commit 01e56cd

Browse files
committed
[Address review comments] test: use Proxy for call-through fallback in proxyquire helper
1 parent 97ffe67 commit 01e56cd

3 files changed

Lines changed: 72 additions & 24 deletions

File tree

‎handwritten/bigtable/test/proxyquire.ts‎

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,31 @@ function proxyquire<T = any>(request: string, stubs: Stubs = {}): T {
7676
try {
7777
const real = prev.call(this, id);
7878
if (real && ['object', 'function'].includes(typeof real)) {
79-
for (const k of Object.keys(real)) {
80-
if (!(k in stub)) {
81-
try {
82-
stub[k] = real[k];
83-
} catch {
84-
// ignore
79+
return new Proxy(stub, {
80+
get(target, prop) {
81+
if (prop in target) {
82+
return Reflect.get(target, prop);
8583
}
86-
}
87-
}
84+
return Reflect.get(real, prop);
85+
},
86+
has(target, prop) {
87+
return prop in target || prop in real;
88+
},
89+
ownKeys(target) {
90+
return Array.from(
91+
new Set([
92+
...Reflect.ownKeys(target),
93+
...Reflect.ownKeys(real),
94+
]),
95+
);
96+
},
97+
getOwnPropertyDescriptor(target, prop) {
98+
const desc =
99+
Reflect.getOwnPropertyDescriptor(target, prop) ||
100+
Reflect.getOwnPropertyDescriptor(real, prop);
101+
return desc ? {...desc, configurable: true} : undefined;
102+
},
103+
});
88104
}
89105
} catch {
90106
// ignore

‎handwritten/spanner/test/proxyquire.ts‎

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,31 @@ function proxyquire<T = any>(request: string, stubs: Stubs = {}): T {
7676
try {
7777
const real = prev.call(this, id);
7878
if (real && ['object', 'function'].includes(typeof real)) {
79-
for (const k of Object.keys(real)) {
80-
if (!(k in stub)) {
81-
try {
82-
stub[k] = real[k];
83-
} catch {
84-
// ignore
79+
return new Proxy(stub, {
80+
get(target, prop) {
81+
if (prop in target) {
82+
return Reflect.get(target, prop);
8583
}
86-
}
87-
}
84+
return Reflect.get(real, prop);
85+
},
86+
has(target, prop) {
87+
return prop in target || prop in real;
88+
},
89+
ownKeys(target) {
90+
return Array.from(
91+
new Set([
92+
...Reflect.ownKeys(target),
93+
...Reflect.ownKeys(real),
94+
]),
95+
);
96+
},
97+
getOwnPropertyDescriptor(target, prop) {
98+
const desc =
99+
Reflect.getOwnPropertyDescriptor(target, prop) ||
100+
Reflect.getOwnPropertyDescriptor(real, prop);
101+
return desc ? {...desc, configurable: true} : undefined;
102+
},
103+
});
88104
}
89105
} catch {
90106
// ignore

‎handwritten/storage/test/proxyquire.ts‎

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,31 @@ function proxyquire<T = any>(request: string, stubs: Stubs = {}): T {
7676
try {
7777
const real = prev.call(this, id);
7878
if (real && ['object', 'function'].includes(typeof real)) {
79-
for (const k of Object.keys(real)) {
80-
if (!(k in stub)) {
81-
try {
82-
stub[k] = real[k];
83-
} catch {
84-
// ignore
79+
return new Proxy(stub, {
80+
get(target, prop) {
81+
if (prop in target) {
82+
return Reflect.get(target, prop);
8583
}
86-
}
87-
}
84+
return Reflect.get(real, prop);
85+
},
86+
has(target, prop) {
87+
return prop in target || prop in real;
88+
},
89+
ownKeys(target) {
90+
return Array.from(
91+
new Set([
92+
...Reflect.ownKeys(target),
93+
...Reflect.ownKeys(real),
94+
])
95+
);
96+
},
97+
getOwnPropertyDescriptor(target, prop) {
98+
const desc =
99+
Reflect.getOwnPropertyDescriptor(target, prop) ||
100+
Reflect.getOwnPropertyDescriptor(real, prop);
101+
return desc ? {...desc, configurable: true} : undefined;
102+
},
103+
});
88104
}
89105
} catch {
90106
// ignore

0 commit comments

Comments
 (0)