Vitest Fail on Console Utility

0.10.1 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

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

view raw JSON →