Files
node/test/parallel/test-util-decorate-error-stack.js
André Føyn Berge 0a88e1bd60 test: replace fixturesDir with fixtures module
PR-URL: https://github.com/nodejs/node/pull/15947
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
2017-11-28 13:10:40 +09:00

58 lines
1.6 KiB
JavaScript

// Flags: --expose_internals
'use strict';
require('../common');
const fixtures = require('../common/fixtures');
const assert = require('assert');
const internalUtil = require('internal/util');
const spawnSync = require('child_process').spawnSync;
assert.doesNotThrow(function() {
internalUtil.decorateErrorStack();
internalUtil.decorateErrorStack(null);
internalUtil.decorateErrorStack(1);
internalUtil.decorateErrorStack(true);
});
// Verify that a stack property is not added to non-Errors
const obj = {};
internalUtil.decorateErrorStack(obj);
assert.strictEqual(obj.stack, undefined);
// Verify that the stack is decorated when possible
function checkStack(stack) {
const matches = stack.match(/var foo bar;/g);
assert.strictEqual(Array.isArray(matches), true);
assert.strictEqual(matches.length, 1);
}
let err;
const badSyntaxPath =
fixtures.path('syntax', 'bad_syntax').replace(/\\/g, '\\\\');
try {
require(badSyntaxPath);
} catch (e) {
err = e;
}
assert(typeof err, 'object');
checkStack(err.stack);
// Verify that the stack is only decorated once
internalUtil.decorateErrorStack(err);
internalUtil.decorateErrorStack(err);
checkStack(err.stack);
// Verify that the stack is only decorated once for uncaught exceptions
const args = [
'-e',
`require('${badSyntaxPath}')`
];
const result = spawnSync(process.argv[0], args, { encoding: 'utf8' });
checkStack(result.stderr);
// Verify that the stack is unchanged when there is no arrow message
err = new Error('foo');
const originalStack = err.stack;
internalUtil.decorateErrorStack(err);
assert.strictEqual(originalStack, err.stack);