Skip to content

test(auth): verify ADC fallback gracefully rejects when GCE check is false#8625

Open
westarle wants to merge 1 commit into
googleapis:mainfrom
westarle:fix-adc-fallback-edge-case
Open

test(auth): verify ADC fallback gracefully rejects when GCE check is false#8625
westarle wants to merge 1 commit into
googleapis:mainfrom
westarle:fix-adc-fallback-edge-case

Conversation

@westarle

Copy link
Copy Markdown

Other client libraries (like Python, Go, and Ruby) have robust tests verifying that if the GCE residency check returns false, the application default credentials (ADC) loader cleanly rejects with a standard NO_ADC_FOUND error. Node.js lacked a test for this clean rejection, only covering scenarios where the residency check threw an active connection error.

…false

Other client libraries (like Python, Go, and Ruby) have robust tests verifying that if the GCE residency check returns false, the application default credentials (ADC) loader cleanly rejects with a standard NO_ADC_FOUND error. Node.js lacked a test for this clean rejection, only covering scenarios where the residency check threw an active connection error.
@westarle westarle requested a review from a team as a code owner June 12, 2026 16:15

@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 adds a new unit test to verify that getApplicationDefault rejects with NO_ADC_FOUND when _checkIsGCE returns false. The review feedback recommends casting auth to any when stubbing the private method _checkIsGCE to avoid TypeScript compilation errors, and using an object pattern with the message property in assert.rejects to prevent strict class/name matching failures with custom error classes.


it('getApplicationDefault should reject with NO_ADC_FOUND if _checkIsGCE returns false', async () => {
mockWindows();
sandbox.stub(auth, '_checkIsGCE').resolves(false);

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

Since _checkIsGCE is a private method, attempting to stub it directly on auth will result in a TypeScript compilation error because keyof GoogleAuth only exposes public members. Casting auth to any avoids this compilation issue.

Suggested change
sandbox.stub(auth, '_checkIsGCE').resolves(false);
sandbox.stub(auth as any, '_checkIsGCE').resolves(false);

Comment on lines +1194 to +1197
await assert.rejects(
auth.getApplicationDefault(),
new Error(GoogleAuthExceptionMessages.NO_ADC_FOUND)
);

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

Using new Error(...) in assert.rejects performs a strict property comparison, including the name property. If getApplicationDefault() rejects with a custom error class (such as GoogleAuthError), the test will fail because the constructor names do not match (GoogleAuthError vs Error).

Using an object pattern with the message property is more robust and avoids strict class/name matching.

Suggested change
await assert.rejects(
auth.getApplicationDefault(),
new Error(GoogleAuthExceptionMessages.NO_ADC_FOUND)
);
await assert.rejects(
auth.getApplicationDefault(),
{message: GoogleAuthExceptionMessages.NO_ADC_FOUND}
);

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