fail-on-console 1.3.1 → 1.4.1
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 +16 -2
- package/index.d.ts +6 -0
- package/index.js +12 -2
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [1.4.1](https://github.com/benquarmby/fail-on-console/compare/v1.4.0...v1.4.1) (2026-08-15)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Bug Fixes
|
|
7
|
+
|
|
8
|
+
* add defense for missing hooks ([6e5323b](https://github.com/benquarmby/fail-on-console/commit/6e5323b433dce8cfa5ad9e9ffd911c648d8721c6))
|
|
9
|
+
|
|
10
|
+
## [1.4.0](https://github.com/benquarmby/fail-on-console/compare/v1.3.1...v1.4.0) (2026-08-08)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
### Features
|
|
14
|
+
|
|
15
|
+
* support custom format functions ([25908c3](https://github.com/benquarmby/fail-on-console/commit/25908c3e9219eb607a944d269188db0113376ddd))
|
|
16
|
+
|
|
3
17
|
## [1.3.1](https://github.com/benquarmby/fail-on-console/compare/v1.3.0...v1.3.1) (2026-08-01)
|
|
4
18
|
|
|
5
19
|
|
package/README.md
CHANGED
|
@@ -58,7 +58,7 @@ setupConsole({beforeEach, afterEach});
|
|
|
58
58
|
|
|
59
59
|
By default, `console.debug` is not monitored but `error`, `warn`, `info`, and `log` are. This can be customized by passing a `methods` array. Similarly, `process.stdout` and `process.stderr` are not monitored by default, but can be configured with a `streams` array:
|
|
60
60
|
|
|
61
|
-
```
|
|
61
|
+
```js
|
|
62
62
|
setupConsole({
|
|
63
63
|
beforeEach,
|
|
64
64
|
afterEach,
|
|
@@ -70,13 +70,27 @@ setupConsole({
|
|
|
70
70
|
});
|
|
71
71
|
```
|
|
72
72
|
|
|
73
|
+
### Using a Custom Formatter
|
|
74
|
+
|
|
75
|
+
By default, a lightweight and browser compatible equivalent of the Node.js [`util.format()`](https://nodejs.org/api/util.html#utilformatformat-args) function is used for `printf`-like formatting of console messages. It covers the most common use cases but has limitations. If a more accurate and complete implementation is required, pass it as the `format` option:
|
|
76
|
+
|
|
77
|
+
```js
|
|
78
|
+
import util from "node:util";
|
|
79
|
+
|
|
80
|
+
setupConsole({
|
|
81
|
+
beforeEach,
|
|
82
|
+
afterEach,
|
|
83
|
+
format: util.format
|
|
84
|
+
});
|
|
85
|
+
```
|
|
86
|
+
|
|
73
87
|
## Suppressing Expected Logs
|
|
74
88
|
|
|
75
89
|
If a specific test or third-party dependency intentionally logs to the console, `allowConsole` and `allowStream` can be used to allow the test to pass.
|
|
76
90
|
|
|
77
91
|
These functions can be invoked globally, inside a `describe` block, or inside a specific `test`/`it` block.
|
|
78
92
|
|
|
79
|
-
```
|
|
93
|
+
```js
|
|
80
94
|
import {allowConsole, allowStream} from "fail-on-console";
|
|
81
95
|
|
|
82
96
|
// Allow a substring.
|
package/index.d.ts
CHANGED
|
@@ -13,6 +13,10 @@ export interface LifecycleHookLike {
|
|
|
13
13
|
(fn: () => void): void;
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
+
export interface FormatLike {
|
|
17
|
+
(format?: any, ...args: any[]): string;
|
|
18
|
+
}
|
|
19
|
+
|
|
16
20
|
export interface TestApi {
|
|
17
21
|
beforeEach: LifecycleHookLike;
|
|
18
22
|
afterEach: LifecycleHookLike;
|
|
@@ -25,6 +29,7 @@ export interface TestApi {
|
|
|
25
29
|
export interface SetupOptions extends TestApi {
|
|
26
30
|
methods?: ConsoleMethod[];
|
|
27
31
|
streams?: ProcessStream[];
|
|
32
|
+
format?: FormatLike;
|
|
28
33
|
}
|
|
29
34
|
|
|
30
35
|
export interface AllowPredicate {
|
|
@@ -47,6 +52,7 @@ export function setup(options: SetupOptions): void;
|
|
|
47
52
|
* @param {Object} options
|
|
48
53
|
* @param {Function} options.beforeEach The beforeEach hook from the test framework.
|
|
49
54
|
* @param {Function} options.afterEach The afterEach hook from the test framework.
|
|
55
|
+
* @param {Function} [options.format] Optional function to format console messages. Uses a basic shim by default.
|
|
50
56
|
* @param {string[]} [options.methods=["error","warn","info","log"]] Console methods to monitor.
|
|
51
57
|
* @param {string[]} [options.streams=[]] Process streams to monitor. None by default.
|
|
52
58
|
* @example
|
package/index.js
CHANGED
|
@@ -53,7 +53,7 @@ function assertSupportedStreams(streams) {
|
|
|
53
53
|
* @param {...*} args The list of arguments passed to the console method.
|
|
54
54
|
* @returns {string} The formatted message string.
|
|
55
55
|
*/
|
|
56
|
-
function
|
|
56
|
+
function defaultFormat(...args) {
|
|
57
57
|
const [format, ...values] = args;
|
|
58
58
|
|
|
59
59
|
if (typeof format !== "string") {
|
|
@@ -104,11 +104,21 @@ function chunkToString(chunk, encoding = "utf8") {
|
|
|
104
104
|
return Buffer.from(chunk).toString(encoding);
|
|
105
105
|
}
|
|
106
106
|
|
|
107
|
-
function setupConsole({
|
|
107
|
+
function setupConsole({
|
|
108
|
+
beforeEach,
|
|
109
|
+
afterEach,
|
|
110
|
+
format = defaultFormat,
|
|
111
|
+
methods = defaultMethods,
|
|
112
|
+
streams = defaultStreams
|
|
113
|
+
}) {
|
|
108
114
|
if (testApi) {
|
|
109
115
|
throw new Error("fail-on-console: Call setupConsole() only once.");
|
|
110
116
|
}
|
|
111
117
|
|
|
118
|
+
if (typeof beforeEach !== "function" || typeof afterEach !== "function") {
|
|
119
|
+
throw new Error("fail-on-console: beforeEach and afterEach hooks must be provided.");
|
|
120
|
+
}
|
|
121
|
+
|
|
112
122
|
assertSupportedMethods(methods);
|
|
113
123
|
assertSupportedStreams(streams);
|
|
114
124
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fail-on-console",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.1",
|
|
4
4
|
"description": "Fail Vitest or Jest tests when unexpected console logs, warnings or errors occur.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"jest",
|
|
@@ -19,9 +19,9 @@
|
|
|
19
19
|
"./index.d.ts"
|
|
20
20
|
],
|
|
21
21
|
"devDependencies": {
|
|
22
|
-
"@commitlint/config-conventional": "^21.2.
|
|
22
|
+
"@commitlint/config-conventional": "^21.2.2",
|
|
23
23
|
"@vitest/coverage-v8": "4.1.10",
|
|
24
|
-
"commitlint": "^21.2.
|
|
24
|
+
"commitlint": "^21.2.2",
|
|
25
25
|
"jest": "^30.4.2",
|
|
26
26
|
"npm-run-all2": "^9.0.3",
|
|
27
27
|
"prettier": "^3.9.6",
|