Aller au contenu principal

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');