Skip to content

Commit 2feaf1b

Browse files
committed
fix: resolve test conflicts with .env environment variables
- 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.
1 parent ebbaff0 commit 2feaf1b

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

jest.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const config: Config = {
1111
moduleDirectories: ['node_modules', 'src'],
1212
preset: 'ts-jest/presets/default-esm',
1313
setupFilesAfterEnv: ['<rootDir>/test/jest-setup.ts'],
14+
globalSetup: '<rootDir>/test/jest-global-setup.ts',
15+
globalTeardown: '<rootDir>/test/jest-global-teardown.ts',
1416
testEnvironment: 'node',
1517
testRegex: ['.*\\.test\\.ts$'],
1618
// Tell Jest to ignore the specific duplicate package.json files

test/jest-global-setup.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
module.exports = async function globalSetup() {
5+
const envPath = path.join(process.cwd(), '.env');
6+
const backupPath = path.join(process.cwd(), '.env.test-backup');
7+
8+
// Backup .env file if it exists and clear related env vars
9+
if (fs.existsSync(envPath)) {
10+
fs.renameSync(envPath, backupPath);
11+
console.log('Backed up .env file for testing');
12+
13+
// Also clear any OCO_ environment variables that might have been loaded
14+
Object.keys(process.env).forEach(key => {
15+
if (key.startsWith('OCO_')) {
16+
delete process.env[key];
17+
}
18+
});
19+
}
20+
}

test/jest-global-teardown.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
module.exports = async function globalTeardown() {
5+
const envPath = path.join(process.cwd(), '.env');
6+
const backupPath = path.join(process.cwd(), '.env.test-backup');
7+
8+
// Restore .env file
9+
if (fs.existsSync(backupPath)) {
10+
fs.renameSync(backupPath, envPath);
11+
console.log('Restored .env file after testing');
12+
}
13+
}

test/unit/config.test.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ describe('config', () => {
1515

1616
function resetEnv(env: NodeJS.ProcessEnv) {
1717
Object.keys(process.env).forEach((key) => {
18-
if (!(key in env)) {
18+
if (key.startsWith('OCO_')) {
19+
// Don't restore OCO_ environment variables to avoid test interference
20+
delete process.env[key];
21+
} else if (!(key in env)) {
1922
delete process.env[key];
2023
} else {
2124
process.env[key] = env[key];
@@ -122,7 +125,7 @@ describe('config', () => {
122125
expect(config.OCO_ONE_LINE_COMMIT).toEqual(false);
123126
expect(config.OCO_OMIT_SCOPE).toEqual(true);
124127
});
125-
128+
126129
it('should handle custom HTTP headers correctly', async () => {
127130
globalConfigFile = await generateConfig('.opencommit', {
128131
OCO_API_CUSTOM_HEADERS: '{"X-Global-Header": "global-value"}'
@@ -139,7 +142,7 @@ describe('config', () => {
139142

140143
expect(config).not.toEqual(null);
141144
expect(config.OCO_API_CUSTOM_HEADERS).toEqual({"Authorization": "Bearer token123", "X-Custom-Header": "test-value"});
142-
145+
143146
// No need to parse JSON again since it's already an object
144147
const parsedHeaders = config.OCO_API_CUSTOM_HEADERS;
145148
expect(parsedHeaders).toHaveProperty('Authorization', 'Bearer token123');

0 commit comments

Comments
 (0)