mirror of
https://github.com/nodejs/node.git
synced 2025-05-03 02:06:12 +00:00

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>
83 lines
1.9 KiB
JavaScript
83 lines
1.9 KiB
JavaScript
'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);
|
|
}
|