|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const { Ono } = require("../../"); |
| 4 | +const { assert, expect } = require("chai"); |
| 5 | +const { compareKeys, comparePOJO, compareStacks, host } = require("../utils"); |
| 6 | + |
| 7 | +// https://nodejs.org/api/util.html#util_util_inspect_custom |
| 8 | +const inspect = Symbol.for("nodejs.util.inspect.custom"); |
| 9 | + |
| 10 | +describe("Ono.extend()", () => { |
| 11 | + |
| 12 | + it("should enhance an error object with Ono functionality", () => { |
| 13 | + function createRangeError () { |
| 14 | + return new RangeError("Something went wrong"); |
| 15 | + } |
| 16 | + |
| 17 | + let error = createRangeError(); |
| 18 | + let onoError = Ono.extend(error); |
| 19 | + |
| 20 | + expect(onoError).to.equal(error); |
| 21 | + expect(onoError.name).to.equal("RangeError"); |
| 22 | + expect(onoError.message).to.equal("Something went wrong"); |
| 23 | + expect(onoError.stack).to.satisfy(compareStacks(["createRangeError"])); |
| 24 | + expect(onoError).to.satisfy(compareKeys("name", "message", "stack", "toJSON")); |
| 25 | + |
| 26 | + expect(onoError.toJSON()).to.satisfy(comparePOJO({ |
| 27 | + name: "RangeError", |
| 28 | + message: "Something went wrong", |
| 29 | + stack: error.stack |
| 30 | + })); |
| 31 | + |
| 32 | + if (host.node) { |
| 33 | + expect(onoError[inspect]()).to.satisfy(comparePOJO({ |
| 34 | + name: "RangeError", |
| 35 | + message: "Something went wrong", |
| 36 | + stack: error.stack, |
| 37 | + toJSON: error.toJSON, |
| 38 | + toString: error.toString, |
| 39 | + })); |
| 40 | + } |
| 41 | + }); |
| 42 | + |
| 43 | + it("should include the stack trace of the original error", () => { |
| 44 | + function createOriginalError () { |
| 45 | + return new RangeError("Bad range"); |
| 46 | + } |
| 47 | + |
| 48 | + function createNewError () { |
| 49 | + return new SyntaxError("Invalid syntax"); |
| 50 | + } |
| 51 | + |
| 52 | + let originalError = createOriginalError(); |
| 53 | + let newError = createNewError(); |
| 54 | + let onoError = Ono.extend(newError, originalError); |
| 55 | + |
| 56 | + expect(onoError).to.equal(newError); |
| 57 | + expect(onoError.name).to.equal("SyntaxError"); |
| 58 | + expect(onoError.message).to.equal("Invalid syntax"); |
| 59 | + expect(onoError.stack).to.satisfy(compareStacks( |
| 60 | + ["createNewError"], |
| 61 | + ["createOriginalError"], |
| 62 | + )); |
| 63 | + expect(onoError).to.satisfy(compareKeys("name", "message", "stack", "toJSON")); |
| 64 | + |
| 65 | + expect(onoError.toJSON()).to.satisfy(comparePOJO({ |
| 66 | + name: "SyntaxError", |
| 67 | + message: "Invalid syntax", |
| 68 | + stack: newError.stack |
| 69 | + })); |
| 70 | + |
| 71 | + if (host.node) { |
| 72 | + expect(onoError[inspect]()).to.satisfy(comparePOJO({ |
| 73 | + name: "SyntaxError", |
| 74 | + message: "Invalid syntax", |
| 75 | + stack: newError.stack, |
| 76 | + toJSON: newError.toJSON, |
| 77 | + toString: newError.toString, |
| 78 | + })); |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + it("should add custom props", () => { |
| 83 | + function createTypeError () { |
| 84 | + return new TypeError("The type is all wrong"); |
| 85 | + } |
| 86 | + |
| 87 | + let error = createTypeError(); |
| 88 | + let onoError = Ono.extend(error, { |
| 89 | + foo: "bar", |
| 90 | + timestamp: new Date("2005-05-05T05:05:05.005Z") |
| 91 | + }); |
| 92 | + |
| 93 | + expect(onoError).to.equal(error); |
| 94 | + expect(onoError.name).to.equal("TypeError"); |
| 95 | + expect(onoError.message).to.equal("The type is all wrong"); |
| 96 | + expect(onoError.stack).to.satisfy(compareStacks(["createTypeError"])); |
| 97 | + expect(onoError).to.satisfy(compareKeys("name", "message", "stack", "toJSON", "foo", "timestamp")); |
| 98 | + |
| 99 | + expect(onoError.toJSON()).to.satisfy(comparePOJO({ |
| 100 | + name: "TypeError", |
| 101 | + message: "The type is all wrong", |
| 102 | + stack: error.stack, |
| 103 | + foo: "bar", |
| 104 | + timestamp: new Date("2005-05-05T05:05:05.005Z"), |
| 105 | + })); |
| 106 | + |
| 107 | + if (host.node) { |
| 108 | + expect(onoError[inspect]()).to.satisfy(comparePOJO({ |
| 109 | + name: "TypeError", |
| 110 | + message: "The type is all wrong", |
| 111 | + stack: error.stack, |
| 112 | + toJSON: error.toJSON, |
| 113 | + toString: error.toString, |
| 114 | + foo: "bar", |
| 115 | + timestamp: new Date("2005-05-05T05:05:05.005Z"), |
| 116 | + })); |
| 117 | + } |
| 118 | + }); |
| 119 | + |
| 120 | + it("should include custom props and the stack trace of the original error", () => { |
| 121 | + function createOriginalError () { |
| 122 | + return new RangeError("Bad range"); |
| 123 | + } |
| 124 | + |
| 125 | + function createNewError () { |
| 126 | + return new SyntaxError("Invalid syntax"); |
| 127 | + } |
| 128 | + |
| 129 | + let originalError = createOriginalError(); |
| 130 | + let newError = createNewError(); |
| 131 | + let onoError = Ono.extend(newError, originalError, { |
| 132 | + foo: "bar", |
| 133 | + timestamp: new Date("2005-05-05T05:05:05.005Z") |
| 134 | + }); |
| 135 | + |
| 136 | + expect(onoError).to.equal(newError); |
| 137 | + expect(onoError.name).to.equal("SyntaxError"); |
| 138 | + expect(onoError.message).to.equal("Invalid syntax"); |
| 139 | + expect(onoError.stack).to.satisfy(compareStacks( |
| 140 | + ["createNewError"], |
| 141 | + ["createOriginalError"], |
| 142 | + )); |
| 143 | + expect(onoError).to.satisfy(compareKeys("name", "message", "stack", "toJSON", "foo", "timestamp")); |
| 144 | + |
| 145 | + expect(onoError.toJSON()).to.satisfy(comparePOJO({ |
| 146 | + name: "SyntaxError", |
| 147 | + message: "Invalid syntax", |
| 148 | + stack: newError.stack, |
| 149 | + foo: "bar", |
| 150 | + timestamp: new Date("2005-05-05T05:05:05.005Z"), |
| 151 | + })); |
| 152 | + |
| 153 | + if (host.node) { |
| 154 | + expect(onoError[inspect]()).to.satisfy(comparePOJO({ |
| 155 | + name: "SyntaxError", |
| 156 | + message: "Invalid syntax", |
| 157 | + stack: newError.stack, |
| 158 | + toJSON: newError.toJSON, |
| 159 | + toString: newError.toString, |
| 160 | + foo: "bar", |
| 161 | + timestamp: new Date("2005-05-05T05:05:05.005Z"), |
| 162 | + })); |
| 163 | + } |
| 164 | + }); |
| 165 | + |
| 166 | + it("should throw an error if called without arguments", () => { |
| 167 | + try { |
| 168 | + Ono.extend(); |
| 169 | + assert.fail("An error should have been thrown"); |
| 170 | + } |
| 171 | + catch (error) { |
| 172 | + expect(error).to.be.an.instanceOf(TypeError); |
| 173 | + expect(error.message).to.include("undefined"); |
| 174 | + } |
| 175 | + }); |
| 176 | + |
| 177 | + it("should throw an error if called with null", () => { |
| 178 | + try { |
| 179 | + Ono.extend(null); |
| 180 | + assert.fail("An error should have been thrown"); |
| 181 | + } |
| 182 | + catch (error) { |
| 183 | + expect(error).to.be.an.instanceOf(TypeError); |
| 184 | + expect(error.message).to.include("null"); |
| 185 | + } |
| 186 | + }); |
| 187 | + |
| 188 | +}); |
0 commit comments