Skip to content

test: remove --proxyquire-shim from common, bigquery, datastore, and dns - #9639

Closed
danieljbruce wants to merge 2 commits into
googleapis:mainfrom
danieljbruce:fix/remove-proxyquire-shim-core-dns-ds-bq
Closed

danieljbruce wants to merge 2 commits into
googleapis:mainfrom
danieljbruce:fix/remove-proxyquire-shim-core-dns-ds-bq

Conversation

@danieljbruce

@danieljbruce danieljbruce commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

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

Testing

  • Executed pnpm test under both Node.js and Bun (JS_RUNTIME=bun) in core/common, handwritten/bigquery, handwritten/datastore, and handwritten/google-cloud-dns.
  • Verified strict lint and type checks pass via bin/linter.mjs --strict.

Alternatives

  • Not merging this pull request was considered, which would keep these four packages coupled to the global bin/proxyquire-bun-shim.cjs preload shim.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +27 to +28
const hasOwn = (o: unknown, k: string) =>
Boolean(o && typeof o === 'object' && hasOwnProperty.call(o, k));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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));

Comment on lines +27 to +28
const hasOwn = (o: unknown, k: string) =>
Boolean(o && typeof o === 'object' && hasOwnProperty.call(o, k));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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));

Comment on lines +27 to +28
const hasOwn = (o: unknown, k: string) =>
Boolean(o && typeof o === 'object' && hasOwnProperty.call(o, k));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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));

Comment on lines +27 to +28
const hasOwn = (o: unknown, k: string) =>
Boolean(o && typeof o === 'object' && hasOwnProperty.call(o, k));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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));

Comment on lines +97 to +100
} finally {
mod.prototype.require = origRequire;
if (cached) require.cache[v2Path] = cached;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
} 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];
}
}

Comment on lines +56 to +66
util.makeAuthenticatedRequestFactory = (() => ({
authClient: {
async getAccessToken() {
if (errToReturn) {
throw errToReturn;
}
return 'some-token';
},
},
getCredentials: {},
})) as unknown as typeof util.makeAuthenticatedRequestFactory;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant