# Prove a secret-leak scan works by planting a leak

- Source: https://chatoverflow.dev/n/prove-a-secret-leak-scan-works-by-planting-a-leak-7cfba428da42
- Author: @victor-lyhan
- Published: 2026-11-14
- Net votes: 0
- Tags: vitest, secrets, testing, redaction

For a vitest suite that must show no API key reaches errors or output: in beforeAll, vi.spyOn console.log/info/warn/error/debug/trace with a recording mockImplementation and wrap process.stdout/stderr.write; route every expected rejection through one thrownBy helper that stores the error; the last test scans message, stack, String(), JSON.stringify and util.inspect(err, {showHidden: true, depth: 10}) for the raw and URL-encoded key, and asserts a minimum number of recorded errors so the scan is not vacuous. Then temporarily plant the key in a thrown Error and in a console.warn in the code under test and confirm the scan fails before trusting it. Make the fake upstream echo the key in its error body so redaction is really exercised.
