eval-basic.js 822 B

12345678910111213141516171819202122232425262728293031
  1. test("basic eval() functionality", () => {
  2. expect(eval("1 + 2")).toBe(3);
  3. function foo(a) {
  4. var x = 5;
  5. eval("x += a");
  6. return x;
  7. }
  8. expect(foo(7)).toBe(12);
  9. });
  10. test("returns value of last value-producing statement", () => {
  11. // See https://tc39.es/ecma262/#sec-block-runtime-semantics-evaluation
  12. expect(eval("1;;;;;")).toBe(1);
  13. expect(eval("1;{}")).toBe(1);
  14. expect(eval("1;var a;")).toBe(1);
  15. });
  16. test("syntax error", () => {
  17. expect(() => {
  18. eval("{");
  19. }).toThrowWithMessage(
  20. SyntaxError,
  21. "Unexpected token Eof. Expected CurlyClose (line: 1, column: 2)"
  22. );
  23. });
  24. test("returns 1st argument unless 1st argument is a string", () => {
  25. var stringObject = new String("1 + 2");
  26. expect(eval(stringObject)).toBe(stringObject);
  27. });