Skip to content

perf(cdk): optimize Jest test suite via shared CDK template synthesis #194

@scottschreckengaust

Description

@scottschreckengaust

Problem

The CDK Jest test suite takes ~6 minutes (371s) because construct tests re-synthesize CDK templates on every test() block — even when the configuration is identical across tests.

Profiling revealed that task-api.test.ts alone calls Template.fromStack() 41 times (370s wall clock), with each synth taking 5-10s for the TaskApi construct (WAF, Cognito, Lambda, API Gateway).

Solution

Share synthesized Template objects via beforeAll within each describe block. Since CDK Template is read-only (assertions don't mutate), tests with identical input can safely reuse a single template.

Results

Metric Before After Improvement
Total Jest time 371s 145s 61% faster
task-api.test.ts 370s 89s 76% faster
CDK synths in file 41 11 73% fewer

Acceptance criteria

  • Refactor task-api.test.ts to use beforeAll shared templates
  • All 1789 tests pass (0 failures)
  • All 40 tests in task-api.test.ts preserve identical assertions
  • (Follow-up) Apply same pattern to task-orchestrator.test.ts (33 synths, 119s)
  • (Follow-up) Apply same pattern to ecs-agent-cluster.test.ts (11 synths, 34s)

PR

#195

Metadata

Metadata

Assignees

No one assigned

    Labels

    approvedWhen an issue has been approved and readyenhancementNew feature or request

    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