fix: resolve failing getApplicationDefault unit tests in gcloud env#3163
Merged
Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request cleans up ESLint disable comments for node:crypto imports, improves environment variable isolation in unit tests by properly copying and restoring process.env, and updates credential tests to handle asynchronous client retrieval and assert the correct types for ApplicationDefaultCredential and UserRefreshClient. I have no feedback to provide as there are no active review comments.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR resolves the unit test failures in
getApplicationDefaultandisApplicationDefaultthat occur on local machines wheregcloudapplication default credentials exist. It also fixes a potential environment pollution issue in the test suite and cleans up unused ESLint directives.Root Cause of Failures
In a prior migration to
google-auth-library,getApplicationDefault()was refactored to return an instance ofApplicationDefaultCredential(which dynamically delegates credential resolution under the hood) instead of manually parsing credential files and returning specific wrappers likeRefreshTokenCredential.However, two unit tests still asserted that the return value of
getApplicationDefault()was directly aninstanceof RefreshTokenCredential. On environments without localgcloudcredentials (like CI), these tests were silently skipped. In environments where the developer has authenticated viagcloud, these tests executed and failed.Changes Implemented
Modernized Spec Assertions:
getApplicationDefault()returnsApplicationDefaultCredential.(c as any).googleAuth.getClient()and verified it resolves to an instance ofUserRefreshClient(fromgoogle-auth-library), preserving the exact test coverage and intent.process.env.GOOGLE_CLOUD_PROJECTinside these asynchronous test cases to preventgoogle-auth-libraryfrom hanging during GCE metadata service auto-discovery on local environments.Isolated test environment variables (
process.env):beforeEachandafterEachenvironment setup/teardown incredential-internal.spec.tsto shallow-copy and properly restore environment variables, preventing modified environment variables from polluting other test suites.ESLint directive cleanups:
eslint-disable-next-line @typescript-eslint/no-var-requirescomments incrypto-signerfiles to resolve linting warnings.