Skip to content

Enable parallel test execution to reduce CI time by 30%

Phase 2: Enable Test Parallelization

Overview

After Phase 1's duplicate test elimination (reducing CI from ~45 to ~22 minutes), this phase will enable parallel test execution to achieve an additional 30% reduction in CI time.

Current State

  • Tests run serially with no parallelization
  • 753 tests across 33 test files
  • Test execution takes 18-27 minutes after Phase 1 fix
  • No GitLab CI parallel matrix jobs configured

Implementation Tasks

1. Configure Vitest for Parallel Execution

Update vitest.config.ts:

export default defineConfig({
  test: {
    // Enable thread pool for parallel execution
    pool: 'threads',
    poolOptions: {
      threads: {
        minThreads: 2,
        maxThreads: 4,
        // Isolate tests to prevent interference
        isolate: true
      }
    },
    // Optimize for CI environment
    testTimeout: isCI() ? 60000 : 30000,
    // Ensure proper test isolation
    sequence: {
      shuffle: false,
      concurrent: true
    }
  }
});

2. Implement GitLab CI Parallel Matrix

Split tests into categories in .gitlab-ci.yml:

test:unit:
  stage: test
  parallel: 2
  script:
    - npm run test:unit -- --shard=$CI_NODE_INDEX/$CI_NODE_TOTAL

test:integration:
  stage: test
  parallel: 3
  script:
    - npm run test:integration -- --shard=$CI_NODE_INDEX/$CI_NODE_TOTAL

3. Test Categorization

Create separate test scripts in package.json:

  • test:unit - Fast unit tests (no containers)
  • test:integration - Container-dependent tests
  • test:e2e - Full workflow tests

4. Optimize Test Database Management

  • Implement connection pooling for test databases
  • Pre-warm containers during global setup
  • Optimize cleanup between test suites

Expected Benefits

  • Time Reduction: From ~22 minutes to ~15 minutes (30% improvement)
  • Better Resource Utilization: Parallel execution uses available CPU cores
  • Faster Feedback: Unit tests complete quickly for rapid iteration
  • Scalability: Easy to add more parallel workers as needed

Risk Assessment

  • Medium Risk: Requires careful test isolation to prevent interference
  • Container Resource Contention: Multiple parallel containers may compete for resources
  • Flakiness Potential: Parallel execution can expose race conditions

Testing Plan

  1. Test locally with parallel configuration
  2. Monitor for test flakiness or failures
  3. Gradually increase parallelization level
  4. Measure actual time improvements
  5. Monitor GitLab runner resource usage

Success Criteria

  • CI time reduced from ~22 to ~15 minutes
  • All tests still pass reliably
  • No increase in test flakiness
  • Coverage reporting still works correctly
  • Resource usage remains acceptable

Dependencies

  • Requires Phase 1 completion (duplicate test elimination)
  • GitLab runners must support parallel execution
  • May require runner resource adjustments

References