bit icon indicating copy to clipboard operation
bit copied to clipboard

Early Return in Tester Aspect Silently Bypasses JUnit XML Report Generation

Open codeCraft-Ritik opened this issue 2 months ago • 0 comments

Issue : Early Return in Tester Aspect Silently Bypasses JUnit XML Report Generation

Metadata

  • Subsystem: scopes/defender/tester
  • Source File: scopes/defender/tester/tester.main.runtime.ts
  • Line Numbers: 161–173
  • Severity: High
  • Keywords: tester, junit, ci-cd, early-return, test-reporting

Problem Description

The bit test command supports testing specific environments using the --env <env-id> flag and outputting test results to an XML file using the --junit <filepath> flag.

In TesterMain.test(), when opts?.env is specified, the method immediately returns the execution promise from envsRuntime.runEnv(). As a result, the code block responsible for invoking generateJUnit() is completely bypassed.

Vulnerable Code

// scopes/defender/tester/tester.main.runtime.ts: Lines 161-172

  async test(components: Component[], opts?: TesterOptions): Promise<TestResults> {
    const options = this.getOptions(opts);
    const envsRuntime = await this.envs.createEnvironment(components);
    if (opts?.env) {
      return envsRuntime.runEnv(opts.env, this.service, options); // <-- BUG: Returns before JUnit generation!
    }
    const results = await envsRuntime.run(this.service, options);
    if (opts?.junit) {
      await this.generateJUnit(opts?.junit, results);
    }
    return results;
  }

Root Cause Analysis

  1. The method checks if (opts?.env) and returns immediately at line 165.
  2. The JUnit XML generation logic resides at lines 168–170, situated after the envsRuntime.run() call.
  3. Because line 165 executes a return, lines 167–171 are unreachable whenever opts?.env is present.

Real-World Impact

  • In CI/CD pipelines (GitHub Actions, CircleCI, GitLab, Jenkins), large monorepos frequently split test execution across multiple matrix jobs by environment:
    bit test --env teambit.react/react --junit test-results/react-junit.xml
    bit test --env teambit.node/node --junit test-results/node-junit.xml
    
  • Because of this bug, bit test runs the tests, prints console output, but silently never creates the JUnit XML report file.
  • CI workflow steps configured to parse and publish JUnit test results immediately crash with file-not-found errors or report 0 tests executed, breaking automated quality gates and test reporting dashboards.

Proposed Fix

Await the test results conditionally, then proceed to JUnit generation before returning:

--- a/scopes/defender/tester/tester.main.runtime.ts
+++ b/scopes/defender/tester/tester.main.runtime.ts
@@ -162,10 +162,9 @@ export class TesterMain {
   async test(components: Component[], opts?: TesterOptions): Promise<TestResults> {
     const options = this.getOptions(opts);
     const envsRuntime = await this.envs.createEnvironment(components);
-    if (opts?.env) {
-      return envsRuntime.runEnv(opts.env, this.service, options);
-    }
-    const results = await envsRuntime.run(this.service, options);
+    const results = opts?.env
+      ? await envsRuntime.runEnv(opts.env, this.service, options)
+      : await envsRuntime.run(this.service, options);
     if (opts?.junit) {
       await this.generateJUnit(opts?.junit, results);
     }

Verification & Unit Test

Add a test in tester.spec.ts:

it('should generate junit xml when both env and junit options are provided', async () => {
  const junitPath = path.join(tmpDir, 'results.xml');
  await tester.test(components, { env: 'teambit.react/react', junit: junitPath });
  expect(fs.existsSync(junitPath)).to.be.true;
  const content = fs.readFileSync(junitPath, 'utf8');
  expect(content).to.include('<testsuites');
});

codeCraft-Ritik avatar Sep 23 '26 03:09 codeCraft-Ritik