1'use strict'; 2 3require('../common'); 4const assert = require('assert'); 5 6// Test that assert.ifError has the correct stack trace of both stacks. 7 8let err; 9// Create some random error frames. 10(function a() { 11 (function b() { 12 (function c() { 13 err = new Error('test error'); 14 })(); 15 })(); 16})(); 17 18const msg = err.message; 19const stack = err.stack; 20 21(function x() { 22 (function y() { 23 (function z() { 24 let threw = false; 25 try { 26 assert.ifError(err); 27 } catch (e) { 28 assert.strictEqual(e.message, 29 'ifError got unwanted exception: test error'); 30 assert.strictEqual(err.message, msg); 31 assert.strictEqual(e.actual, err); 32 assert.strictEqual(e.actual.stack, stack); 33 assert.strictEqual(e.expected, null); 34 assert.strictEqual(e.operator, 'ifError'); 35 threw = true; 36 } 37 assert(threw); 38 })(); 39 })(); 40})(); 41 42assert.throws( 43 () => assert.ifError(new TypeError()), 44 { 45 message: 'ifError got unwanted exception: TypeError' 46 } 47); 48 49assert.throws( 50 () => assert.ifError({ stack: false }), 51 { 52 message: 'ifError got unwanted exception: { stack: false }' 53 } 54); 55 56assert.throws( 57 () => assert.ifError({ constructor: null, message: '' }), 58 { 59 message: 'ifError got unwanted exception: ' 60 } 61); 62 63assert.throws( 64 () => { assert.ifError(false); }, 65 { 66 message: 'ifError got unwanted exception: false' 67 } 68); 69 70// Should not throw. 71assert.ifError(null); 72assert.ifError(); 73assert.ifError(undefined); 74 75// https://github.com/nodejs/node-v0.x-archive/issues/2893 76{ 77 let threw = false; 78 try { 79 // eslint-disable-next-line no-restricted-syntax 80 assert.throws(() => { 81 assert.ifError(null); 82 }); 83 } catch (e) { 84 threw = true; 85 assert.strictEqual(e.message, 'Missing expected exception.'); 86 assert(!e.stack.includes('throws'), e); 87 } 88 assert(threw); 89} 90