mirror of
https://github.com/nodejs/node.git
synced 2025-08-15 21:58:48 +02:00
assert: wrap original error in ifError
It is hard to know where ifError is actually triggered due to the original error being thrown. This changes it by wrapping the original error in a AssertionError. This has the positive effect of also making clear that it is indeed a assertion function that triggered that error. The original stack can still be accessed by checking the `actual` property. PR-URL: https://github.com/nodejs/node/pull/18247 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
7a23fc0760
commit
72bb4445c6
9 changed files with 187 additions and 32 deletions
82
test/parallel/test-assert-if-error.js
Normal file
82
test/parallel/test-assert-if-error.js
Normal file
|
@ -0,0 +1,82 @@
|
|||
'use strict';
|
||||
|
||||
require('../common');
|
||||
const assert = require('assert').strict;
|
||||
/* eslint-disable no-restricted-properties */
|
||||
|
||||
// Test that assert.ifError has the correct stack trace of both stacks.
|
||||
|
||||
let err;
|
||||
// Create some random error frames.
|
||||
(function a() {
|
||||
(function b() {
|
||||
(function c() {
|
||||
err = new Error('test error');
|
||||
})();
|
||||
})();
|
||||
})();
|
||||
|
||||
const msg = err.message;
|
||||
const stack = err.stack;
|
||||
|
||||
(function x() {
|
||||
(function y() {
|
||||
(function z() {
|
||||
let threw = false;
|
||||
try {
|
||||
assert.ifError(err);
|
||||
} catch (e) {
|
||||
assert.equal(e.message, 'ifError got unwanted exception: test error');
|
||||
assert.equal(err.message, msg);
|
||||
assert.equal(e.actual, err);
|
||||
assert.equal(e.actual.stack, stack);
|
||||
assert.equal(e.expected, null);
|
||||
assert.equal(e.operator, 'ifError');
|
||||
threw = true;
|
||||
}
|
||||
assert(threw);
|
||||
})();
|
||||
})();
|
||||
})();
|
||||
|
||||
assert.throws(
|
||||
() => assert.ifError(new TypeError()),
|
||||
{
|
||||
message: 'ifError got unwanted exception: TypeError'
|
||||
}
|
||||
);
|
||||
|
||||
assert.throws(
|
||||
() => assert.ifError({ stack: false }),
|
||||
{
|
||||
message: 'ifError got unwanted exception: { stack: false }'
|
||||
}
|
||||
);
|
||||
|
||||
assert.throws(
|
||||
() => assert.ifError({ constructor: null, message: '' }),
|
||||
{
|
||||
message: 'ifError got unwanted exception: '
|
||||
}
|
||||
);
|
||||
|
||||
assert.doesNotThrow(() => { assert.ifError(null); });
|
||||
assert.doesNotThrow(() => { assert.ifError(); });
|
||||
assert.doesNotThrow(() => { assert.ifError(undefined); });
|
||||
assert.doesNotThrow(() => { assert.ifError(false); });
|
||||
|
||||
// https://github.com/nodejs/node-v0.x-archive/issues/2893
|
||||
{
|
||||
let threw = false;
|
||||
try {
|
||||
// eslint-disable-next-line no-restricted-syntax
|
||||
assert.throws(() => {
|
||||
assert.ifError(null);
|
||||
});
|
||||
} catch (e) {
|
||||
threw = true;
|
||||
assert.strictEqual(e.message, 'Missing expected exception.');
|
||||
assert(!e.stack.includes('throws'), e.stack);
|
||||
}
|
||||
assert(threw);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue