Package detail

console-fail-test

JoshuaKGoldberg74.5kMIT0.6.1

Gently fails test runs if the console was used during them. πŸ“’

readme

console-fail-test

Gently fails test runs if the console was used during them. πŸ“’

πŸ‘ͺ All Contributors: 6 🀝 Code of Conduct: Kept πŸ§ͺ Coverage πŸ“ License: MIT πŸ“¦ npm version πŸ’ͺ TypeScript: Strict

Why?

Logging to the console during tests can be a sign of:

  • 🚫 warnings from third-party libraries such as React for improper usage
  • πŸ€• temporary code that shouldn't be checked into your project
  • πŸ“’ unnecessary spam in your tests window

This little library throws an error after each test if a console method was called during it. It's got some nifty features:

  • πŸ“Š Summary of which methods are called with calling arguments
  • πŸ›« Failures are thrown after tests finish, so your tests will fail normally if they should
stdout | src/index.test.ts > index > example test that console.logs
Whoops!

 ❯ src/index.test.ts (4)
   ❯ index (4)
     Γ— example test that console.logs
       β ™ [ afterEach ]
     βœ“ example test that does not console.log

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯- Failed Tests 1 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯-

 FAIL  src/index.test.ts > index > example test that console.logs
Error: Oh no! Your test called the following console method:
  * log (1 call)
    > Call 0: "Whoops!"

Usage

console-fail-test is meant to support any (test framework) & (spy library) combination. It will auto-detect your combination if possible and use the most appropriate environment hooks and function spies it can find.

For example, in a Jest config:

// jest.config.js
module.exports = {
    setupFilesAfterEnv: ["console-fail-test/setup"],
};

Prior to console-fail-test@0.6.0, this was setup.cjs or setup.mjs.

Test Frameworks

See the Documentation link for each supported framework for how to set up console-fail-test with that framework.

Framework Support? API Request Documentation
Ava βœ…οΈ require("ava") Ava.md
Mocha βœ…οΈ ✨ "mocha" Mocha.md
Jasmine βœ…οΈ ✨ "jasmine" Jasmine.md
Jest βœ…οΈ ✨ "jest" Jest.md
lab βœ… exports.lab Lab.md
node-tap βœ…οΈ require("node-tap") NodeTap.md
Vitest βœ…οΈ ✨ "vitest" Vitest.md
Cypress βš™οΈ /issues/199
QUnit βš™οΈ /issues/19
Playwright βš™οΈ /issues/198
tape βš™οΈ /issues/17
TestCafe βš™οΈ /issues/15

Spy Libraries

If your test framework provides its own spy library, console-fail-test will by default use that library. If a supported spy library isn't detected, an internal fallback will be used to spy on console methods.

You can request a specific test library using the Node API with its API request:

require("console-fail-test").cft({
    spyLibrary: "sinon",
});
Library Support? API Request Spy Documentation
Jasmine βœ…οΈ "jasmine" jasmine.createSpy() Jasmine.md#spies
Jest βœ…οΈ "jest" jest.fn() Jest.md#spies
Sinon βœ…οΈ require("sinon") sinon.spy() Sinon.md#spies
Vitest βœ…οΈ "vitest" vi.fn() Vitest.md#spies

Ignoring console methods

By default, console-fail-test will error on any called console method. If you'd like allow certain methods, pass a console object to the cft API when you set it up:

require("console-fail-test").cft({
    console: {
        warn: true, // won't error on any instance of console.warn
    },
});

Development

See .github/CONTRIBUTING.md, then .github/DEVELOPMENT.md. Thanks! πŸ“’

Contributors

Colin MacKenzie
Colin MacKenzie

πŸ’» πŸ€”
Dimitri Kopriwa
Dimitri Kopriwa

πŸ’»
Faraz Patankar
Faraz Patankar

πŸ›
Joel
Joel

πŸ€” πŸ’»
Josh Goldberg
Josh Goldberg

πŸ”§ πŸ’» 🚧 πŸš‡ πŸ€” πŸ› ⚠️ πŸ–‹ πŸ“– πŸ“†
SUZUKI Sosuke
SUZUKI Sosuke

πŸ’»

πŸ’ This package was templated with create-typescript-app using the Bingo framework.

changelog

Changelog

0.6.0 (2025-12-04)

Features

  • bump Node.js minimum to 20.19.0; remove CJS files (#1049) a1209e4, closes #1041 #1046

0.5.0 (2024-08-14)

Features

0.4.4 (2024-07-17)

Bug Fixes

  • correct setup.mjs files path in package.json (9c42001)

0.4.3 (2024-07-15)

Bug Fixes

0.4.2 (2024-07-13)

Bug Fixes

0.4.1 (2024-07-11)

Bug Fixes

0.4.0 (2024-07-10)

Bug Fixes

  • don't crash when printing recursive objects (#208) (a4cdf34)
  • jest: Allow being used inside Jest beforeAll (442c07c), closes #72
  • refresh tooling with create-typescript-app@1.62 (#574) (97346e1), closes #531
  • use public-facing Vitest spy mock functions (#205) (29a8d50)

Features