array-spread.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. describe("errors", () => {
  2. test("cannot spread number in array", () => {
  3. expect(() => {
  4. [...1];
  5. }).toThrowWithMessage(TypeError, "1 is not iterable");
  6. });
  7. test("cannot spread object in array", () => {
  8. expect(() => {
  9. [...{}];
  10. }).toThrowWithMessage(TypeError, "[object Object] is not iterable");
  11. });
  12. });
  13. test("basic functionality", () => {
  14. expect([1, ...[2, 3], 4]).toEqual([1, 2, 3, 4]);
  15. let a = [2, 3];
  16. expect([1, ...a, 4]).toEqual([1, 2, 3, 4]);
  17. let obj = { a: [2, 3] };
  18. expect([1, ...obj.a, 4]).toEqual([1, 2, 3, 4]);
  19. expect([...[], ...[...[1, 2, 3]], 4]).toEqual([1, 2, 3, 4]);
  20. });
  21. test("allows assignment expressions", () => {
  22. expect("([ ...a = { hello: 'world' } ])").toEval();
  23. expect("([ ...a += 'hello' ])").toEval();
  24. expect("([ ...a -= 'hello' ])").toEval();
  25. expect("([ ...a **= 'hello' ])").toEval();
  26. expect("([ ...a *= 'hello' ])").toEval();
  27. expect("([ ...a /= 'hello' ])").toEval();
  28. expect("([ ...a %= 'hello' ])").toEval();
  29. expect("([ ...a <<= 'hello' ])").toEval();
  30. expect("([ ...a >>= 'hello' ])").toEval();
  31. expect("([ ...a >>>= 'hello' ])").toEval();
  32. expect("([ ...a &= 'hello' ])").toEval();
  33. expect("([ ...a ^= 'hello' ])").toEval();
  34. expect("([ ...a |= 'hello' ])").toEval();
  35. expect("([ ...a &&= 'hello' ])").toEval();
  36. expect("([ ...a ||= 'hello' ])").toEval();
  37. expect("([ ...a ??= 'hello' ])").toEval();
  38. expect("function* test() { return ([ ...yield a ]); }").toEval();
  39. });