1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 11:17:35 +00:00

LibJS/Tests: Consolidate Error.prototype.stack tests

We don't usually separate tests for the getter and setter of a property,
and that error-stack.js (getter) test belongs in builtins/.
This commit is contained in:
Linus Groh 2022-03-15 14:10:38 +00:00 committed by Andreas Kling
parent ecba29c158
commit 37e988675f
2 changed files with 44 additions and 30 deletions

View file

@ -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);

View file

@ -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();
}
});