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