webssh2-server
Version:
A Websocket to SSH2 gateway using xterm.js, socket.io, ssh2
58 lines (51 loc) • 1.48 kB
JavaScript
// server
// tests/test-helpers.js
/**
* Global test helper utilities for environment cleanup
*/
/**
* Clean up all WEBSSH2_ and PORT environment variables
* Should be called in beforeEach/afterEach hooks across all test files
*/
export function cleanupEnvironmentVariables() {
Object.keys(process.env).forEach(key => {
if (key.startsWith('WEBSSH2_') || key === 'PORT') {
delete process.env[key]
}
})
}
/**
* Global cleanup function for test file module imports
*/
function cleanupEnvironmentVariablesGlobal() {
cleanupEnvironmentVariables()
}
// Global cleanup - runs when module is imported
cleanupEnvironmentVariablesGlobal()
/**
* Store current environment variables for later restoration
* @returns {Object} Map of environment variables to restore
*/
export function storeEnvironmentVariables() {
const originalEnv = {}
Object.keys(process.env).forEach(key => {
if (key.startsWith('WEBSSH2_') || key === 'PORT') {
originalEnv[key] = process.env[key]
}
})
return originalEnv
}
/**
* Restore environment variables from stored state
* @param {Object} originalEnv - Map of environment variables to restore
*/
export function restoreEnvironmentVariables(originalEnv) {
// First clean up current env vars
cleanupEnvironmentVariables()
// Then restore original values
Object.keys(originalEnv).forEach(key => {
if (originalEnv[key] !== undefined) {
process.env[key] = originalEnv[key]
}
})
}