Skip to content

Fix JavaScript tests - handle agent output file reading correctly#1882

Merged
pelikhan merged 4 commits intomainfrom
copilot/fix-javascript-tests
Oct 17, 2025
Merged

Fix JavaScript tests - handle agent output file reading correctly#1882
pelikhan merged 4 commits intomainfrom
copilot/fix-javascript-tests

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Oct 17, 2025

Problem

The JavaScript test suite was failing with 113+ test failures across multiple test files. The root cause was a mismatch between how tests were setting up agent output data and how the actual scripts expected to consume it.

The Issue: Tests were setting GITHUB_AW_AGENT_OUTPUT to JSON strings directly:

process.env.GITHUB_AW_AGENT_OUTPUT = JSON.stringify({ items: [...] });

But the scripts expected this to be a file path and would call:

const content = fs.readFileSync(agentOutputFile, "utf8");
const data = JSON.parse(content);

This caused various failures including:

  • "Cannot read properties of undefined (reading 'trim')" errors
  • "Error parsing agent output JSON: Unexpected token 'd', "diff--git"..." errors when mocks returned wrong content
  • Tests expecting certain behavior but getting different error messages

Solution

1. Updated Tests to Use File-Based Approach

Modified tests to use the existing setAgentOutput() helper function that creates actual temporary files:

// Before (wrong)
process.env.GITHUB_AW_AGENT_OUTPUT = JSON.stringify({ items: [...] });

// After (correct)
setAgentOutput({ items: [...] });

This change was applied to:

  • add_comment.test.cjs
  • add_labels.test.cjs
  • create_issue.test.cjs
  • create_pr_review_comment.test.cjs
  • create_code_scanning_alert.test.cjs
  • update_issue.test.cjs
  • push_to_pull_request_branch.test.cjs

2. Fixed Scripts to Handle Empty Content

Added proper empty content checks after reading files in:

  • missing_tool.cjs
  • push_to_pull_request_branch.cjs
if (agentOutput.trim() === "") {
  core.info("No agent output to process");
  // ... handle gracefully
  return;
}

3. Improved Mock Infrastructure

Created mockPatchContent() helper functions that preserve agent output file reading while allowing tests to mock patch file content:

const mockPatchContent = (patchContent) => {
  mockFs.readFileSync.mockImplementation((filepath, encoding) => {
    // Read real temp file for agent output
    if (filepath === process.env.GITHUB_AW_AGENT_OUTPUT) {
      return fs.readFileSync(filepath, encoding || "utf8");
    }
    // Return mocked content for patch files
    return patchContent;
  });
};

4. Fixed Test Infrastructure Issues

  • Fixed variable shadowing in create_code_scanning_alert.test.cjs where securityReportScript was declared twice
  • Updated invalid JSON test to use actual temp file instead of setting env var to invalid string

Results

  • All 534 JavaScript tests now passing (100%)
  • ✅ All Go tests passing
  • ✅ Code formatting and linting validated
  • ✅ Full make agent-finish validation successful
  • ✅ Workflow compilation completed without errors

Testing

Ran the complete test suite:

make test-js  # All 534 tests passing
make agent-finish  # Full validation successful

Fixes #[issue number if applicable]

Original prompt

Fix javascript tests


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 3 commits October 17, 2025 20:25
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix issues in JavaScript tests Fix JavaScript tests - handle agent output file reading correctly Oct 17, 2025
Copilot AI requested a review from pelikhan October 17, 2025 20:37
@pelikhan pelikhan marked this pull request as ready for review October 17, 2025 20:37
@github-actions
Copy link
Copy Markdown
Contributor

Agentic Changeset Generator triggered by this pull request.

@pelikhan pelikhan merged commit 92140b8 into main Oct 17, 2025
6 checks passed
@pelikhan pelikhan deleted the copilot/fix-javascript-tests branch October 17, 2025 20:38
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