0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-21 21:19:50 +01:00
nodejs/test/parallel/test-assert-deep-with-error.js
Antoine du Hamel 6af5c4e2b4
assert: differentiate cases where cause is undefined or missing
PR-URL: https://github.com/nodejs/node/pull/55738
Reviewed-By: LiviaMedeiros <livia@cirno.name>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
2024-11-08 01:01:09 +00:00

83 lines
3.0 KiB
JavaScript

'use strict';
require('../common');
const assert = require('assert');
const { test } = require('node:test');
const defaultStartMessage = 'Expected values to be strictly deep-equal:\n' +
'+ actual - expected\n' +
'\n';
test('Handle error causes', () => {
assert.deepStrictEqual(new Error('a', { cause: new Error('x') }), new Error('a', { cause: new Error('x') }));
assert.deepStrictEqual(
new Error('a', { cause: new RangeError('x') }),
new Error('a', { cause: new RangeError('x') }),
);
assert.throws(() => {
assert.deepStrictEqual(new Error('a', { cause: new Error('x') }), new Error('a', { cause: new Error('y') }));
}, { message: defaultStartMessage + ' [Error: a] {\n' +
'+ [cause]: [Error: x]\n' +
'- [cause]: [Error: y]\n' +
' }\n' });
assert.throws(() => {
assert.deepStrictEqual(new Error('a', { cause: new Error('x') }), new Error('a', { cause: new TypeError('x') }));
}, { message: defaultStartMessage + ' [Error: a] {\n' +
'+ [cause]: [Error: x]\n' +
'- [cause]: [TypeError: x]\n' +
' }\n' });
assert.throws(() => {
assert.deepStrictEqual(new Error('a'), new Error('a', { cause: new Error('y') }));
}, { message: defaultStartMessage + '+ [Error: a]\n' +
'- [Error: a] {\n' +
'- [cause]: [Error: y]\n' +
'- }\n' });
assert.throws(() => {
assert.deepStrictEqual(new Error('a'), new Error('a', { cause: { prop: 'value' } }));
}, { message: defaultStartMessage + '+ [Error: a]\n' +
'- [Error: a] {\n' +
'- [cause]: {\n' +
'- prop: \'value\'\n' +
'- }\n' +
'- }\n' });
assert.notDeepStrictEqual(new Error('a', { cause: new Error('x') }), new Error('a', { cause: new Error('y') }));
assert.notDeepStrictEqual(
new Error('a', { cause: { prop: 'value' } }),
new Error('a', { cause: { prop: 'a different value' } })
);
});
test('Handle undefined causes', () => {
assert.deepStrictEqual(new Error('a', { cause: undefined }), new Error('a', { cause: undefined }));
assert.notDeepStrictEqual(new Error('a', { cause: 'undefined' }), new Error('a', { cause: undefined }));
assert.notDeepStrictEqual(new Error('a', { cause: undefined }), new Error('a'));
assert.notDeepStrictEqual(new Error('a'), new Error('a', { cause: undefined }));
assert.throws(() => {
assert.deepStrictEqual(new Error('a'), new Error('a', { cause: undefined }));
}, { message: defaultStartMessage +
'+ [Error: a]\n' +
'- [Error: a] {\n' +
'- [cause]: undefined\n' +
'- }\n' });
assert.throws(() => {
assert.deepStrictEqual(new Error('a', { cause: undefined }), new Error('a'));
}, { message: defaultStartMessage +
'+ [Error: a] {\n' +
'+ [cause]: undefined\n' +
'+ }\n' +
'- [Error: a]\n' });
assert.throws(() => {
assert.deepStrictEqual(new Error('a', { cause: undefined }), new Error('a', { cause: 'undefined' }));
}, { message: defaultStartMessage + ' [Error: a] {\n' +
'+ [cause]: undefined\n' +
'- [cause]: \'undefined\'\n' +
' }\n' });
});