Vitest Fail on Console Utility
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.
Common errors
-
TypeError: default is not a function
cause Attempting to import `failOnConsole` as a named export (e.g., `{ failOnConsole }`) or using CommonJS `require()` when the package is primarily an ESM default export.fixChange your import statement to `import failOnConsole from 'vitest-fail-on-console';`. -
Cannot find module 'vitest-fail-on-console' or its corresponding type declarations.
cause Module resolution failure due to incorrect `tsconfig.json` settings, outdated package version, or improper `package.json` exports configuration on older environments.fixUpdate `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]
cause 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.fixIf 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.
Warnings
- gotcha When 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.
- gotcha Older 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.
- gotcha This 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.
- gotcha By 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.
- gotcha Version 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.
Install
-
npm install vitest-fail-on-console -
yarn add vitest-fail-on-console -
pnpm add vitest-fail-on-console
Imports
- failOnConsole
import { failOnConsole } from 'vitest-fail-on-console'; const failOnConsole = require('vitest-fail-on-console');import failOnConsole from 'vitest-fail-on-console';
Quickstart
/* 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;
}
});