fail-on-console 1.1.0 â 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/README.md +28 -12
- package/index.js +32 -1
- package/package.json +8 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [1.1.2](https://github.com/benquarmby/fail-on-console/compare/v1.1.1...v1.1.2) (2026-07-22)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Bug Fixes
|
|
7
|
+
|
|
8
|
+
* guard against invalid and unsupported console methods ([504fa30](https://github.com/benquarmby/fail-on-console/commit/504fa301e245d0965afdfaf3c31d2d3b9a0cb10c))
|
|
9
|
+
|
|
10
|
+
## [1.1.1](https://github.com/benquarmby/fail-on-console/compare/v1.1.0...v1.1.1) (2026-07-08)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
### Bug Fixes
|
|
14
|
+
|
|
15
|
+
* add missing keywords and refine description ([3d8688a](https://github.com/benquarmby/fail-on-console/commit/3d8688aa33ce9a717adb7e01b0f8431cbd86ea0e))
|
|
16
|
+
|
|
3
17
|
## [1.1.0](https://github.com/benquarmby/fail-on-console/compare/v1.0.1...v1.1.0) (2026-07-06)
|
|
4
18
|
|
|
5
19
|
|
package/README.md
CHANGED
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
# Fail on Console
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Fail Vitest or Jest tests when unexpected console logs, warnings or errors occur.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
[](https://www.npmjs.com/package/fail-on-console)
|
|
6
|
+
[](https://github.com/benquarmby/fail-on-console/blob/main/LICENSE)
|
|
7
|
+
|
|
8
|
+
The `fail-on-console` utility fails test suites whenever unexpected `console` logs, warnings, or errors are triggered, keeping test results clear and easy to read.
|
|
6
9
|
|
|
7
10
|
## Features
|
|
8
11
|
|
|
9
|
-
-
|
|
10
|
-
-
|
|
11
|
-
-
|
|
12
|
-
-
|
|
13
|
-
- **Flexible Allowlist**: Suppresses known or expected console outputs globally, per suite, or per individual test. Exceptions can be matched against strings, regular expressions, or custom predicate functions.
|
|
12
|
+
- **⥠Vitest and Jest Native**: Seamless integration with Vitest (including Browser Mode) and Jest using standard lifecycle hooks.
|
|
13
|
+
- **đĒļ Zero Dependencies**: Pure, lightweight JavaScript with a tiny footprint.
|
|
14
|
+
- **đ¯ Configurable Targets**: Choose exactly which console methods to monitor (`log`, `warn`, `error`, `info`, `debug`).
|
|
15
|
+
- **đ Flexible Allowlist**: Easily suppress expected console noise globally, per suite, or per test using strings, regular expressions, or custom predicates.
|
|
14
16
|
|
|
15
17
|
## Installation
|
|
16
18
|
|
|
@@ -27,11 +29,12 @@ yarn add --dev fail-on-console
|
|
|
27
29
|
|
|
28
30
|
## Setup
|
|
29
31
|
|
|
30
|
-
`setupConsole` should be called once in a global setup file, accepting the lifecycle hooks and `expect` utility from the testing framework.
|
|
31
|
-
|
|
32
32
|
### With Vitest
|
|
33
33
|
|
|
34
|
-
|
|
34
|
+
Initialize `setupConsole` inside a configured [`setupFiles`](https://vitest.dev/config/setupfiles) module (e.g., `vitest.setup.js`).
|
|
35
|
+
|
|
36
|
+
```js
|
|
37
|
+
// vitest.setup.js
|
|
35
38
|
import {beforeEach, afterEach, expect} from "vitest";
|
|
36
39
|
import {setupConsole} from "fail-on-console";
|
|
37
40
|
|
|
@@ -40,7 +43,10 @@ setupConsole({beforeEach, afterEach, expect});
|
|
|
40
43
|
|
|
41
44
|
### With Jest
|
|
42
45
|
|
|
43
|
-
|
|
46
|
+
Initialize `setupConsole` inside a configured [`setupFilesAfterEnv`](https://jestjs.io/docs/configuration#setupfilesafterenv-array) module (e.g., `jest.setup.js`).
|
|
47
|
+
|
|
48
|
+
```js
|
|
49
|
+
// jest.setup.js
|
|
44
50
|
import {beforeEach, afterEach, expect} from "@jest/globals";
|
|
45
51
|
import {setupConsole} from "fail-on-console";
|
|
46
52
|
|
|
@@ -56,7 +62,7 @@ setupConsole({
|
|
|
56
62
|
beforeEach,
|
|
57
63
|
afterEach,
|
|
58
64
|
expect,
|
|
59
|
-
// Fail on console.error, console.warn and console.debug.
|
|
65
|
+
// Fail on console.error, console.warn, and console.debug.
|
|
60
66
|
methods: ["error", "warn", "debug"]
|
|
61
67
|
});
|
|
62
68
|
```
|
|
@@ -106,8 +112,18 @@ Registers a temporary or global allowlist rule for a monitored console method.
|
|
|
106
112
|
|
|
107
113
|
## Limitations
|
|
108
114
|
|
|
115
|
+
### Concurrency
|
|
116
|
+
|
|
109
117
|
`fail-on-console` is not compatible with concurrent asynchronous tests (e.g., `test.concurrent`). Because concurrent tests execute simultaneously within the same environment context, console logs cannot be isolated reliably per individual test. For suites requiring specific log suppression, tests must be run sequentially.
|
|
110
118
|
|
|
119
|
+
### Assert
|
|
120
|
+
|
|
121
|
+
Monitoring `console.assert` is currently unsupported. It has a distinct signature and unique assertion mechanics compared to standard logging methods.
|
|
122
|
+
|
|
123
|
+
### Mocha
|
|
124
|
+
|
|
125
|
+
Mocha is unsupported due to API incompatibilities related to test context. There are no clean or reliable workarounds for integration.
|
|
126
|
+
|
|
111
127
|
## Credits & Prior Art
|
|
112
128
|
|
|
113
129
|
This package is inspired by and builds on the excellent foundation laid by:
|
package/index.js
CHANGED
|
@@ -1,9 +1,36 @@
|
|
|
1
1
|
let testApi;
|
|
2
|
-
const
|
|
2
|
+
const supportedMethods = ["error", "warn", "info", "log", "debug"];
|
|
3
|
+
// Everything but console.debug is monitored by default. Debug logging in tests
|
|
4
|
+
// is usually intentional.
|
|
5
|
+
const defaultMethods = supportedMethods.slice(0, -1);
|
|
3
6
|
const allowed = new Map();
|
|
4
7
|
// %s string, %d/%i integer, %o object, %f float
|
|
5
8
|
const printfPattern = /%[sdiof]/g;
|
|
6
9
|
|
|
10
|
+
function quoteString(value) {
|
|
11
|
+
return `"${value}"`;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function assertSupportedMethods(methods) {
|
|
15
|
+
if (!Array.isArray(methods)) {
|
|
16
|
+
throw new Error("fail-on-console: Expected an array of console methods.");
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const unsupported = methods.filter((method) => !supportedMethods.includes(method));
|
|
20
|
+
|
|
21
|
+
if (!unsupported.length) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const method = unsupported.length === 1 ? "method" : "methods";
|
|
26
|
+
const invalidList = unsupported.map(quoteString).join(", ");
|
|
27
|
+
const validList = supportedMethods.map(quoteString).join(", ");
|
|
28
|
+
|
|
29
|
+
throw new Error(
|
|
30
|
+
`fail-on-console: Unsupported console ${method} provided: ${invalidList}. Supported methods are: ${validList}.`
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
|
|
7
34
|
/**
|
|
8
35
|
* Basic implementation of node:util/format for console message formatting.
|
|
9
36
|
* Covers only the most common uses. Does not handle all specifiers (such as
|
|
@@ -51,6 +78,8 @@ function setupConsole({beforeEach, afterEach, expect, methods = defaultMethods})
|
|
|
51
78
|
throw new Error("fail-on-console: Call setupConsole() only once.");
|
|
52
79
|
}
|
|
53
80
|
|
|
81
|
+
assertSupportedMethods(methods);
|
|
82
|
+
|
|
54
83
|
testApi = {beforeEach, afterEach, expect};
|
|
55
84
|
|
|
56
85
|
beforeEach(() => allowed.clear());
|
|
@@ -98,6 +127,8 @@ function allowConsole(method, rules) {
|
|
|
98
127
|
throw new Error("fail-on-console: Call setupConsole() before using allowConsole().");
|
|
99
128
|
}
|
|
100
129
|
|
|
130
|
+
assertSupportedMethods([method]);
|
|
131
|
+
|
|
101
132
|
const normalized = Array.isArray(rules) ? rules : [rules];
|
|
102
133
|
const isInsideTest = !!testApi.expect.getState().currentTestName;
|
|
103
134
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fail-on-console",
|
|
3
|
-
"version": "1.1.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.1.2",
|
|
4
|
+
"description": "Fail Vitest or Jest tests when unexpected console logs, warnings or errors occur.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"jest",
|
|
7
|
+
"vitest",
|
|
8
|
+
"console",
|
|
9
|
+
"testing"
|
|
10
|
+
],
|
|
5
11
|
"repository": {
|
|
6
12
|
"type": "git",
|
|
7
13
|
"url": "https://github.com/benquarmby/fail-on-console"
|