Serenity Operating System
1test("basic functionality", () => {
2 expect(function () {}.name).toBe("");
3
4 function bar() {}
5 expect(bar.name).toBe("bar");
6 expect((bar.name = "baz")).toBe("baz");
7 expect(bar.name).toBe("bar");
8});
9
10test("function assigned to variable", () => {
11 let foo = function () {};
12 expect(foo.name).toBe("foo");
13 expect((foo.name = "bar")).toBe("bar");
14 expect(foo.name).toBe("foo");
15
16 let a, b;
17 a = b = function () {};
18 expect(a.name).toBe("b");
19 expect(b.name).toBe("b");
20});
21
22test("functions in array assigned to variable", () => {
23 const arr = [function () {}, function () {}, function () {}];
24 expect(arr[0].name).toBe("");
25 expect(arr[1].name).toBe("");
26 expect(arr[2].name).toBe("");
27});
28
29test("functions in objects", () => {
30 let f;
31 let o = { a: function () {} };
32
33 expect(o.a.name).toBe("a");
34 f = o.a;
35 expect(f.name).toBe("a");
36 expect(o.a.name).toBe("a");
37
38 o = { ...o, b: f };
39 expect(o.a.name).toBe("a");
40 expect(o.b.name).toBe("a");
41
42 // Member expressions do not get named.
43 o.c = function () {};
44 expect(o.c.name).toBe("");
45});
46
47test("names of native functions", () => {
48 expect(console.debug.name).toBe("debug");
49 expect((console.debug.name = "warn")).toBe("warn");
50 expect(console.debug.name).toBe("debug");
51});
52
53describe("some anonymous functions get renamed", () => {
54 test("direct assignment does name new function expression", () => {
55 // prettier-ignore
56 let f1 = (function () {});
57 expect(f1.name).toBe("f1");
58
59 let f2 = false;
60 f2 ||= function () {};
61 expect(f2.name).toBe("f2");
62 });
63
64 test("assignment from variable does not name", () => {
65 const f1 = function () {};
66 let f3 = f1;
67 expect(f3.name).toBe("f1");
68 });
69
70 test("assignment via expression does not name", () => {
71 let f4 = false || function () {};
72 expect(f4.name).toBe("");
73 });
74});