Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More β†’
Socket
Sign inDemoInstall
Socket

console-fail-test

Package Overview
Dependencies
Maintainers
0
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

console-fail-test

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

  • 0.5.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

console-fail-test

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

All Contributors: 5 πŸ‘ͺ 🀝 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
Whoopsies!

 ❯ 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: "Whoopsies!"

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.mjs"],
};

If your package only supports CommonJS, you can use console-fail-test/setup.cjs.

Test Frameworks

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

FrameworkSupport?API RequestDocumentation
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",
});
LibrarySupport?API RequestSpyDocumentation
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

Requires:

After forking the repo from GitHub:

git clone https://github.com/<your-name-here>/console-fail-test
cd console-fail-test
yarn

Contribution Guidelines

We'd love to have you contribute! Check the issue tracker for issues labeled accepting prs to find bug fixes and feature requests the community can work on. If this is your first time working with this code, the good first issue label indicates good introductory issues.

Please note that this project is released with a Contributor Covenant. By participating in this project you agree to abide by its terms. See CODE_OF_CONDUCT.md.

Contributors

Colin MacKenzie
Colin MacKenzie

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

πŸ’»
Joel
Joel

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

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

πŸ’»

πŸ’™ This package is based on @JoshuaKGoldberg's create-typescript-app.

FAQs

Package last updated on 14 Aug 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚑️ by Socket Inc