Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/joshuakgoldberg/console-fail-test

Gently fails test runs if the console was used during them. ๐Ÿ“ข
https://github.com/joshuakgoldberg/console-fail-test

console info jasmine jest karma mocha test warn

Last synced: about 13 hours ago
JSON representation

Gently fails test runs if the console was used during them. ๐Ÿ“ข

Awesome Lists containing this project

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

```plaintext
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:

```js
// 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.



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:

```js
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:

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

## Development

Requires:

- [Node.js](https://nodejs.org) >14 (LTS)
- [Yarn](https://yarnpkg.com/en)

After [forking the repo from GitHub](https://help.github.com/articles/fork-a-repo):

```shell
git clone https://github.com//console-fail-test
cd console-fail-test
yarn
```

### Contribution Guidelines

We'd love to have you contribute!
Check the [issue tracker](https://github.com/JoshuaKGoldberg/console-fail-test/issues) for issues labeled [`accepting prs`](https://github.com/JoshuaKGoldberg/console-fail-test/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+label%3A%22accepting+prs%22) 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`](https://github.com/JoshuaKGoldberg/console-fail-test/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22+) label indicates good introductory issues.

Please note that this project is released with a [Contributor Covenant](https://www.contributor-covenant.org).
By participating in this project you agree to abide by its terms.
See [CODE_OF_CONDUCT.md](./CODE_OF_CONDUCT.md).

## 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 is based on [@JoshuaKGoldberg](https://github.com/JoshuaKGoldberg)'s [create-typescript-app](https://github.com/JoshuaKGoldberg/create-typescript-app).