11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_cirequire('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci// Test that assert.ifError has the correct stack trace of both stacks.
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_cilet err;
91cb0ef41Sopenharmony_ci// Create some random error frames.
101cb0ef41Sopenharmony_ci(function a() {
111cb0ef41Sopenharmony_ci  (function b() {
121cb0ef41Sopenharmony_ci    (function c() {
131cb0ef41Sopenharmony_ci      err = new Error('test error');
141cb0ef41Sopenharmony_ci    })();
151cb0ef41Sopenharmony_ci  })();
161cb0ef41Sopenharmony_ci})();
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ciconst msg = err.message;
191cb0ef41Sopenharmony_ciconst stack = err.stack;
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci(function x() {
221cb0ef41Sopenharmony_ci  (function y() {
231cb0ef41Sopenharmony_ci    (function z() {
241cb0ef41Sopenharmony_ci      let threw = false;
251cb0ef41Sopenharmony_ci      try {
261cb0ef41Sopenharmony_ci        assert.ifError(err);
271cb0ef41Sopenharmony_ci      } catch (e) {
281cb0ef41Sopenharmony_ci        assert.strictEqual(e.message,
291cb0ef41Sopenharmony_ci                           'ifError got unwanted exception: test error');
301cb0ef41Sopenharmony_ci        assert.strictEqual(err.message, msg);
311cb0ef41Sopenharmony_ci        assert.strictEqual(e.actual, err);
321cb0ef41Sopenharmony_ci        assert.strictEqual(e.actual.stack, stack);
331cb0ef41Sopenharmony_ci        assert.strictEqual(e.expected, null);
341cb0ef41Sopenharmony_ci        assert.strictEqual(e.operator, 'ifError');
351cb0ef41Sopenharmony_ci        threw = true;
361cb0ef41Sopenharmony_ci      }
371cb0ef41Sopenharmony_ci      assert(threw);
381cb0ef41Sopenharmony_ci    })();
391cb0ef41Sopenharmony_ci  })();
401cb0ef41Sopenharmony_ci})();
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ciassert.throws(
431cb0ef41Sopenharmony_ci  () => {
441cb0ef41Sopenharmony_ci    const error = new Error();
451cb0ef41Sopenharmony_ci    error.stack = 'Error: containing weird stack\nYes!\nI am part of a stack.';
461cb0ef41Sopenharmony_ci    assert.ifError(error);
471cb0ef41Sopenharmony_ci  },
481cb0ef41Sopenharmony_ci  (error) => {
491cb0ef41Sopenharmony_ci    assert(!error.stack.includes('Yes!'));
501cb0ef41Sopenharmony_ci    return true;
511cb0ef41Sopenharmony_ci  }
521cb0ef41Sopenharmony_ci);
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ciassert.throws(
551cb0ef41Sopenharmony_ci  () => assert.ifError(new TypeError()),
561cb0ef41Sopenharmony_ci  {
571cb0ef41Sopenharmony_ci    message: 'ifError got unwanted exception: TypeError'
581cb0ef41Sopenharmony_ci  }
591cb0ef41Sopenharmony_ci);
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ciassert.throws(
621cb0ef41Sopenharmony_ci  () => assert.ifError({ stack: false }),
631cb0ef41Sopenharmony_ci  {
641cb0ef41Sopenharmony_ci    message: 'ifError got unwanted exception: { stack: false }'
651cb0ef41Sopenharmony_ci  }
661cb0ef41Sopenharmony_ci);
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ciassert.throws(
691cb0ef41Sopenharmony_ci  () => assert.ifError({ constructor: null, message: '' }),
701cb0ef41Sopenharmony_ci  {
711cb0ef41Sopenharmony_ci    message: 'ifError got unwanted exception: '
721cb0ef41Sopenharmony_ci  }
731cb0ef41Sopenharmony_ci);
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ciassert.throws(
761cb0ef41Sopenharmony_ci  () => { assert.ifError(false); },
771cb0ef41Sopenharmony_ci  {
781cb0ef41Sopenharmony_ci    message: 'ifError got unwanted exception: false'
791cb0ef41Sopenharmony_ci  }
801cb0ef41Sopenharmony_ci);
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ci// Should not throw.
831cb0ef41Sopenharmony_ciassert.ifError(null);
841cb0ef41Sopenharmony_ciassert.ifError();
851cb0ef41Sopenharmony_ciassert.ifError(undefined);
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ci// https://github.com/nodejs/node-v0.x-archive/issues/2893
881cb0ef41Sopenharmony_ci{
891cb0ef41Sopenharmony_ci  let threw = false;
901cb0ef41Sopenharmony_ci  try {
911cb0ef41Sopenharmony_ci    // eslint-disable-next-line no-restricted-syntax
921cb0ef41Sopenharmony_ci    assert.throws(() => {
931cb0ef41Sopenharmony_ci      assert.ifError(null);
941cb0ef41Sopenharmony_ci    });
951cb0ef41Sopenharmony_ci  } catch (e) {
961cb0ef41Sopenharmony_ci    threw = true;
971cb0ef41Sopenharmony_ci    assert.strictEqual(e.message, 'Missing expected exception.');
981cb0ef41Sopenharmony_ci    assert(!e.stack.includes('throws'), e);
991cb0ef41Sopenharmony_ci  }
1001cb0ef41Sopenharmony_ci  assert(threw);
1011cb0ef41Sopenharmony_ci}
102