Skip to main content

silenceConsole()

silenceConsole(...methods): () => void

Silences specified console methods for the duration of a test.


Parametersโ€‹

methods: ...ConsoleMethod[]โ€‹

Console methods to silence (defaults to all).


Returnsโ€‹

The restore function.

(): void

Returns: voidโ€‹


Sinceโ€‹

2.0.0


Exampleโ€‹

const restore = silenceConsole("log", "warn");
console.log("silenced");
console.error("still prints");
restore();

Use Casesโ€‹

Mute expected errors ๐Ÿ“Œโ€‹

Prevent specific console methods from printing during a test block. Useful when testing error boundaries that naturally log errors.

const restore = silenceConsole('error');

// This error is expected and handled
renderWithError(<BrokenComponent />);

restore();

Clean test outputโ€‹

Keep test reports readable by silencing verbose debug logs.

const restore = silenceConsole('log', 'debug');

runVerboseOperation();

restore();