Skip to main content

withSilentConsole()

withSilentConsole<T>(fn, ...methods): T

Executes a function with console methods silenced, restoring automatically.


Type Parametersโ€‹

T: Tโ€‹


Parametersโ€‹

fn: () => Tโ€‹

Function to execute.

methods: ...ConsoleMethod[]โ€‹

Console methods to silence (defaults to all).


Returns: Tโ€‹

The return value of fn.


Sinceโ€‹

2.0.0


Exampleโ€‹

const result = withSilentConsole(() => {
console.log("silenced");
return computeSomething();
});

const data = await withSilentConsole(async () => {
console.warn("silenced");
return await fetchData();
}, "warn");

Use Casesโ€‹

Wrap noisy functions ๐Ÿ“Œโ€‹

Execute a function with silenced console output, automatically restoring afterwards. Perfect for cleaner syntax without manual restore calls.

await withSilentConsole(async () => {
await component.triggerError();
}, 'error');
// Console automatically restored here

Test error scenarios cleanlyโ€‹

Combine with assertions inside the callback.

await withSilentConsole(async () => {
const result = await failingOperation();
expect(result.success).toBe(false);
expect(result.error).toBeDefined();
}, 'error', 'warn');