Skip to content

[chore]: configure test coverage with env var#1730

Open
seanmcguire12 wants to merge 3 commits intomainfrom
use-env-var-for-coverage
Open

[chore]: configure test coverage with env var#1730
seanmcguire12 wants to merge 3 commits intomainfrom
use-env-var-for-coverage

Conversation

@seanmcguire12
Copy link
Member

@seanmcguire12 seanmcguire12 commented Feb 22, 2026

why

  • to make test coverage outputs configurable via an environment variable so that you can run tests locally without clogging up storage unnecessarily

what changed

  • moved packages/core/scripts/test-utils.ts to th workspace root level scripts/test-utils.ts so that utils can be shared across other test scripts (test-core.ts, test-e2e.ts, test-server.ts, test-evals.ts)
  • added scripts/test-artifacts.ts which handles conditionally writing test artifacts based on the TEST_COVERAGE env var

Summary by cubic

Make test coverage and JUnit/CTRF artifact generation opt-in via the TEST_COVERAGE env var to avoid writing artifacts during local runs. CI sets TEST_COVERAGE=1 to keep coverage and reports in pipelines.

  • New Features

    • Added scripts/test-artifacts.ts with helpers to conditionally create coverage dirs, JUnit paths, and env (NODE_V8_COVERAGE, CTRF_JUNIT_PATH), and to unset them when disabled.
    • Updated core, e2e, evals, and server test scripts to only set reporters/paths and write coverage/JUnit/CTRF when TEST_COVERAGE is enabled; env vars are ignored when no path is provided.
    • Moved test-utils to workspace root for sharing; updated imports accordingly.
    • CI config sets TEST_COVERAGE=1; removed hardcoded NODE_V8_COVERAGE in evals job.
    • Excluded scripts from package tsconfigs.
  • Migration

    • To generate coverage locally: export TEST_COVERAGE=1 (or true) before running test scripts.

Written for commit 9852ae8. Summary will update on new commits. Review in cubic

@changeset-bot
Copy link

changeset-bot bot commented Feb 22, 2026

⚠️ No Changeset found

Latest commit: 9852ae8

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@seanmcguire12
Copy link
Member Author

@cubic-dev-ai

@cubic-dev-ai
Copy link
Contributor

cubic-dev-ai bot commented Feb 22, 2026

@cubic-dev-ai

@seanmcguire12 I have started the AI code review. It will take a few minutes to complete.

Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 9 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.
Architecture diagram
sequenceDiagram
    participant CI as CI / Developer
    participant Script as Test Script (Core/E2E/Server)
    participant Utils as NEW: test-artifacts.ts
    participant FS as File System
    participant Runner as Test Runner (Vitest/Playwright/Node)

    CI->>Script: Run test command (e.g. pnpm test:core)
    
    Script->>Utils: isTestCoverageEnabled()
    Note over Utils: Checks TEST_COVERAGE env var
    Utils-->>Script: boolean

    alt TEST_COVERAGE is enabled (CI or Local Opt-in)
        Script->>Utils: NEW: initCoverageDir(path)
        Utils->>FS: CHANGED: mkdirSync (recursive)
        Utils-->>Script: coveragePath

        Script->>Utils: NEW: initJunitPath(path)
        Utils->>FS: CHANGED: mkdirSync (parent dir)
        Utils-->>Script: junitPath

        Script->>Utils: NEW: withCoverageEnv(env, coveragePath)
        Utils-->>Script: Updated Env (NODE_V8_COVERAGE)
        
        Script->>Runner: spawnSync (with reporter + coverage flags)
        Runner->>FS: Write JUnit XML & Coverage data
        Runner-->>Script: Exit Code
        
        Script->>Utils: NEW: maybeWriteCtrf(junitPath, writer)
        Utils->>FS: Convert JUnit to CTRF JSON
    else TEST_COVERAGE is disabled (Default Local)
        Script->>Utils: NEW: initCoverageDir(path)
        Utils-->>Script: null
        
        Script->>Utils: NEW: initJunitPath(path)
        Utils-->>Script: null

        Script->>Runner: CHANGED: spawnSync (minimal flags, no coverage env)
        Note right of Runner: No artifacts generated
        Runner-->>Script: Exit Code
    end

    Script-->>CI: Return Exit Status
Loading

@seanmcguire12 seanmcguire12 marked this pull request as ready for review February 22, 2026 18:26
@greptile-apps
Copy link
Contributor

greptile-apps bot commented Feb 22, 2026

Greptile Summary

This PR makes test coverage and artifact generation opt-in via the TEST_COVERAGE environment variable. Locally, tests no longer write coverage directories or JUnit/CTRF reports by default, preventing unnecessary storage usage. CI sets TEST_COVERAGE=1 to maintain all reporting.

Key changes:

  • Created scripts/test-artifacts.ts with helpers (initCoverageDir, initJunitPath, withCoverageEnv, etc.) that check TEST_COVERAGE env var
  • Moved test-utils.ts from packages/core/scripts/ to workspace root scripts/ for shared access
  • Updated all test scripts (test-core.ts, test-e2e.ts, test-evals.ts, test-server.ts) to use conditional artifact helpers
  • CI workflow now sets TEST_COVERAGE=1 globally; removed redundant NODE_V8_COVERAGE from evals job
  • Excluded scripts directories from package tsconfig.json files

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • Clean refactoring that centralizes artifact generation logic with proper conditionals. All test scripts consistently updated to use the new helpers. CI configuration properly sets TEST_COVERAGE=1 to preserve existing behavior in pipelines.
  • No files require special attention

Important Files Changed

Filename Overview
scripts/test-artifacts.ts New utility module providing helpers to conditionally enable test coverage and artifact generation based on TEST_COVERAGE env var
packages/core/scripts/test-core.ts Updated to use test-artifacts helpers, making coverage and JUnit output conditional on TEST_COVERAGE env var
packages/core/scripts/test-e2e.ts Updated to use test-artifacts helpers, making coverage and CTRF output conditional on TEST_COVERAGE env var
packages/evals/scripts/test-evals.ts Updated to use test-artifacts helpers, making coverage and JUnit/CTRF output conditional on TEST_COVERAGE env var
packages/server/scripts/test-server.ts Updated to use test-artifacts helpers, making coverage and JUnit output conditional on TEST_COVERAGE env var
.github/workflows/ci.yml Added TEST_COVERAGE=1 to global env vars and removed hardcoded NODE_V8_COVERAGE from evals job

Last reviewed commit: 0d969af

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

9 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 9 files

Confidence score: 4/5

  • There is a medium-severity issue where NODE_V8_COVERAGE/CTRF_JUNIT_PATH can leak into the child process even when TEST_COVERAGE is off, potentially bypassing the opt-in gate in packages/core/scripts/test-e2e.ts.
  • Impact seems limited to test environment configuration rather than production behavior, so overall risk looks low-to-moderate and likely safe to merge with awareness.
  • Pay close attention to packages/core/scripts/test-e2e.ts - ensure coverage-related env vars are only set when explicitly enabled.
Prompt for AI agents (all issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="packages/core/scripts/test-e2e.ts">

<violation number="1" location="packages/core/scripts/test-e2e.ts:162">
P2: Environment variables `NODE_V8_COVERAGE` and `CTRF_JUNIT_PATH` leak from the parent process when `TEST_COVERAGE` is not enabled, bypassing the opt-in gate. When `coverageDir`/`ctrfPath` are `null`, `withCoverageEnv`/`withCtrfEnv` return the env unchanged, so any pre-existing values from `process.env` are passed to the child. Consider explicitly deleting these keys when coverage is disabled, e.g. `{ ...env, NODE_V8_COVERAGE: undefined }`.</violation>
</file>
Architecture diagram
sequenceDiagram
    participant CI as CI / Developer
    participant Script as Test Script (Core/E2E/Server)
    participant Utils as NEW: test-artifacts.ts
    participant FS as File System
    participant Runner as Test Runner (Vitest/Playwright/Node)

    CI->>Script: Run test command (e.g. pnpm test:core)
    
    Script->>Utils: isTestCoverageEnabled()
    Note over Utils: Checks TEST_COVERAGE env var
    Utils-->>Script: boolean

    alt TEST_COVERAGE is enabled (CI or Local Opt-in)
        Script->>Utils: NEW: initCoverageDir(path)
        Utils->>FS: CHANGED: mkdirSync (recursive)
        Utils-->>Script: coveragePath

        Script->>Utils: NEW: initJunitPath(path)
        Utils->>FS: CHANGED: mkdirSync (parent dir)
        Utils-->>Script: junitPath

        Script->>Utils: NEW: withCoverageEnv(env, coveragePath)
        Utils-->>Script: Updated Env (NODE_V8_COVERAGE)
        
        Script->>Runner: spawnSync (with reporter + coverage flags)
        Runner->>FS: Write JUnit XML & Coverage data
        Runner-->>Script: Exit Code
        
        Script->>Utils: NEW: maybeWriteCtrf(junitPath, writer)
        Utils->>FS: Convert JUnit to CTRF JSON
    else TEST_COVERAGE is disabled (Default Local)
        Script->>Utils: NEW: initCoverageDir(path)
        Utils-->>Script: null
        
        Script->>Utils: NEW: initJunitPath(path)
        Utils-->>Script: null

        Script->>Runner: CHANGED: spawnSync (minimal flags, no coverage env)
        Note right of Runner: No artifacts generated
        Runner-->>Script: Exit Code
    end

    Script-->>CI: Return Exit Status
Loading

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

},
"include": ["**/*.ts", "lib/v3/cli.js"],
"exclude": ["node_modules", "dist"]
"exclude": ["node_modules", "dist", "scripts"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why exclude scripts?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the scripts all use tsx

@pirate
Copy link
Member

pirate commented Feb 23, 2026

moved packages/core/scripts/test-utils.ts to th workspace root level scripts/test-utils.ts so that utils can be shared across other test scripts (test-core.ts, test-e2e.ts, test-server.ts, test-evals.ts)

I dont think this is a good idea, it significantly complicates the build because it introduces a cross-repo dependency at the tsx level, it's not worth it for simple helpers.

@pirate
Copy link
Member

pirate commented Feb 23, 2026

here this is a simpler way to accomplish the same thing without branching in all the test scripts: #1733

@seanmcguire12
Copy link
Member Author

seanmcguire12 commented Feb 23, 2026

here this is a simpler way to accomplish the same thing without branching in all the test scripts: #1733

yeah i was intentionally shooting for "don't write in the first place" behaviour, instead of "write, then remove"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants