Skip to content

fix: resolve test conflicts with .env environment variables - #526

Open
cometjc wants to merge 1 commit into
di-sukharev:masterfrom
cometjc:fix/test-env-conflict
Open

fix: resolve test conflicts with .env environment variables#526
cometjc wants to merge 1 commit into
di-sukharev:masterfrom
cometjc:fix/test-env-conflict

Conversation

@cometjc

@cometjc cometjc commented Oct 31, 2025

Copy link
Copy Markdown
Contributor

This PR fixes test failures caused by environment variables from the user's .env file interfering with test expectations.

Changes Made

  • Jest Global Hooks: Added globalSetup and globalTeardown to backup and restore the .env file during testing
  • Environment Variable Cleanup: Clear OCO_ environment variables during test execution to prevent interference
  • Test Environment Reset: Modified the test's resetEnv function to avoid restoring OCO_ environment variables

Problem Solved

Previously, when users had OCO_ environment variables set in their .env file (like OCO_MODEL=gpt-5-mini, OCO_TOKENS_MAX_OUTPUT=8192), these would override the expected default values in tests, causing failures.

Solution

The fix ensures that:

  1. The .env file is temporarily backed up during test runs
  2. OCO_ environment variables are cleared from process.env
  3. Tests run with clean, predictable defaults
  4. The .env file is restored after testing

This maintains backward compatibility while ensuring reliable test execution.

- Add Jest global setup/teardown hooks to backup and restore .env file
- Clear OCO_ environment variables during test execution
- Modify test resetEnv function to prevent OCO_ env vars from interfering
- Prevent .env file from interfering with test expectations

This fixes the issue where tests were failing due to environment variables
from the user's .env file overriding the expected default config values.

@di-sukharev di-sukharev left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

The test reproducibility problem is real, but moving the actual .env file from global setup is unsafe. Please isolate the tests through temporary paths or explicit environment values without modifying the user file.

Comment thread test/jest-global-setup.ts

// Backup .env file if it exists and clear related env vars
if (fs.existsSync(envPath)) {
fs.renameSync(envPath, backupPath);

@di-sukharev di-sukharev Aug 20, 2026

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Do not rename the working .env file. If Jest crashes or is killed before globalTeardown, the file remains at .env.test-backup, and concurrent runs will race. The tests already support temporary globalPath and envPath values; clear OCO_* variables in setup and create fixtures in a temporary directory without touching the developer file.

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