diff --git a/Userland/Libraries/LibJS/Tests/builtins/Error/Error.prototype.stack-setter.js b/Userland/Libraries/LibJS/Tests/builtins/Error/Error.prototype.stack.js similarity index 59% rename from Userland/Libraries/LibJS/Tests/builtins/Error/Error.prototype.stack-setter.js rename to Userland/Libraries/LibJS/Tests/builtins/Error/Error.prototype.stack.js index f987ce962b..807cf7944d 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/Error/Error.prototype.stack-setter.js +++ b/Userland/Libraries/LibJS/Tests/builtins/Error/Error.prototype.stack.js @@ -1,7 +1,49 @@ const stackDescriptor = Object.getOwnPropertyDescriptor(Error.prototype, "stack"); const stackSetter = stackDescriptor.set; -describe("normal behavior", () => { +describe("getter - normal behavior", () => { + test("basic functionality", () => { + const stackFrames = [ + /^ at .*Error \(.*\/Error\.prototype\.stack\.js:\d+:\d+\)$/, + /^ at .+\/Error\/Error\.prototype\.stack\.js:\d+:\d+$/, + /^ at test \(.+\/test-common.js:557:21\)$/, + /^ at .+\/Error\/Error\.prototype\.stack\.js:5:33$/, + /^ at describe \(.+\/test-common\.js:534:21\)$/, + /^ at .+\/Error\/Error\.prototype\.stack\.js:4:38$/, + ]; + const values = [ + { + error: new Error(), + header: "Error", + stackFrames, + }, + { + error: new TypeError(), + header: "TypeError", + stackFrames, + }, + { + error: new Error("Something went wrong!"), + header: "Error: Something went wrong!", + stackFrames, + }, + ]; + + for (const { error, header: expectedHeader, stackFrames: expectedStackFrames } of values) { + const [header, ...stackFrames] = error.stack.trim().split("\n"); + + expect(header).toBe(expectedHeader); + expect(stackFrames).toHaveLength(expectedStackFrames.length); + for (let i = 0; i < stackFrames.length; ++i) { + const stackFrame = stackFrames[i]; + const expectedStackFrame = expectedStackFrames[i]; + expect(!!stackFrame.match(expectedStackFrame)).toBeTrue(); + } + } + }); +}); + +describe("setter - normal behavior", () => { test("basic functionality", () => { "use strict"; const error = new Error(); @@ -38,7 +80,7 @@ describe("normal behavior", () => { }); }); -describe("errors", () => { +describe("setter - errors", () => { test("this is not an object", () => { expect(() => { stackSetter.call(undefined); diff --git a/Userland/Libraries/LibJS/Tests/error-stack.js b/Userland/Libraries/LibJS/Tests/error-stack.js deleted file mode 100644 index 5b57e76dcd..0000000000 --- a/Userland/Libraries/LibJS/Tests/error-stack.js +++ /dev/null @@ -1,28 +0,0 @@ -test("Anonymous function", function () { - let stackString = (() => { - return Error(); - })().stack; - let [header, ...stackFrames] = stackString.split("\n"); - - expect(header).toBe("Error"); - expect(!!stackFrames[0].match(/^ at Error \(.*\/error-stack\.js:3:\d+\)$/)).toBeTrue(); - expect(!!stackFrames[1].match(/^ at .*\/error-stack\.js:3:\d+$/)).toBeTrue(); - expect(!!stackFrames[2].match(/^ at .*\/error-stack\.js:2:\d+$/)).toBeTrue(); -}); - -test("Named function with message", function () { - function f() { - throw Error("You Shalt Not Pass!"); - } - try { - f(); - } catch (e) { - let stackString = e.stack; - let [header, ...stack_frames] = stackString.split("\n"); - - expect(header).toBe("Error: You Shalt Not Pass!"); - expect(!!stack_frames[0].match(/^ at Error \(.*\/error-stack\.js:15:\d+\)$/)).toBeTrue(); - expect(!!stack_frames[1].match(/^ at f \(.*\/error-stack\.js:15:\d+\)$/)).toBeTrue(); - expect(!!stack_frames[2].match(/^ at .*\/error-stack\.js:18:\d+$/)).toBeTrue(); - } -});