UNPKG

@wgtechlabs/log-engine

Version:

A lightweight, security-first logging utility with automatic data redaction for Node.js applications - the first logging library with built-in PII protection.

65 lines 1.97 kB
/** * Shared test utilities for redaction tests * Common setup, mocks, and helper functions */ // Mock console methods to capture output for integration tests export const mockConsole = { log: jest.fn(), warn: jest.fn(), error: jest.fn() }; // Store original console methods export const originalConsole = { log: console.log, warn: console.warn, error: console.error }; /** * Setup console mocks for testing */ export function setupConsoleMocks() { jest.clearAllMocks(); console.log = mockConsole.log; console.warn = mockConsole.warn; console.error = mockConsole.error; } /** * Restore original console methods */ export function restoreConsole() { console.log = originalConsole.log; console.warn = originalConsole.warn; console.error = originalConsole.error; } /** * Setup environment for testing */ export function setupTestEnvironment() { const originalEnv = { ...process.env }; return { originalEnv }; } /** * Restore environment after testing */ export function restoreEnvironment(originalEnv) { // Only remove environment variables that were added during the test // (i.e., those not present in the originalEnv) // Create a copy of the keys to avoid mutation during iteration const currentEnvKeys = Object.keys(process.env); for (const key of currentEnvKeys) { if (!Object.prototype.hasOwnProperty.call(originalEnv, key)) { // Safe deletion using explicit type assertion delete process.env[key]; } } // Restore or update variables that existed originally // Iterate over originalEnv keys safely const originalEnvKeys = Object.keys(originalEnv); for (const key of originalEnvKeys) { if (Object.prototype.hasOwnProperty.call(originalEnv, key)) { // Safe assignment using explicit access process.env[key] = originalEnv[key]; } } } //# sourceMappingURL=test-utils.js.map