diff --git a/jest.config.ts b/jest.config.ts index 001e0ef9..d0198e35 100644 --- a/jest.config.ts +++ b/jest.config.ts @@ -11,6 +11,8 @@ const config: Config = { moduleDirectories: ['node_modules', 'src'], preset: 'ts-jest/presets/default-esm', setupFilesAfterEnv: ['/test/jest-setup.ts'], + globalSetup: '/test/jest-global-setup.ts', + globalTeardown: '/test/jest-global-teardown.ts', testEnvironment: 'node', testRegex: ['.*\\.test\\.ts$'], // Tell Jest to ignore the specific duplicate package.json files diff --git a/test/jest-global-setup.ts b/test/jest-global-setup.ts new file mode 100644 index 00000000..c0efca91 --- /dev/null +++ b/test/jest-global-setup.ts @@ -0,0 +1,20 @@ +const fs = require('fs'); +const path = require('path'); + +module.exports = async function globalSetup() { + const envPath = path.join(process.cwd(), '.env'); + const backupPath = path.join(process.cwd(), '.env.test-backup'); + + // Backup .env file if it exists and clear related env vars + if (fs.existsSync(envPath)) { + fs.renameSync(envPath, backupPath); + console.log('Backed up .env file for testing'); + + // Also clear any OCO_ environment variables that might have been loaded + Object.keys(process.env).forEach(key => { + if (key.startsWith('OCO_')) { + delete process.env[key]; + } + }); + } +} diff --git a/test/jest-global-teardown.ts b/test/jest-global-teardown.ts new file mode 100644 index 00000000..5f741a48 --- /dev/null +++ b/test/jest-global-teardown.ts @@ -0,0 +1,13 @@ +const fs = require('fs'); +const path = require('path'); + +module.exports = async function globalTeardown() { + const envPath = path.join(process.cwd(), '.env'); + const backupPath = path.join(process.cwd(), '.env.test-backup'); + + // Restore .env file + if (fs.existsSync(backupPath)) { + fs.renameSync(backupPath, envPath); + console.log('Restored .env file after testing'); + } +} diff --git a/test/unit/config.test.ts b/test/unit/config.test.ts index fc4709dc..324f7697 100644 --- a/test/unit/config.test.ts +++ b/test/unit/config.test.ts @@ -15,7 +15,10 @@ describe('config', () => { function resetEnv(env: NodeJS.ProcessEnv) { Object.keys(process.env).forEach((key) => { - if (!(key in env)) { + if (key.startsWith('OCO_')) { + // Don't restore OCO_ environment variables to avoid test interference + delete process.env[key]; + } else if (!(key in env)) { delete process.env[key]; } else { process.env[key] = env[key]; @@ -122,7 +125,7 @@ describe('config', () => { expect(config.OCO_ONE_LINE_COMMIT).toEqual(false); expect(config.OCO_OMIT_SCOPE).toEqual(true); }); - + it('should handle custom HTTP headers correctly', async () => { globalConfigFile = await generateConfig('.opencommit', { OCO_API_CUSTOM_HEADERS: '{"X-Global-Header": "global-value"}' @@ -139,7 +142,7 @@ describe('config', () => { expect(config).not.toEqual(null); expect(config.OCO_API_CUSTOM_HEADERS).toEqual({"Authorization": "Bearer token123", "X-Custom-Header": "test-value"}); - + // No need to parse JSON again since it's already an object const parsedHeaders = config.OCO_API_CUSTOM_HEADERS; expect(parsedHeaders).toHaveProperty('Authorization', 'Bearer token123');