1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 22:48:11 +00:00

LibJS: Add name and message properties to NativeError prototypes

Otherwise these will get their name/default message from the Error
prototype, and as a result would always just say "Error" in error
messages, not the specific type.
Something I missed in da177c6, now with tests. :^)
This commit is contained in:
Linus Groh 2021-04-14 10:02:51 +02:00 committed by Andreas Kling
parent 1aec9a508e
commit ea60b344eb
4 changed files with 52 additions and 9 deletions

View file

@ -0,0 +1,26 @@
describe("normal behavior", () => {
test("initial message value is empty string", () => {
expect(Error.prototype.message).toBe("");
expect(EvalError.prototype.message).toBe("");
expect(RangeError.prototype.message).toBe("");
expect(ReferenceError.prototype.message).toBe("");
expect(SyntaxError.prototype.message).toBe("");
expect(TypeError.prototype.message).toBe("");
});
test("Error gets message via prototype by default", () => {
const error = new Error();
expect(error.hasOwnProperty("message")).toBeFalse();
expect(error.message).toBe("");
Error.prototype.message = "Well hello friends";
expect(error.message).toBe("Well hello friends");
});
test("Error gets message via object if given to constructor", () => {
const error = new Error("Custom error message");
expect(error.hasOwnProperty("message")).toBeTrue();
expect(error.message).toBe("Custom error message");
Error.prototype.message = "Well hello friends";
expect(error.message).toBe("Custom error message");
});
});

View file

@ -1,10 +1,18 @@
test("basic functionality", () => {
expect(Error.prototype).not.toHaveProperty("length");
describe("normal behavior", () => {
test("initial name value is type name", () => {
expect(Error.prototype.name).toBe("Error");
expect(EvalError.prototype.name).toBe("EvalError");
expect(RangeError.prototype.name).toBe("RangeError");
expect(ReferenceError.prototype.name).toBe("ReferenceError");
expect(SyntaxError.prototype.name).toBe("SyntaxError");
expect(TypeError.prototype.name).toBe("TypeError");
});
var changedInstance = new Error("");
changedInstance.name = "NewCustomError";
expect(changedInstance.name).toBe("NewCustomError");
var normalInstance = new Error("");
expect(normalInstance.name).toBe("Error");
test("Error gets name via prototype", () => {
const error = new Error();
expect(error.hasOwnProperty("name")).toBeFalse();
expect(error.name).toBe("Error");
Error.prototype.name = "Foo";
expect(error.name).toBe("Foo");
});
});