fail-on-console 1.3.0 → 1.4.0

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 CHANGED
@@ -1,5 +1,19 @@
1
1
  # Changelog
2
2
 
3
+ ## [1.4.0](https://github.com/benquarmby/fail-on-console/compare/v1.3.1...v1.4.0) (2026-08-08)
4
+
5
+
6
+ ### Features
7
+
8
+ * support custom format functions ([25908c3](https://github.com/benquarmby/fail-on-console/commit/25908c3e9219eb607a944d269188db0113376ddd))
9
+
10
+ ## [1.3.1](https://github.com/benquarmby/fail-on-console/compare/v1.3.0...v1.3.1) (2026-08-01)
11
+
12
+
13
+ ### Bug Fixes
14
+
15
+ * use correct rule type for allowStream declaration ([23fe420](https://github.com/benquarmby/fail-on-console/commit/23fe42050a7a46959ea6f2b68204a41e424bcacc))
16
+
3
17
  ## [1.3.0](https://github.com/benquarmby/fail-on-console/compare/v1.2.0...v1.3.0) (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
- ```ts
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
- ```javascript
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
@@ -105,4 +111,4 @@ export function allowConsole(method: ConsoleMethod, rules: AllowRule | AllowRule
105
111
  * // Mixed array - allow standard errors using multiple rules at once
106
112
  * allowStream("stderr", ["known warning", /deprecated/, (m) => m.includes("third-party")]);
107
113
  */
108
- export function allowStream(stream: ProcessStream, rules: Rule | Rule[]): void;
114
+ export function allowStream(stream: ProcessStream, rules: AllowRule | AllowRule[]): void;
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 format(...args) {
56
+ function defaultFormat(...args) {
57
57
  const [format, ...values] = args;
58
58
 
59
59
  if (typeof format !== "string") {
@@ -104,7 +104,13 @@ function chunkToString(chunk, encoding = "utf8") {
104
104
  return Buffer.from(chunk).toString(encoding);
105
105
  }
106
106
 
107
- function setupConsole({beforeEach, afterEach, methods = defaultMethods, streams = defaultStreams}) {
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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fail-on-console",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "description": "Fail Vitest or Jest tests when unexpected console logs, warnings or errors occur.",
5
5
  "keywords": [
6
6
  "jest",
@@ -26,6 +26,7 @@
26
26
  "npm-run-all2": "^9.0.3",
27
27
  "prettier": "^3.9.6",
28
28
  "prettier-plugin-packagejson": "^3.0.2",
29
+ "typescript": "^7.0.2",
29
30
  "vitest": "^4.1.10"
30
31
  },
31
32
  "scripts": {
@@ -34,6 +35,7 @@
34
35
  "format:check": "prettier --check .",
35
36
  "test:jest": "jest",
36
37
  "test:vitest": "vitest run --coverage",
37
- "verify": "run-s commit:check format:check test:vitest test:jest"
38
+ "type:check": "tsc",
39
+ "verify": "run-s commit:check format:check type:check test:vitest test:jest"
38
40
  }
39
41
  }