Type: Enhancement / Refactoring
Labels: enhancement, testing, technical-debt
The current test suite uses migueleliasweb/go-github-mock for mocking GitHub REST API responses in unit tests. While this library has served us well, there are several reasons to consider replacing it with stretchr/testify/mock:
-
Dependency Consolidation: We already use
stretchr/testifyfor assertions (assertandrequire). Usingtestify/mockwould consolidate our testing dependencies. -
Interface-based Mocking:
testify/mockencourages interface-based mocking, which leads to better separation of concerns and more flexible test design. -
Maintenance & Activity:
stretchr/testifyis one of the most widely used Go testing libraries with active maintenance. -
Type Safety: Interface-based mocking provides compile-time type checking, whereas HTTP-level mocking relies on runtime matching.
-
Test Clarity: Mock expectations at the interface level make tests more readable and focused on behavior rather than HTTP transport details.
The codebase currently uses go-github-mock extensively (metrics from grep search):
| Metric | Count |
|---|---|
| Files using go-github-mock | 16 |
mock.NewMockedHTTPClient calls |
~449 |
mock.WithRequestMatchHandler calls |
~267 |
mock.WithRequestMatch calls |
~79 |
| Unique mock endpoint patterns | ~80+ |
Note: Run grep -c "mock.NewMockedHTTPClient" pkg/github/*_test.go to verify current counts.
Files affected:
pkg/github/*_test.go(14 files)pkg/raw/raw_test.gopkg/raw/raw_mock.go
Replace HTTP-level mocking with interface-based mocking using testify/mock:
- Define interfaces for GitHub client operations (if not already present)
- Create mock implementations using
testify/mock - Update the codebase to depend on interfaces rather than concrete clients
- Start with a single test file to establish patterns
- Create helper functions for common mock setups
- Migrate remaining test files one at a time
- Remove
go-github-mockdependency when complete
Before (HTTP-level mocking):
mockedClient := mock.NewMockedHTTPClient(
mock.WithRequestMatch(
mock.GetReposIssuesByOwnerByRepoByIssueNumber,
mockIssue,
),
)
client := github.NewClient(mockedClient)After (Interface-based mocking):
mockClient := new(MockGitHubClient)
mockClient.On("GetIssue", ctx, "owner", "repo", 42).Return(mockIssue, nil)- Simpler Test Setup: No need to construct HTTP responses
- Better Error Testing: Easy to mock error conditions without crafting HTTP error responses
- Faster Tests: No HTTP round-trip overhead (even if mocked)
- Clearer Intent: Tests read more like specifications
- Reduced Dependencies: One less external dependency to maintain
- Migration Effort: This is a significant refactoring with ~449 mock usages to update
- Interface Design: Need to carefully design interfaces that balance granularity and usability
- GraphQL Mocking: The existing
githubv4mockfor GraphQL is out of scope for this issue and will remain unchanged. It already provides a different mocking approach specific to GraphQL. - Breaking Changes: Test file changes only, no production code changes expected
- Audit current mock usage patterns to identify common interfaces needed
- Design and implement mock interfaces for GitHub REST API client
- Create helper functions and test utilities for common mock scenarios
- Migrate test files incrementally (suggest starting with smallest files):
-
pkg/github/code_scanning_test.go(~4 mocks) -
pkg/github/secret_scanning_test.go(~5 mocks) -
pkg/github/dependabot_test.go(~6 mocks) - Continue with remaining files...
-
- Update
docs/testing.mdto reflect new mocking patterns - Remove
go-github-mockfromgo.modafter complete migration
Current testing documentation reference:
Mocking is performed using go-github-mock or
githubv4mockfor simulating GitHub REST and GQL API responses.
This will need to be updated to:
Mocking is performed using testify/mock for interface-based mocking or
githubv4mockfor simulating GitHub GQL API responses.
- stretchr/testify documentation
- Current testing guidelines:
docs/testing.md