Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const config: Config = {
moduleDirectories: ['node_modules', 'src'],
preset: 'ts-jest/presets/default-esm',
setupFilesAfterEnv: ['<rootDir>/test/jest-setup.ts'],
globalSetup: '<rootDir>/test/jest-global-setup.ts',
globalTeardown: '<rootDir>/test/jest-global-teardown.ts',
testEnvironment: 'node',
testRegex: ['.*\\.test\\.ts$'],
// Tell Jest to ignore the specific duplicate package.json files
Expand Down
20 changes: 20 additions & 0 deletions test/jest-global-setup.ts
Original file line number Diff line number Diff line change
@@ -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];
}
});
}
}
13 changes: 13 additions & 0 deletions test/jest-global-teardown.ts
Original file line number Diff line number Diff line change
@@ -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');
}
}
9 changes: 6 additions & 3 deletions test/unit/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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"}'
Expand All @@ -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');
Expand Down