test(auth): verify ADC fallback gracefully rejects when GCE check is false#8625
test(auth): verify ADC fallback gracefully rejects when GCE check is false#8625westarle wants to merge 1 commit into
Conversation
…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.
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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.
| sandbox.stub(auth, '_checkIsGCE').resolves(false); | |
| sandbox.stub(auth as any, '_checkIsGCE').resolves(false); |
| await assert.rejects( | ||
| auth.getApplicationDefault(), | ||
| new Error(GoogleAuthExceptionMessages.NO_ADC_FOUND) | ||
| ); |
There was a problem hiding this comment.
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.
| await assert.rejects( | |
| auth.getApplicationDefault(), | |
| new Error(GoogleAuthExceptionMessages.NO_ADC_FOUND) | |
| ); | |
| await assert.rejects( | |
| auth.getApplicationDefault(), | |
| {message: GoogleAuthExceptionMessages.NO_ADC_FOUND} | |
| ); |
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.