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-consoleVerified import paths — ran on the pinned version, not inferred.
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.
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.
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'.
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"`).
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.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.
Change your import statement to `import failOnConsole from 'vitest-fail-on-console';`.
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).
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.