ladybird/Libraries/LibJS/Tests/null-or-undefined-access.js
Timothy Flynn 019c529c07 Meta: Increase the line length enforced by prettier to 120
This matches our coding style recommendation in CodingStyle.md, and
matches our python formatting.
2025-10-31 19:55:50 -04:00

66 lines
2.2 KiB
JavaScript

test("null/undefined object", () => {
[null, undefined].forEach(value => {
let foo = value;
expect(() => {
foo.bar;
}).toThrowWithMessage(TypeError, `Cannot access property "bar" on ${value} object "foo"`);
expect(() => {
foo.bar = 1;
}).toThrowWithMessage(TypeError, `Cannot access property "bar" on ${value} object "foo"`);
expect(() => {
foo[0];
}).toThrowWithMessage(TypeError, `Cannot access property "0" on ${value} object "foo"`);
expect(() => {
foo[0] = 1;
}).toThrowWithMessage(TypeError, `Cannot access property "0" on ${value} object "foo"`);
});
});
test("null/undefined object key", () => {
[null, undefined].forEach(value => {
let foo = { bar: value };
expect(() => {
foo.bar.baz;
}).toThrowWithMessage(TypeError, `Cannot access property "baz" on ${value} object "foo.bar"`);
expect(() => {
foo.bar.baz = 1;
}).toThrowWithMessage(TypeError, `Cannot access property "baz" on ${value} object "foo.bar"`);
expect(() => {
foo["bar"].baz;
}).toThrowWithMessage(TypeError, `Cannot access property "baz" on ${value} object "foo['bar']"`);
expect(() => {
foo["bar"].baz = 1;
}).toThrowWithMessage(TypeError, `Cannot access property "baz" on ${value} object "foo['bar']"`);
});
});
test("null/undefined array index", () => {
[null, undefined].forEach(value => {
let foo = [value];
let index = 0;
expect(() => {
foo[0].bar;
}).toThrowWithMessage(TypeError, `Cannot access property "bar" on ${value} object "foo[0]"`);
expect(() => {
foo[0].bar = 1;
}).toThrowWithMessage(TypeError, `Cannot access property "bar" on ${value} object "foo[0]"`);
expect(() => {
foo[index].bar;
}).toThrowWithMessage(TypeError, `Cannot access property "bar" on ${value} object "foo[index]"`);
expect(() => {
foo[index].bar = 1;
}).toThrowWithMessage(TypeError, `Cannot access property "bar" on ${value} object "foo[index]"`);
});
});