function-name.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. });