File: index.spec.js

package info (click to toggle)
node-make-error 1.3.7-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 400 kB
  • sloc: javascript: 253; sh: 9; makefile: 6
file content (164 lines) | stat: -rw-r--r-- 4,565 bytes parent folder | download | duplicates (2)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
"use strict";

/* eslint-env jest */

// ===================================================================

var makeError = require("./");
var BaseError = makeError.BaseError;

// ===================================================================

function getCommonLastItems(arr1, arr2, butFirsts) {
  var n = Math.min(arr1.length, arr2.length) - (butFirsts || 0);

  return [arr1.slice(-n), arr2.slice(-n)];
}

function compareStacks(actual, expected) {
  actual = actual.split(/\r?\n/);
  expected = expected.split(/\r?\n/);

  // We want to compare only the beginning (bottom) of the stack and
  // we don't want to compare the first (from head) two items.
  var tmp = getCommonLastItems(actual, expected, 2);
  actual = tmp[0];
  expected = tmp[1];

  expect(actual).toEqual(expected);
}

function randomString() {
  return Math.random().toString(36).slice(2);
}

// ===================================================================

var factories = {
  "makeError(name)": makeError,
  "makeError(constructor)": function (name, super_) {
    function MyError(message) {
      MyError.super.call(this, message);
    }
    Object.defineProperty(MyError, "name", { value: name });

    expect(makeError(MyError, super_)).toBe(MyError);
    return MyError;
  },
  "ES3 inheritance": function (name, super_) {
    function MyError(message) {
      BaseError.call(this, message);
    }
    Object.defineProperty(MyError, "name", {
      value: name,
    });

    function Tmp() {
      this.constructor = MyError;
    }
    Tmp.prototype = super_.prototype;
    MyError.prototype = new Tmp();

    return MyError;
  },
  "ES5 inheritance": function (name, super_) {
    function MyError(message) {
      super_.call(this, message);
    }
    Object.defineProperty(MyError, "name", {
      value: name,
    });

    // Manually inherits from BaseError.
    MyError.prototype = Object.create(super_.prototype, {
      constructor: {
        configurable: true,
        value: MyError,
        writable: true,
      },
    });

    return MyError;
  },
  "ES6 inheritance":
    typeof Reflect !== "undefined" &&
    function (name, super_) {
      /* eslint-disable-next-line no-eval */
      return Object.defineProperty(eval("(class extends super_ {})"), "name", {
        value: name,
      });
    },
};

var keys = Object.keys(factories);

it("makeError throws on invalid arguments", function () {
  expect(function () {
    makeError(42);
  }).toThrow(TypeError);
  expect(function () {
    makeError("MyError", 42);
  }).toThrow(TypeError);
});

keys.forEach(function (title) {
  var factory = factories[title];
  (factory ? describe : describe.skip)(title, function () {
    it("creates a new error class", function () {
      var name = randomString();
      var MyError = factory(name, BaseError);

      var message = randomString();
      var e = new MyError(message);
      var stack = new Error().stack;

      expect(e).toBeInstanceOf(Error);
      expect(e).toBeInstanceOf(BaseError);
      expect(e).toBeInstanceOf(MyError);

      expect(e.name).toBe(name);
      expect(e.message).toBe(message);
      expect(typeof e.stack).toBe("string");
      compareStacks(e.stack, stack);
    });

    describe("derivable with", function () {
      keys.forEach(function (title2) {
        var factory2 = factories[title2];

        // these use cases are known not to be working, mark them as skipped
        if (
          (title === "makeError(name)" &&
            (title2 === "makeError(constructor)" ||
              title2 === "ES5 inheritance")) ||
          (title === "ES6 inheritance" &&
            (title2 === "makeError(constructor)" ||
              title2 === "ES5 inheritance"))
        ) {
          factory2 = undefined;
        }

        (factory2 ? it : it.skip)(title2, function () {
          var baseName = randomString();
          var MyBaseError = factory(baseName, BaseError);
          var derivedName = randomString();
          var MyDerivedError = factory2(derivedName, MyBaseError);

          var message = randomString();
          var e = new MyDerivedError(message);
          var stack = new Error().stack;

          expect(e).toBeInstanceOf(Error);
          expect(e).toBeInstanceOf(BaseError);
          expect(e).toBeInstanceOf(MyBaseError);
          expect(e).toBeInstanceOf(MyDerivedError);

          expect(e.name).toBe(derivedName);
          expect(e.message).toBe(message);
          expect(typeof e.stack).toBe("string");
          compareStacks(e.stack, stack);
        });
      });
    });
  });
});