Duplicate Code Opportunity
Summary
- Pattern: Each of the 6
describe blocks in container-cleanup-branches.test.ts declares its own let testDir: string, beforeEach that calls makeTmpDir() + jest.clearAllMocks(), and afterEach that removes the directory. The only variation between blocks is which mock is reset (mockExecaFn vs mockExecaSync).
- Locations:
src/container-cleanup-branches.test.ts lines ~44–58, 150–164, 214–228, 247–261, 283–297, 322–336
- Impact: ~60 lines of identical boilerplate; every future describe block will copy-paste the same pattern
Evidence
Block at lines 150–164 (repeated identically at 214, 247, 283, 322):
describe('cleanup - cli-proxy logs', () => {
let testDir: string;
beforeEach(() => {
testDir = makeTmpDir();
jest.clearAllMocks();
mockExecaSync.mockReturnValue(undefined);
});
afterEach(() => {
if (fs.existsSync(testDir)) {
fs.rmSync(testDir, { recursive: true, force: true });
}
});
// ...tests...
});
The first describe block (lines 44–58) is identical except it resets mockExecaFn instead of mockExecaSync.
Suggested Refactoring
Extract a shared useCleanupTestDir helper (similar to the existing useTempWorkDir helper in src/test-helpers/docker-test-fixtures.test-utils.ts) that accepts an optional mock-reset callback:
// In src/test-helpers/docker-test-fixtures.test-utils.ts or a new cleanup-test-utils
export function useCleanupTestDir(
resetMocks: () => void = () => jest.clearAllMocks()
): { getDir: () => string } {
let testDir: string;
beforeEach(() => {
testDir = makeTmpDir();
resetMocks();
});
afterEach(() => {
if (fs.existsSync(testDir)) {
fs.rmSync(testDir, { recursive: true, force: true });
}
});
return { getDir: () => testDir };
}
Each describe block then becomes a one-liner:
describe('cleanup - cli-proxy logs', () => {
const { getDir } = useCleanupTestDir(() => {
jest.clearAllMocks();
mockExecaSync.mockReturnValue(undefined);
});
// tests use getDir() instead of testDir
});
Affected Files
src/container-cleanup-branches.test.ts — lines ~44–58, 150–164, 214–228, 247–261, 283–297, 322–336
Effort Estimate
Low
Detected by Duplicate Code Detector workflow. Run date: 2026-05-20
Generated by Duplicate Code Detector · ● 20.3M · ◷
Duplicate Code Opportunity
Summary
describeblocks incontainer-cleanup-branches.test.tsdeclares its ownlet testDir: string,beforeEachthat callsmakeTmpDir()+jest.clearAllMocks(), andafterEachthat removes the directory. The only variation between blocks is which mock is reset (mockExecaFnvsmockExecaSync).src/container-cleanup-branches.test.tslines ~44–58, 150–164, 214–228, 247–261, 283–297, 322–336Evidence
Block at lines 150–164 (repeated identically at 214, 247, 283, 322):
The first
describeblock (lines 44–58) is identical except it resetsmockExecaFninstead ofmockExecaSync.Suggested Refactoring
Extract a shared
useCleanupTestDirhelper (similar to the existinguseTempWorkDirhelper insrc/test-helpers/docker-test-fixtures.test-utils.ts) that accepts an optional mock-reset callback:Each
describeblock then becomes a one-liner:Affected Files
src/container-cleanup-branches.test.ts— lines ~44–58, 150–164, 214–228, 247–261, 283–297, 322–336Effort Estimate
Low
Detected by Duplicate Code Detector workflow. Run date: 2026-05-20