function-name.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. test("basic functionality", () => {
  2. expect(function () {}.name).toBe("");
  3. function bar() {}
  4. expect(bar.name).toBe("bar");
  5. expect((bar.name = "baz")).toBe("baz");
  6. expect(bar.name).toBe("bar");
  7. });
  8. test("function assigned to variable", () => {
  9. let foo = function () {};
  10. expect(foo.name).toBe("foo");
  11. expect((foo.name = "bar")).toBe("bar");
  12. expect(foo.name).toBe("foo");
  13. let a, b;
  14. a = b = function () {};
  15. expect(a.name).toBe("b");
  16. expect(b.name).toBe("b");
  17. });
  18. test("functions in array assigned to variable", () => {
  19. const arr = [function () {}, function () {}, function () {}];
  20. expect(arr[0].name).toBe("arr");
  21. expect(arr[1].name).toBe("arr");
  22. expect(arr[2].name).toBe("arr");
  23. });
  24. test("functions in objects", () => {
  25. let f;
  26. let o = { a: function () {} };
  27. expect(o.a.name).toBe("a");
  28. f = o.a;
  29. expect(f.name).toBe("a");
  30. expect(o.a.name).toBe("a");
  31. o = { ...o, b: f };
  32. expect(o.a.name).toBe("a");
  33. expect(o.b.name).toBe("a");
  34. o.c = function () {};
  35. expect(o.c.name).toBe("c");
  36. });
  37. test("names of native functions", () => {
  38. expect(console.debug.name).toBe("debug");
  39. expect((console.debug.name = "warn")).toBe("warn");
  40. expect(console.debug.name).toBe("debug");
  41. });
  42. test("cyclic members should not cause infinite recursion (#3471)", () => {
  43. let a = [() => 4];
  44. a[1] = a;
  45. a = a;
  46. expect(a[0].name).toBe("a");
  47. });