From 68fe8bda4d39a0ceac86931ef56e48b18f5a798f Mon Sep 17 00:00:00 2001 From: anshikakalpana Date: Thu, 11 Jun 2026 01:59:24 +0530 Subject: [PATCH] assert: fix diff option ignored in Assert throws/rejects Signed-off-by: anshikakalpana --- lib/assert.js | 8 ++++---- test/parallel/test-assert-class.js | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/lib/assert.js b/lib/assert.js index 7c0edfc8f7f94f..07c38af75e10e6 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -725,7 +725,7 @@ function expectsNoError(stackStartFn, actual, error, message) { * @returns {void} */ Assert.prototype.throws = function throws(promiseFn, ...args) { - expectsError(throws, getActual(promiseFn), ...args); + expectsError.call(this, throws, getActual(promiseFn), ...args); }; /** @@ -735,7 +735,7 @@ Assert.prototype.throws = function throws(promiseFn, ...args) { * @returns {Promise} */ Assert.prototype.rejects = async function rejects(promiseFn, ...args) { - expectsError(rejects, await waitForActual(promiseFn), ...args); + expectsError.call(this, rejects, await waitForActual(promiseFn), ...args); }; /** @@ -745,7 +745,7 @@ Assert.prototype.rejects = async function rejects(promiseFn, ...args) { * @returns {void} */ Assert.prototype.doesNotThrow = function doesNotThrow(fn, ...args) { - expectsNoError(doesNotThrow, getActual(fn), ...args); + expectsNoError.call(this, doesNotThrow, getActual(fn), ...args); }; /** @@ -755,7 +755,7 @@ Assert.prototype.doesNotThrow = function doesNotThrow(fn, ...args) { * @returns {Promise} */ Assert.prototype.doesNotReject = async function doesNotReject(fn, ...args) { - expectsNoError(doesNotReject, await waitForActual(fn), ...args); + expectsNoError.call(this, doesNotReject, await waitForActual(fn), ...args); }; /** diff --git a/test/parallel/test-assert-class.js b/test/parallel/test-assert-class.js index cccc7a2f36fff9..e167827bb8a034 100644 --- a/test/parallel/test-assert-class.js +++ b/test/parallel/test-assert-class.js @@ -638,4 +638,30 @@ test('Assert class non strict with simple diff', () => { { code: 'ERR_ASSERTION' } ); }); + + test('Assert class diff option is passed through throws/rejects', async () => { + const assertInstance = new Assert({ diff: 'full' }); + + // throws + { + let err; + try { + assertInstance.throws(() => {}, /Missing expected exception/); + } catch (e) { + err = e; + } + assert.strictEqual(err?.diff, 'full'); + } + + // rejects + { + let err; + try { + await assertInstance.rejects(async () => {}, /Missing expected rejection/); + } catch (e) { + err = e; + } + assert.strictEqual(err?.diff, 'full'); + } + }); }