Registry / testing / vitest-fail-on-console

vitest-fail-on-console

JSON →
library0.10.1jsnpmunverified

vitest-fail-on-console is a utility library for Vitest that enhances test reliability by automatically failing tests that emit `console.error()` or `console.warn()` messages. This prevents critical debugging information from being overlooked in large test suites and ensures that all console outputs are intentional or properly handled. Currently at version 0.10.1, the library maintains an active development pace with several releases occurring within a few months, indicating consistent updates and bug fixes. Unlike its Jest counterpart, `jest-fail-on-console`, this library is specifically refactored and typed with TypeScript for the Vitest ecosystem, providing robust type safety and a streamlined developer experience. It offers granular control over which console methods trigger failures and allows for custom messaging and conditional message allowances, ensuring flexibility for expected console outputs while enforcing strict logging discipline.

npm install vitest-fail-on-console
INSTALL
IMPORT
SIG · VITEST-FAIL-ON-CON
V
vitest-fail-on-console
testingjavascriptv0.10.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

failOnConsole
import failOnConsole from 'vitest-fail-on-console';
import { failOnConsole } from 'vitest-fail-on-console'; const failOnConsole = require('vitest-fail-on-console');
The utility is exported as a default function. Use standard ESM default import syntax in Vitest setup files.

Demonstrates how to integrate `vitest-fail-on-console` into a Vitest project using `setupFiles` in `vitest.config.ts` to automatically fail tests on specified console outputs, including basic configuration and custom message allowance.

/* vitest.config.ts */ import { defineConfig } from "vitest/config"; export default defineConfig({ test: { environment: "node", setupFiles: ["./tests/setup.ts"], }, }); /* tests/setup.ts */ import failOnConsole from 'vitest-fail-on-console'; // Enable failure on console.error and console.warn by default failOnConsole(); // Or with custom options, e.g., to only fail on errors and allow specific warnings failOnConsole({ shouldFailOnWarn: false, // Don't fail on warnings allowMessage: (message, methodName) => { if (methodName === 'warn' && message.includes('Expected warning')) { return true; // Allow specific warning messages to pass without failing the test } return false; } });
Debug
Known issues
gotchaWhen migrating or upgrading, users might encounter 'TypeError: default is not a function' if they attempt to import `failOnConsole` as a named export or use CommonJS `require()` syntax. The library is primarily designed for ESM default imports.
fix
Ensure you are using `import failOnConsole from 'vitest-fail-on-console';` in your setup file. If using older module systems, consult the `package.json` 'main' or 'exports' fields for compatibility.
affects: >=0.7.1
gotchaOlder versions (0.6.1, 0.6.2) experienced 'Cannot find module 'vitest-fail-on-console'' errors, indicating module resolution issues. This could recur with misconfigured `tsconfig.json` or bundlers.
fix
Update to the latest version of `vitest-fail-on-console`. Verify your `tsconfig.json` `moduleResolution` and `module` settings are compatible with Vitest's ESM-first approach, typically using 'bundler' or 'nodenext'.
affects: 0.6.1, 0.6.2
gotchaThis utility requires specific peer dependencies including `@vitest/utils`, `vite`, and `vitest` at minimum specified versions. Incompatible versions can lead to runtime errors or unexpected behavior.
fix
Ensure that your project's `devDependencies` for `@vitest/utils`, `vite`, and `vitest` meet or exceed the versions specified in `vitest-fail-on-console`'s peer dependencies (e.g., `"@vitest/utils": ">=0.26.2"`).
affects: >=0.1.0
gotchaBy default, `console.error()` and `console.warn()` will fail tests. If these are expected outputs, tests will fail unless explicitly mocked, spied upon, or allowed via options.
fix
For expected outputs, use `vi.spyOn(console, 'error').mockImplementation(() => {})` and assert its calls, or utilize the `allowMessage` option to permit specific console messages to pass without failing the test.
affects: >=0.1.0
gotchaVersion 0.6.0 included a security update to package dependencies. While no specific CVE was detailed, it highlights the importance of keeping the utility updated to benefit from security patches in underlying packages.
fix
Regularly update `vitest-fail-on-console` and its related peer dependencies to their latest stable versions to ensure you receive critical fixes and security enhancements.
affects: >=0.6.0
Errors
Common errors & fixes
TypeError: default is not a function
Attempting to import `failOnConsole` as a named export (e.g., `{ failOnConsole }`) or using CommonJS `require()` when the package is primarily an ESM default export.
fix
Change your import statement to `import failOnConsole from 'vitest-fail-on-console';`.
Cannot find module 'vitest-fail-on-console' or its corresponding type declarations.
Module resolution failure due to incorrect `tsconfig.json` settings, outdated package version, or improper `package.json` exports configuration on older environments.
fix
Update `vitest-fail-on-console` to the latest version. Verify `tsconfig.json` includes `"moduleResolution": "bundler"` or `"nodenext"` and `"module": "esnext"` (or similar for Vitest compatibility).
Test failed due to console.error being called: [your error message here]
A test executed code that resulted in `console.error()` (or `console.warn()`, `console.log()` etc., depending on configuration) being called, and this output was not explicitly allowed or mocked.
fix
If the message is expected, use `vi.spyOn(console, 'error').mockImplementation(() => {})` and `expect(console.error).toHaveBeenCalledWith(...)` within your test, or configure the `allowMessage` option in `failOnConsole` to permit that specific message.
Upgrade
Version history
0.10.1latest on npm
Audit
Dependencies
@vitest/utilsrequiredCore utilities for Vitest integration
viterequiredVitest's underlying build tool dependency
vitestrequiredThe primary testing framework this utility extends
Agent activity
11 hits · last 30 days
node
10
Resources
vitest-fail-on-console — npm install vitest-fail-on-console · libregistry