{"id":12386,"library":"vitest-fail-on-console","title":"Vitest Fail on Console Utility","description":"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.","status":"active","version":"0.10.1","language":"javascript","source_language":"en","source_url":"https://github.com/thomasbrodusch/vitest-fail-on-console","tags":["javascript","vitest","testing","utility","console-warning","typescript"],"install":[{"cmd":"npm install vitest-fail-on-console","lang":"bash","label":"npm"},{"cmd":"yarn add vitest-fail-on-console","lang":"bash","label":"yarn"},{"cmd":"pnpm add vitest-fail-on-console","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Core utilities for Vitest integration","package":"@vitest/utils","optional":false},{"reason":"Vitest's underlying build tool dependency","package":"vite","optional":false},{"reason":"The primary testing framework this utility extends","package":"vitest","optional":false}],"imports":[{"note":"The utility is exported as a default function. Use standard ESM default import syntax in Vitest setup files.","wrong":"import { failOnConsole } from 'vitest-fail-on-console';\nconst failOnConsole = require('vitest-fail-on-console');","symbol":"failOnConsole","correct":"import failOnConsole from 'vitest-fail-on-console';"}],"quickstart":{"code":"/* vitest.config.ts */\nimport { defineConfig } from \"vitest/config\";\nexport default defineConfig({\n    test: {\n        environment: \"node\",\n        setupFiles: [\"./tests/setup.ts\"],\n    },\n});\n\n/* tests/setup.ts */\nimport failOnConsole from 'vitest-fail-on-console';\n\n// Enable failure on console.error and console.warn by default\nfailOnConsole();\n\n// Or with custom options, e.g., to only fail on errors and allow specific warnings\nfailOnConsole({\n  shouldFailOnWarn: false, // Don't fail on warnings\n  allowMessage: (message, methodName) => {\n    if (methodName === 'warn' && message.includes('Expected warning')) {\n      return true; // Allow specific warning messages to pass without failing the test\n    }\n    return false;\n  }\n});","lang":"typescript","description":"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."},"warnings":[{"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.","message":"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.","severity":"gotcha","affected_versions":">=0.7.1"},{"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'.","message":"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.","severity":"gotcha","affected_versions":"0.6.1, 0.6.2"},{"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\"`).","message":"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.","severity":"gotcha","affected_versions":">=0.1.0"},{"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.","message":"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.","severity":"gotcha","affected_versions":">=0.1.0"},{"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.","message":"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.","severity":"gotcha","affected_versions":">=0.6.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Change your import statement to `import failOnConsole from 'vitest-fail-on-console';`.","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.","error":"TypeError: default is not a function"},{"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).","cause":"Module resolution failure due to incorrect `tsconfig.json` settings, outdated package version, or improper `package.json` exports configuration on older environments.","error":"Cannot find module 'vitest-fail-on-console' or its corresponding type declarations."},{"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.","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.","error":"Test failed due to console.error being called: [your error message here]"}],"ecosystem":"npm"}