44 * @see [source](https://github.com/nodejs/node/blob/v22.x/lib/assert.js)
55 */
66declare module "assert" {
7+ import strict = require( "assert/strict" ) ;
78 /**
8- * An alias of {@link ok}.
9+ * An alias of {@link assert. ok}.
910 * @since v0.5.9
1011 * @param value The input that is checked for being truthy.
1112 */
1213 function assert ( value : unknown , message ?: string | Error ) : asserts value ;
14+ const kOptions : unique symbol ;
1315 namespace assert {
1416 type AssertMethodNames =
1517 | "deepEqual"
@@ -30,10 +32,100 @@ declare module "assert" {
3032 | "rejects"
3133 | "strictEqual"
3234 | "throws" ;
35+ interface AssertOptions {
36+ /**
37+ * If set to `'full'`, shows the full diff in assertion errors.
38+ * @default 'simple'
39+ */
40+ diff ?: "simple" | "full" | undefined ;
41+ /**
42+ * If set to `true`, non-strict methods behave like their
43+ * corresponding strict methods.
44+ * @default true
45+ */
46+ strict ?: boolean | undefined ;
47+ }
48+ interface Assert extends Pick < typeof assert , AssertMethodNames > {
49+ readonly [ kOptions ] : AssertOptions & { strict : false } ;
50+ }
51+ interface AssertStrict extends Pick < typeof strict , AssertMethodNames > {
52+ readonly [ kOptions ] : AssertOptions & { strict : true } ;
53+ }
54+ /**
55+ * The `Assert` class allows creating independent assertion instances with custom options.
56+ * @since v22.19.0
57+ */
58+ var Assert : {
59+ /**
60+ * Creates a new assertion instance. The `diff` option controls the verbosity of diffs in assertion error messages.
61+ *
62+ * ```js
63+ * const { Assert } = require('node:assert');
64+ * const assertInstance = new Assert({ diff: 'full' });
65+ * assertInstance.deepStrictEqual({ a: 1 }, { a: 2 });
66+ * // Shows a full diff in the error message.
67+ * ```
68+ *
69+ * **Important**: When destructuring assertion methods from an `Assert` instance,
70+ * the methods lose their connection to the instance's configuration options (such as `diff` and `strict` settings).
71+ * The destructured methods will fall back to default behavior instead.
72+ *
73+ * ```js
74+ * const myAssert = new Assert({ diff: 'full' });
75+ *
76+ * // This works as expected - uses 'full' diff
77+ * myAssert.strictEqual({ a: 1 }, { b: { c: 1 } });
78+ *
79+ * // This loses the 'full' diff setting - falls back to default 'simple' diff
80+ * const { strictEqual } = myAssert;
81+ * strictEqual({ a: 1 }, { b: { c: 1 } });
82+ * ```
83+ *
84+ * When destructured, methods lose access to the instance's `this` context and revert to default assertion behavior
85+ * (diff: 'simple', non-strict mode).
86+ * To maintain custom options when using destructured methods, avoid
87+ * destructuring and call methods directly on the instance.
88+ * @since v22.19.0
89+ */
90+ new (
91+ options ?: AssertOptions & { strict ?: true } ,
92+ ) : AssertStrict ;
93+ new (
94+ options : AssertOptions ,
95+ ) : Assert ;
96+ } ;
97+ interface AssertionErrorOptions {
98+ /**
99+ * If provided, the error message is set to this value.
100+ */
101+ message ?: string | undefined ;
102+ /**
103+ * The `actual` property on the error instance.
104+ */
105+ actual ?: unknown ;
106+ /**
107+ * The `expected` property on the error instance.
108+ */
109+ expected ?: unknown ;
110+ /**
111+ * The `operator` property on the error instance.
112+ */
113+ operator ?: string | undefined ;
114+ /**
115+ * If provided, the generated stack trace omits frames before this function.
116+ */
117+ stackStartFn ?: Function | undefined ;
118+ /**
119+ * If set to `'full'`, shows the full diff in assertion errors.
120+ * @default 'simple'
121+ */
122+ diff ?: "simple" | "full" | undefined ;
123+ }
33124 /**
34125 * Indicates the failure of an assertion. All errors thrown by the `node:assert` module will be instances of the `AssertionError` class.
35126 */
36127 class AssertionError extends Error {
128+ constructor ( options : AssertionErrorOptions ) ;
37129 /**
38130 * Set to the `actual` argument for methods such as {@link assert.strictEqual()}.
39131 */
@@ -42,10 +134,6 @@ declare module "assert" {
42134 * Set to the `expected` argument for methods such as {@link assert.strictEqual()}.
43135 */
44136 expected : unknown ;
45- /**
46- * Set to the passed in operator value.
47- */
48- operator : string ;
49137 /**
50138 * Indicates if the message was auto-generated (`true`) or not.
51139 */
@@ -54,19 +142,10 @@ declare module "assert" {
54142 * Value is always `ERR_ASSERTION` to show that the error is an assertion error.
55143 */
56144 code : "ERR_ASSERTION" ;
57- constructor ( options ?: {
58- /** If provided, the error message is set to this value. */
59- message ?: string | undefined ;
60- /** The `actual` property on the error instance. */
61- actual ?: unknown | undefined ;
62- /** The `expected` property on the error instance. */
63- expected ?: unknown | undefined ;
64- /** The `operator` property on the error instance. */
65- operator ?: string | undefined ;
66- /** If provided, the generated stack trace omits frames before this function. */
67- // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
68- stackStartFn ?: Function | undefined ;
69- } ) ;
145+ /**
146+ * Set to the passed in operator value.
147+ */
148+ operator : string ;
70149 }
71150 /**
72151 * This feature is deprecated and will be removed in a future version.
@@ -987,83 +1066,9 @@ declare module "assert" {
9871066 * @since v22.13.0
9881067 */
9891068 function partialDeepStrictEqual ( actual : unknown , expected : unknown , message ?: string | Error ) : void ;
990- /**
991- * In strict assertion mode, non-strict methods behave like their corresponding strict methods. For example,
992- * {@link deepEqual} will behave like {@link deepStrictEqual}.
993- *
994- * In strict assertion mode, error messages for objects display a diff. In legacy assertion mode, error
995- * messages for objects display the objects, often truncated.
996- *
997- * To use strict assertion mode:
998- *
999- * ```js
1000- * import { strict as assert } from 'node:assert';
1001- * import assert from 'node:assert/strict';
1002- * ```
1003- *
1004- * Example error diff:
1005- *
1006- * ```js
1007- * import { strict as assert } from 'node:assert';
1008- *
1009- * assert.deepEqual([[[1, 2, 3]], 4, 5], [[[1, 2, '3']], 4, 5]);
1010- * // AssertionError: Expected inputs to be strictly deep-equal:
1011- * // + actual - expected ... Lines skipped
1012- * //
1013- * // [
1014- * // [
1015- * // ...
1016- * // 2,
1017- * // + 3
1018- * // - '3'
1019- * // ],
1020- * // ...
1021- * // 5
1022- * // ]
1023- * ```
1024- *
1025- * To deactivate the colors, use the `NO_COLOR` or `NODE_DISABLE_COLORS` environment variables. This will also
1026- * deactivate the colors in the REPL. For more on color support in terminal environments, read the tty
1027- * `getColorDepth()` documentation.
1028- *
1029- * @since v15.0.0, v13.9.0, v12.16.2, v9.9.0
1030- */
1031- namespace strict {
1032- type AssertionError = assert . AssertionError ;
1033- type AssertPredicate = assert . AssertPredicate ;
1034- type CallTrackerCall = assert . CallTrackerCall ;
1035- type CallTrackerReportInformation = assert . CallTrackerReportInformation ;
1036- }
1037- const strict :
1038- & Omit <
1039- typeof assert ,
1040- | "equal"
1041- | "notEqual"
1042- | "deepEqual"
1043- | "notDeepEqual"
1044- | "ok"
1045- | "strictEqual"
1046- | "deepStrictEqual"
1047- | "ifError"
1048- | "strict"
1049- | "AssertionError"
1050- >
1051- & {
1052- ( value : unknown , message ?: string | Error ) : asserts value ;
1053- equal : typeof strictEqual ;
1054- notEqual : typeof notStrictEqual ;
1055- deepEqual : typeof deepStrictEqual ;
1056- notDeepEqual : typeof notDeepStrictEqual ;
1057- // Mapped types and assertion functions are incompatible?
1058- // TS2775: Assertions require every name in the call target
1059- // to be declared with an explicit type annotation.
1060- ok : typeof ok ;
1061- strictEqual : typeof strictEqual ;
1062- deepStrictEqual : typeof deepStrictEqual ;
1063- ifError : typeof ifError ;
1064- strict : typeof strict ;
1065- AssertionError : typeof AssertionError ;
1066- } ;
1069+ }
1070+ namespace assert {
1071+ export { strict } ;
10671072 }
10681073 export = assert ;
10691074}
0 commit comments