Error.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. describe("normal behavior", () => {
  2. test("length is 1", () => {
  3. expect(Error).toHaveLength(1);
  4. expect(EvalError).toHaveLength(1);
  5. expect(RangeError).toHaveLength(1);
  6. expect(ReferenceError).toHaveLength(1);
  7. expect(SyntaxError).toHaveLength(1);
  8. expect(TypeError).toHaveLength(1);
  9. });
  10. test("name matches constructor name", () => {
  11. expect(Error.name).toBe("Error");
  12. expect(EvalError.name).toBe("EvalError");
  13. expect(RangeError.name).toBe("RangeError");
  14. expect(ReferenceError.name).toBe("ReferenceError");
  15. expect(SyntaxError.name).toBe("SyntaxError");
  16. expect(TypeError.name).toBe("TypeError");
  17. });
  18. test("basic functionality", () => {
  19. expect(Error()).toBeInstanceOf(Error);
  20. expect(new Error()).toBeInstanceOf(Error);
  21. expect(EvalError()).toBeInstanceOf(EvalError);
  22. expect(new EvalError()).toBeInstanceOf(EvalError);
  23. expect(RangeError()).toBeInstanceOf(RangeError);
  24. expect(new RangeError()).toBeInstanceOf(RangeError);
  25. expect(ReferenceError()).toBeInstanceOf(ReferenceError);
  26. expect(new ReferenceError()).toBeInstanceOf(ReferenceError);
  27. expect(SyntaxError()).toBeInstanceOf(SyntaxError);
  28. expect(new SyntaxError()).toBeInstanceOf(SyntaxError);
  29. expect(TypeError()).toBeInstanceOf(TypeError);
  30. expect(new TypeError()).toBeInstanceOf(TypeError);
  31. });
  32. test("supports options object with cause", () => {
  33. const errors = [Error, EvalError, RangeError, ReferenceError, SyntaxError, TypeError];
  34. const cause = new Error();
  35. errors.forEach(T => {
  36. const error = new T("test", { cause });
  37. expect(error.hasOwnProperty("cause")).toBeTrue();
  38. expect(error.cause).toBe(cause);
  39. });
  40. });
  41. test("supports options object with cause (chained)", () => {
  42. let error;
  43. try {
  44. try {
  45. throw new Error("foo");
  46. } catch (e) {
  47. throw new Error("bar", { cause: e });
  48. }
  49. } catch (e) {
  50. error = new Error("baz", { cause: e });
  51. }
  52. expect(error.message).toBe("baz");
  53. expect(error.cause.message).toBe("bar");
  54. expect(error.cause.cause.message).toBe("foo");
  55. expect(error.cause.cause.cause).toBe(undefined);
  56. });
  57. });