Skip to content

Commit 89a7751

Browse files
assert: fix diff option ignored in Assert throws/rejects
Signed-off-by: anshikakalpana <anshikajain196872@gmail.com>
1 parent 1b04f16 commit 89a7751

2 files changed

Lines changed: 52 additions & 4 deletions

File tree

lib/assert.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ function expectsNoError(stackStartFn, actual, error, message) {
725725
* @returns {void}
726726
*/
727727
Assert.prototype.throws = function throws(promiseFn, ...args) {
728-
expectsError(throws, getActual(promiseFn), ...args);
728+
expectsError.call(this, throws, getActual(promiseFn), ...args);
729729
};
730730

731731
/**
@@ -735,7 +735,7 @@ Assert.prototype.throws = function throws(promiseFn, ...args) {
735735
* @returns {Promise<void>}
736736
*/
737737
Assert.prototype.rejects = async function rejects(promiseFn, ...args) {
738-
expectsError(rejects, await waitForActual(promiseFn), ...args);
738+
expectsError.call(this, rejects, await waitForActual(promiseFn), ...args);
739739
};
740740

741741
/**
@@ -745,7 +745,7 @@ Assert.prototype.rejects = async function rejects(promiseFn, ...args) {
745745
* @returns {void}
746746
*/
747747
Assert.prototype.doesNotThrow = function doesNotThrow(fn, ...args) {
748-
expectsNoError(doesNotThrow, getActual(fn), ...args);
748+
expectsNoError.call(this, doesNotThrow, getActual(fn), ...args);
749749
};
750750

751751
/**
@@ -755,7 +755,7 @@ Assert.prototype.doesNotThrow = function doesNotThrow(fn, ...args) {
755755
* @returns {Promise<void>}
756756
*/
757757
Assert.prototype.doesNotReject = async function doesNotReject(fn, ...args) {
758-
expectsNoError(doesNotReject, await waitForActual(fn), ...args);
758+
expectsNoError.call(this, doesNotReject, await waitForActual(fn), ...args);
759759
};
760760

761761
/**

test/parallel/test-assert-class.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,4 +638,52 @@ test('Assert class non strict with simple diff', () => {
638638
{ code: 'ERR_ASSERTION' }
639639
);
640640
});
641+
642+
test('Assert class diff option is passed through throws/rejects/doesNotThrow/doesNotReject', async () => {
643+
const assertInstance = new Assert({ diff: 'full' });
644+
645+
// throws
646+
{
647+
let err;
648+
try {
649+
assertInstance.throws(() => {});
650+
} catch (e) {
651+
err = e;
652+
}
653+
assert.strictEqual(err?.diff, 'full');
654+
}
655+
656+
// rejects
657+
{
658+
let err;
659+
try {
660+
await assertInstance.rejects(async () => {});
661+
} catch (e) {
662+
err = e;
663+
}
664+
assert.strictEqual(err?.diff, 'full');
665+
}
666+
667+
// doesNotThrow
668+
{
669+
let err;
670+
try {
671+
assertInstance.doesNotThrow(() => { throw new Error('test'); });
672+
} catch (e) {
673+
err = e;
674+
}
675+
assert.strictEqual(err?.diff, 'full');
676+
}
677+
678+
// doesNotReject
679+
{
680+
let err;
681+
try {
682+
await assertInstance.doesNotReject(async () => { throw new Error('test'); });
683+
} catch (e) {
684+
err = e;
685+
}
686+
assert.strictEqual(err?.diff, 'full');
687+
}
688+
});
641689
}

0 commit comments

Comments
 (0)