summaryrefslogtreecommitdiff
path: root/test/parallel/test-assert-if-error.js
blob: 9bc58cd73825a6f074f0cf1926796b26edd9e902 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
'use strict';

require('../common');
const assert = require('assert');

// Test that assert.ifError has the correct stack trace of both stacks.

let err;
// Create some random error frames.
(function a() {
  (function b() {
    (function c() {
      err = new Error('test error');
    })();
  })();
})();

const msg = err.message;
const stack = err.stack;

(function x() {
  (function y() {
    (function z() {
      let threw = false;
      try {
        assert.ifError(err);
      } catch (e) {
        assert.strictEqual(e.message,
                           'ifError got unwanted exception: test error');
        assert.strictEqual(err.message, msg);
        assert.strictEqual(e.actual, err);
        assert.strictEqual(e.actual.stack, stack);
        assert.strictEqual(e.expected, null);
        assert.strictEqual(e.operator, 'ifError');
        threw = true;
      }
      assert(threw);
    })();
  })();
})();

assert.throws(
  () => {
    const error = new Error();
    error.stack = 'Error: containing weird stack\nYes!\nI am part of a stack.';
    assert.ifError(error);
  },
  (error) => {
    assert(!error.stack.includes('Yes!'));
    return true;
  }
);

assert.throws(
  () => assert.ifError(new TypeError()),
  {
    message: 'ifError got unwanted exception: TypeError'
  }
);

assert.throws(
  () => assert.ifError({ stack: false }),
  {
    message: 'ifError got unwanted exception: { stack: false }'
  }
);

assert.throws(
  () => assert.ifError({ constructor: null, message: '' }),
  {
    message: 'ifError got unwanted exception: '
  }
);

assert.throws(
  () => { assert.ifError(false); },
  {
    message: 'ifError got unwanted exception: false'
  }
);

// Should not throw.
assert.ifError(null);
assert.ifError();
assert.ifError(undefined);

// https://github.com/nodejs/node-v0.x-archive/issues/2893
{
  let threw = false;
  try {
    // eslint-disable-next-line no-restricted-syntax
    assert.throws(() => {
      assert.ifError(null);
    });
  } catch (e) {
    threw = true;
    assert.strictEqual(e.message, 'Missing expected exception.');
    assert(!e.stack.includes('throws'), e);
  }
  assert(threw);
}