fail-on-console 1.3.1 → 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,12 @@
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
+
3
10
  ## [1.3.1](https://github.com/benquarmby/fail-on-console/compare/v1.3.0...v1.3.1) (2026-08-01)
4
11
 
5
12
 
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
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.1",
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",