Skip to content

Commit 822b030

Browse files
committed
Prevent API token env vars from polluting test snapshots
Add detection and automatic clearing of all Socket API token environment variable aliases (SOCKET_CLI_API_KEY, SOCKET_CLI_API_TOKEN, SOCKET_SECURITY_API_KEY, SOCKET_SECURITY_API_TOKEN) in test-wrapper to ensure consistent snapshot testing across different environments.
1 parent 1d64043 commit 822b030

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

scripts/test-wrapper.mjs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,40 @@ async function main() {
6262
args = args.slice(1)
6363
}
6464

65+
// Check for and warn about environment variables that can cause snapshot mismatches.
66+
// These are all aliases for the Socket API token that should not be set during tests.
67+
const problematicEnvVars = [
68+
'SOCKET_CLI_API_KEY',
69+
'SOCKET_CLI_API_TOKEN',
70+
'SOCKET_SECURITY_API_KEY',
71+
'SOCKET_SECURITY_API_TOKEN',
72+
]
73+
const foundEnvVars = problematicEnvVars.filter(v => process.env[v])
74+
if (foundEnvVars.length > 0) {
75+
logger.warn(
76+
`Detected environment variable(s) that may cause snapshot test failures: ${foundEnvVars.join(', ')}`,
77+
)
78+
logger.warn(
79+
'These will be cleared for the test run to ensure consistent snapshots.',
80+
)
81+
logger.warn(
82+
'Tests use .env.test configuration which should not include real API tokens.',
83+
)
84+
}
85+
6586
const spawnEnv = {
6687
...process.env,
6788
// Increase Node.js heap size to prevent out of memory errors.
6889
// Use 8GB in CI, 4GB locally.
6990
// Add --max-semi-space-size for better GC with RegExp-heavy tests.
7091
NODE_OPTIONS:
7192
`${process.env.NODE_OPTIONS || ''} --max-old-space-size=${process.env.CI ? 8192 : 4096} --max-semi-space-size=512`.trim(),
93+
// Clear problematic environment variables that cause snapshot mismatches.
94+
// Tests should use .env.test configuration instead.
95+
SOCKET_CLI_API_KEY: undefined,
96+
SOCKET_CLI_API_TOKEN: undefined,
97+
SOCKET_SECURITY_API_KEY: undefined,
98+
SOCKET_SECURITY_API_TOKEN: undefined,
7299
}
73100

74101
// Handle Windows vs Unix for vitest executable.

0 commit comments

Comments
 (0)