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 () => { 44 const error = new Error(); 45 error.stack = 'Error: containing weird stack\nYes!\nI am part of a stack.'; 46 assert.ifError(error); 47 }, 48 (error) => { 49 assert(!error.stack.includes('Yes!')); 50 return true; 51 } 52); 53 54assert.throws( 55 () => assert.ifError(new TypeError()), 56 { 57 message: 'ifError got unwanted exception: TypeError' 58 } 59); 60 61assert.throws( 62 () => assert.ifError({ stack: false }), 63 { 64 message: 'ifError got unwanted exception: { stack: false }' 65 } 66); 67 68assert.throws( 69 () => assert.ifError({ constructor: null, message: '' }), 70 { 71 message: 'ifError got unwanted exception: ' 72 } 73); 74 75assert.throws( 76 () => { assert.ifError(false); }, 77 { 78 message: 'ifError got unwanted exception: false' 79 } 80); 81 82// Should not throw. 83assert.ifError(null); 84assert.ifError(); 85assert.ifError(undefined); 86 87// https://github.com/nodejs/node-v0.x-archive/issues/2893 88{ 89 let threw = false; 90 try { 91 // eslint-disable-next-line no-restricted-syntax 92 assert.throws(() => { 93 assert.ifError(null); 94 }); 95 } catch (e) { 96 threw = true; 97 assert.strictEqual(e.message, 'Missing expected exception.'); 98 assert(!e.stack.includes('throws'), e); 99 } 100 assert(threw); 101} 102