From df671f706662927ce861b5285b34e7bab2fae3e1 Mon Sep 17 00:00:00 2001 From: Miguel Marcondes Date: Fri, 25 Apr 2025 10:13:50 -0300 Subject: [PATCH 01/12] lib: restructure assert to become a class --- doc/api/assert.md | 27 + lib/assert.js | 135 ++++- lib/internal/assert/assertion_error.js | 19 +- test/fixtures/errors/error_exit.snapshot | 3 +- .../errors/if-error-has-good-stack.snapshot | 3 +- .../output/assertion-color-tty.snapshot | 3 +- .../test-runner/output/dot_reporter.snapshot | 6 +- .../output/junit_reporter.snapshot | 6 +- .../test-runner/output/spec_reporter.snapshot | 6 +- .../output/spec_reporter_cli.snapshot | 6 +- test/message/assert_throws_stack.out | 6 +- test/parallel/test-assert-class.js | 478 ++++++++++++++++++ test/parallel/test-fs-promises.js | 2 +- test/parallel/test-runner-assert.js | 2 + 14 files changed, 653 insertions(+), 49 deletions(-) create mode 100644 test/parallel/test-assert-class.js diff --git a/doc/api/assert.md b/doc/api/assert.md index cbb8446b5d5444..45f889712e6513 100644 --- a/doc/api/assert.md +++ b/doc/api/assert.md @@ -149,6 +149,8 @@ added: v0.1.21 * `operator` {string} The `operator` property on the error instance. * `stackStartFn` {Function} If provided, the generated stack trace omits frames before this function. + * `diff` {string} If set to `'full'`, shows the full diff in assertion errors. Defaults to `'simple'`. + Accepted values: `'simple'`, `'full'`. A subclass of {Error} that indicates the failure of an assertion. @@ -215,6 +217,31 @@ try { } ``` +## Class: assert.Assert + + + +The `Assert` class allows creating independent assertion instances with custom options. + +### `new assert.Assert([options])` + +* `options` {Object} + * `diff` {string} If set to `'full'`, shows the full diff in assertion errors. Defaults to `'simple'`. + Accepted values: `'simple'`, `'full'`. + * `strict` {boolean} If set to `true`, non-strict methods behave like their + corresponding strict methods. Defaults to `true`. + +Creates a new assertion instance. The `diff` option controls the verbosity of diffs in assertion error messages. + +```js +const { Assert } = require('node:assert'); +const assertInstance = new Assert({ diff: 'full' }); +assertInstance.deepStrictEqual({ a: 1 }, { a: 2 }); +// Shows a full diff in the error message. +``` + ## `assert(value[, message])`