1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 13:47: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);