Gently fails test runs if the console was used during them. π’
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!"
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 wassetup.cjsorsetup.mjs.
See docs/examples/ for runnable example projects per framework.
For test frameworks whose test.extend() creates fixtures, such as Playwright and Vitest, extendTest can instead add console-fail-test as an automatic fixture:
// fixtures.js
import { extendTest } from "console-fail-test";
import { test as base } from "vitest";
export const test = extendTest(base);Tests that use the extended test are checked for console calls from the test, its hooks, and its fixtures' setup and teardown.
extendTest takes the same options as cft as its second parameter, except for testFramework.
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
|
| node:test | β οΈ β¨ |
"node:test"
|
NodeTest.md
|
| Playwright | β οΈ |
extendTest(test)
|
Playwright.md
|
| QUnit | β οΈ β¨ |
"qunit"
|
QUnit.md
|
| tape | β οΈ |
require("tape")
|
Tape.md
|
| Vitest | β οΈ β¨ |
"vitest"
|
Vitest.md
|
| Cypress | βοΈ |
/issues/199
|
|
| TestCafe | βοΈ |
/issues/15
|
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
|
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
},
});To allow only specific messages, pass an allow array of strings or regular expressions.
A call is allowed if its arguments, joined by spaces, contain any string or match any regular expression:
require("console-fail-test").cft({
console: {
error: {
allow: ["Failed to fetch", /^Deprecated: /],
},
},
});See .github/CONTRIBUTING.md, then .github/DEVELOPMENT.md.
Thanks! π’
Colin MacKenzie π» π€ |
Dimitri Kopriwa π» |
Faraz Patankar π |
Joel π€ π» |
Josh Goldberg π§ π» π§ π π€ π |
SUZUKI Sosuke π» |
π This package was templated with
create-typescript-appusing the Bingo framework.