Repository navigation
test: remove --proxyquire-shim from common, bigquery, datastore, and dns - #9639
danieljbruce wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request removes the --proxyquire-shim test flag across multiple packages, replacing the external proxyquire dependency with a custom local implementation or refactoring tests to use sinon stubs and manual module mocking. The review feedback highlights a few key improvements: first, the custom proxyquire helper hasOwn should be updated to support function stubs; second, the manual module mocking in google-cloud-translate should properly clean up require.cache when the module was not previously cached to avoid test pollution; and finally, the stubbed makeAuthenticatedRequestFactory in error-reporting should return a function rather than a plain object to prevent potential runtime errors.
| const hasOwn = (o: unknown, k: string) => | ||
| Boolean(o && typeof o === 'object' && hasOwnProperty.call(o, k)); |
There was a problem hiding this comment.
The hasOwn helper currently checks typeof o === 'object'. This will return false if the stub is a function (e.g., a constructor or a stubbed class), preventing properties like @global or @noCallThru from being detected on function stubs. Updating the check to include typeof o === 'function' resolves this issue.
| const hasOwn = (o: unknown, k: string) => | |
| Boolean(o && typeof o === 'object' && hasOwnProperty.call(o, k)); | |
| const hasOwn = (o: unknown, k: string) => | |
| Boolean(o && (typeof o === 'object' || typeof o === 'function') && hasOwnProperty.call(o, k)); |
| const hasOwn = (o: unknown, k: string) => | ||
| Boolean(o && typeof o === 'object' && hasOwnProperty.call(o, k)); |
There was a problem hiding this comment.
The hasOwn helper currently checks typeof o === 'object'. This will return false if the stub is a function (e.g., a constructor or a stubbed class), preventing properties like @global or @noCallThru from being detected on function stubs. Updating the check to include typeof o === 'function' resolves this issue.
| const hasOwn = (o: unknown, k: string) => | |
| Boolean(o && typeof o === 'object' && hasOwnProperty.call(o, k)); | |
| const hasOwn = (o: unknown, k: string) => | |
| Boolean(o && (typeof o === 'object' || typeof o === 'function') && hasOwnProperty.call(o, k)); |
| const hasOwn = (o: unknown, k: string) => | ||
| Boolean(o && typeof o === 'object' && hasOwnProperty.call(o, k)); |
There was a problem hiding this comment.
The hasOwn helper currently checks typeof o === 'object'. This will return false if the stub is a function (e.g., a constructor or a stubbed class), preventing properties like @global or @noCallThru from being detected on function stubs. Updating the check to include typeof o === 'function' resolves this issue.
| const hasOwn = (o: unknown, k: string) => | |
| Boolean(o && typeof o === 'object' && hasOwnProperty.call(o, k)); | |
| const hasOwn = (o: unknown, k: string) => | |
| Boolean(o && (typeof o === 'object' || typeof o === 'function') && hasOwnProperty.call(o, k)); |
| const hasOwn = (o: unknown, k: string) => | ||
| Boolean(o && typeof o === 'object' && hasOwnProperty.call(o, k)); |
There was a problem hiding this comment.
The hasOwn helper currently checks typeof o === 'object'. This will return false if the stub is a function (e.g., a constructor or a stubbed class), preventing properties like @global or @noCallThru from being detected on function stubs. Updating the check to include typeof o === 'function' resolves this issue.
| const hasOwn = (o: unknown, k: string) => | |
| Boolean(o && typeof o === 'object' && hasOwnProperty.call(o, k)); | |
| const hasOwn = (o: unknown, k: string) => | |
| Boolean(o && (typeof o === 'object' || typeof o === 'function') && hasOwnProperty.call(o, k)); |
| } finally { | ||
| mod.prototype.require = origRequire; | ||
| if (cached) require.cache[v2Path] = cached; | ||
| } |
There was a problem hiding this comment.
If cached is undefined (meaning the module was not previously loaded), the stubbed module remains in require.cache after the test setup. This causes test pollution, as subsequent tests requiring ../src/v2 will receive the stubbed version. Deleting the cache entry when cached is undefined prevents this.
| } finally { | |
| mod.prototype.require = origRequire; | |
| if (cached) require.cache[v2Path] = cached; | |
| } | |
| } finally { | |
| mod.prototype.require = origRequire; | |
| if (cached) { | |
| require.cache[v2Path] = cached; | |
| } else { | |
| delete require.cache[v2Path]; | |
| } | |
| } |
| util.makeAuthenticatedRequestFactory = (() => ({ | ||
| authClient: { | ||
| async getAccessToken() { | ||
| if (errToReturn) { | ||
| throw errToReturn; | ||
| } | ||
| return 'some-token'; | ||
| }, | ||
| }, | ||
| getCredentials: {}, | ||
| })) as unknown as typeof util.makeAuthenticatedRequestFactory; |
There was a problem hiding this comment.
The stubbed util.makeAuthenticatedRequestFactory returns a plain object instead of a function. If the constructor or any other code attempts to invoke this.request as a function, it will throw a TypeError. Returning a function with the stubbed properties attached is safer and matches the expected signature.
| util.makeAuthenticatedRequestFactory = (() => ({ | |
| authClient: { | |
| async getAccessToken() { | |
| if (errToReturn) { | |
| throw errToReturn; | |
| } | |
| return 'some-token'; | |
| }, | |
| }, | |
| getCredentials: {}, | |
| })) as unknown as typeof util.makeAuthenticatedRequestFactory; | |
| const fakeRequest = (() => {}) as any; | |
| fakeRequest.authClient = { | |
| async getAccessToken() { | |
| if (errToReturn) { | |
| throw errToReturn; | |
| } | |
| return 'some-token'; | |
| }, | |
| }; | |
| fakeRequest.getCredentials = {}; | |
| util.makeAuthenticatedRequestFactory = (() => fakeRequest) as unknown as typeof util.makeAuthenticatedRequestFactory; |
Description
Replaces global proxyquire shim with local test helper in four packages.
Impact
Removes --proxyquire-shim from common, bigquery, datastore, and google-cloud-dns test scripts.
Changes
core/common,handwritten/bigquery,handwritten/datastore,handwritten/google-cloud-dns: Add a runtime-agnostictest/proxyquire.tshelper built onModule.prototype.requireandrequire.cachethat works identically in Node.js and Bun.'proxyquire'to'./proxyquire'(or relative path) and remove--proxyquire-shimfrom theirpackage.jsontest scripts.Testing
pnpm testunder both Node.js and Bun (JS_RUNTIME=bun) incore/common,handwritten/bigquery,handwritten/datastore, andhandwritten/google-cloud-dns.bin/linter.mjs --strict.Alternatives
bin/proxyquire-bun-shim.cjspreload shim.