Skip to content

[Duplicate Code] Repeated beforeEach/afterEach testDir setup block across 6 describe groups in container-cleanup-branches.test.ts #3471

@github-actions

Description

@github-actions

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 ·

  • expires on Jun 19, 2026, 10:16 PM UTC

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions