Skip to content

Commit 3c88b76

Browse files
miguelmarcondesfRafaelGSS
authored andcommitted
lib: restructure assert to become a class
PR-URL: #58253 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent eecd327 commit 3c88b76

15 files changed

+823
-49
lines changed

doc/api/assert.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ added: v0.1.21
149149
* `operator` {string} The `operator` property on the error instance.
150150
* `stackStartFn` {Function} If provided, the generated stack trace omits
151151
frames before this function.
152+
* `diff` {string} If set to `'full'`, shows the full diff in assertion errors. Defaults to `'simple'`.
153+
Accepted values: `'simple'`, `'full'`.
152154

153155
A subclass of {Error} that indicates the failure of an assertion.
154156

@@ -215,6 +217,51 @@ try {
215217
}
216218
```
217219

220+
## Class: `assert.Assert`
221+
222+
<!-- YAML
223+
added: REPLACEME
224+
-->
225+
226+
The `Assert` class allows creating independent assertion instances with custom options.
227+
228+
### `new assert.Assert([options])`
229+
230+
* `options` {Object}
231+
* `diff` {string} If set to `'full'`, shows the full diff in assertion errors. Defaults to `'simple'`.
232+
Accepted values: `'simple'`, `'full'`.
233+
* `strict` {boolean} If set to `true`, non-strict methods behave like their
234+
corresponding strict methods. Defaults to `true`.
235+
236+
Creates a new assertion instance. The `diff` option controls the verbosity of diffs in assertion error messages.
237+
238+
```js
239+
const { Assert } = require('node:assert');
240+
const assertInstance = new Assert({ diff: 'full' });
241+
assertInstance.deepStrictEqual({ a: 1 }, { a: 2 });
242+
// Shows a full diff in the error message.
243+
```
244+
245+
**Important**: When destructuring assertion methods from an `Assert` instance,
246+
the methods lose their connection to the instance's configuration options (such as `diff` and `strict` settings).
247+
The destructured methods will fall back to default behavior instead.
248+
249+
```js
250+
const myAssert = new Assert({ diff: 'full' });
251+
252+
// This works as expected - uses 'full' diff
253+
myAssert.strictEqual({ a: 1 }, { b: { c: 1 } });
254+
255+
// This loses the 'full' diff setting - falls back to default 'simple' diff
256+
const { strictEqual } = myAssert;
257+
strictEqual({ a: 1 }, { b: { c: 1 } });
258+
```
259+
260+
When destructured, methods lose access to the instance's `this` context and revert to default assertion behavior
261+
(diff: 'simple', non-strict mode).
262+
To maintain custom options when using destructured methods, avoid
263+
destructuring and call methods directly on the instance.
264+
218265
## Class: `assert.CallTracker`
219266

220267
<!-- YAML

0 commit comments

Comments
 (0)