-
Notifications
You must be signed in to change notification settings - Fork 663
test(utils): add fallback and error-handling tests for getGitHubToken #1438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Added new test cases to validate getGitHubToken behavior when: - GH_TOKEN is used as a fallback - token is loaded from a .env file - missing token errors are handled correctly These tests improve reliability and coverage for authentication utilities.
|
Hi! 😊 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR aims to add comprehensive test coverage for the getGitHubToken() utility function, including tests for fallback token resolution mechanisms and error handling. However, the tests are validating functionality that doesn't exist in the current implementation.
Key Issues:
- Tests expect
GH_TOKENfallback,.envfile reading, and detailed error messages that are not implemented ingetGitHubToken() - The current implementation only checks
process.env.GITHUB_TOKENand throws a simple error if not found - Tests will fail because they don't match the actual behavior of the function
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| it("should return token when GH_TOKEN is set as a fallback", () => { | ||
| vi.stubEnv("GITHUB_TOKEN", ""); | ||
| vi.stubEnv("GH_TOKEN", "fallback-token"); | ||
|
|
||
| const result = getGitHubToken(); | ||
| expect(result).toBe("fallback-token"); | ||
| }); |
Copilot
AI
Nov 12, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test expects GH_TOKEN fallback behavior, but the current implementation of getGitHubToken() only checks process.env.GITHUB_TOKEN. The implementation doesn't have any fallback logic to check GH_TOKEN. Either update the implementation to support this fallback, or remove this test.
| it("should read token from .env file if environment variables are not set", () => { | ||
| vi.stubEnv("GITHUB_TOKEN", ""); | ||
| vi.stubEnv("GH_TOKEN", ""); | ||
| const envPath = path.join(process.cwd(), ".env"); | ||
| fs.writeFileSync(envPath, "GITHUB_TOKEN=env-file-token"); | ||
| const result = getGitHubToken(); | ||
| expect(result).toBe("env-file-token"); | ||
| fs.unlinkSync(envPath); | ||
| }); | ||
|
|
Copilot
AI
Nov 12, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test expects .env file reading functionality, but the current implementation of getGitHubToken() doesn't read from .env files. The implementation only checks process.env.GITHUB_TOKEN. Either update the implementation to support reading from .env files, or remove this test.
Additionally, there are file system side effects: if the test fails before reaching line 61, the .env file will not be cleaned up. Consider using a try-finally block or vitest's cleanup hooks to ensure the file is always deleted.
| it("should read token from .env file if environment variables are not set", () => { | |
| vi.stubEnv("GITHUB_TOKEN", ""); | |
| vi.stubEnv("GH_TOKEN", ""); | |
| const envPath = path.join(process.cwd(), ".env"); | |
| fs.writeFileSync(envPath, "GITHUB_TOKEN=env-file-token"); | |
| const result = getGitHubToken(); | |
| expect(result).toBe("env-file-token"); | |
| fs.unlinkSync(envPath); | |
| }); |
| it("should throw detailed error when no token found", () => { | ||
| vi.stubEnv("GITHUB_TOKEN", ""); | ||
| vi.stubEnv("GH_TOKEN", ""); | ||
| expect(() => getGitHubToken()).toThrow(/Missing GitHub authentication token/); |
Copilot
AI
Nov 12, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test expects a detailed error message matching the regex /Missing GitHub authentication token/, but the actual implementation throws "GITHUB_TOKEN environment variable is required.". This test will fail. Update the expected error message to match the actual implementation, or update the implementation to throw the expected message.
| expect(() => getGitHubToken()).toThrow(/Missing GitHub authentication token/); | |
| expect(() => getGitHubToken()).toThrow("GITHUB_TOKEN environment variable is required."); |
|
@nitesh404240 please resolve the comments. |
Added comprehensive test coverage for getGitHubToken() to validate new fallback logic:
Tests for GITHUB_TOKEN, GH_TOKEN, and .env token resolution.
Ensures proper error handling when no tokens are found.
Adds clear environment cleanup between tests to avoid cross-test interference.
These tests strengthen reliability and ensure consistent behavior across environments