0.2.3 • Published 12 months ago

console-fail-test v0.2.3

Weekly downloads
1,930
License
MIT
Repository
github
Last release
12 months ago

console-fail-test

GitHub CI Code Style: Prettier TypeScript: Strict NPM version Downloads

Gently fails test runs if the console was used during them.

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
setupFilesAfterEnv: ["console-fail-test/setup.js"],

Test Frameworks

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

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

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.

0.2.1

1 year ago

0.2.0

1 year ago

0.1.9

1 year ago

0.2.3

12 months ago

0.2.2

12 months ago

0.1.8

1 year ago

0.1.7

2 years ago

0.1.6

2 years ago

0.1.5

4 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.1-beta1

5 years ago

0.1.1-beta0

5 years ago

0.1.0

5 years ago