function-name.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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("");
  21. expect(arr[1].name).toBe("");
  22. expect(arr[2].name).toBe("");
  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("no invalid autonaming of anonymous functions", () => {
  43. // prettier-ignore
  44. let f1 = (function () {});
  45. expect(f1.name).toBe("");
  46. let f2 = f1;
  47. expect(f2.name).toBe("");
  48. let f3;
  49. f3 = false || f2;
  50. expect(f3.name).toBe("");
  51. let f4 = false;
  52. f4 ||= function () {};
  53. expect(f4.name).toBe("");
  54. });