update-expressions-basic.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. describe("correct behavior", () => {
  2. test("basic functionality", () => {
  3. let n = 0;
  4. expect(++n).toBe(1);
  5. expect(n).toBe(1);
  6. n = 0;
  7. expect(n++).toBe(0);
  8. expect(n).toBe(1);
  9. n = 0;
  10. expect(--n).toBe(-1);
  11. expect(n).toBe(-1);
  12. n = 0;
  13. expect(n--).toBe(0);
  14. expect(n).toBe(-1);
  15. let a = [];
  16. expect(a++).toBe(0);
  17. expect(a).toBe(1);
  18. let b = true;
  19. expect(b--).toBe(1);
  20. expect(b).toBe(0);
  21. });
  22. test("updates that produce NaN", () => {
  23. let s = "foo";
  24. expect(++s).toBeNaN();
  25. expect(s).toBeNaN();
  26. s = "foo";
  27. expect(s++).toBeNaN();
  28. expect(s).toBeNaN();
  29. s = "foo";
  30. expect(--s).toBeNaN();
  31. expect(s).toBeNaN();
  32. s = "foo";
  33. expect(s--).toBeNaN();
  34. expect(s).toBeNaN();
  35. });
  36. });
  37. describe("errors", () => {
  38. test("update expression throws reference error", () => {
  39. expect(() => {
  40. ++x;
  41. }).toThrowWithMessage(ReferenceError, "'x' is not defined");
  42. });
  43. });