function-rest-params.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. test("rest parameter with no arguments", () => {
  2. function foo(...a) {
  3. expect(a).toBeInstanceOf(Array);
  4. expect(a).toHaveLength(0);
  5. }
  6. foo();
  7. });
  8. test("rest parameter with arguments", () => {
  9. function foo(...a) {
  10. expect(a).toEqual(["foo", 123, undefined, { foo: "bar" }]);
  11. }
  12. foo("foo", 123, undefined, { foo: "bar" });
  13. });
  14. test("rest parameter after normal parameters with no arguments", () => {
  15. function foo(a, b, ...c) {
  16. expect(a).toBe("foo");
  17. expect(b).toBe(123);
  18. expect(c).toEqual([]);
  19. }
  20. foo("foo", 123);
  21. });
  22. test("rest parameter after normal parameters with arguments", () => {
  23. function foo(a, b, ...c) {
  24. expect(a).toBe("foo");
  25. expect(b).toBe(123);
  26. expect(c).toEqual([undefined, { foo: "bar" }]);
  27. }
  28. foo("foo", 123, undefined, { foo: "bar" });
  29. });
  30. test("basic arrow function rest parameters", () => {
  31. let foo = (...a) => {
  32. expect(a).toBeInstanceOf(Array);
  33. expect(a).toHaveLength(0);
  34. };
  35. foo();
  36. foo = (a, b, ...c) => {
  37. expect(a).toBe("foo");
  38. expect(b).toBe(123);
  39. expect(c).toEqual([undefined, { foo: "bar" }]);
  40. };
  41. foo("foo", 123, undefined, { foo: "bar" });
  42. });