AggregateError.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. describe("errors", () => {
  2. test("first argument must be coercible to object", () => {
  3. expect(() => {
  4. new AggregateError();
  5. }).toThrowWithMessage(TypeError, "ToObject on null or undefined");
  6. });
  7. test("@@iterator throws", () => {
  8. expect(() => {
  9. new AggregateError({
  10. [Symbol.iterator]() {
  11. throw Error("oops!");
  12. },
  13. });
  14. }).toThrowWithMessage(Error, "oops!");
  15. });
  16. });
  17. describe("normal behavior", () => {
  18. test("length is 2", () => {
  19. expect(AggregateError).toHaveLength(2);
  20. });
  21. test("name is AggregateError", () => {
  22. expect(AggregateError.name).toBe("AggregateError");
  23. });
  24. test("Prototype of the AggregateError constructor is the Error constructor", () => {
  25. expect(Object.getPrototypeOf(AggregateError)).toBe(Error);
  26. });
  27. test("Prototype of AggregateError.prototype is Error.prototype", () => {
  28. expect(Object.getPrototypeOf(AggregateError.prototype)).toBe(Error.prototype);
  29. });
  30. test("basic functionality", () => {
  31. expect(AggregateError([])).toBeInstanceOf(AggregateError);
  32. expect(new AggregateError([])).toBeInstanceOf(AggregateError);
  33. expect(new AggregateError([]).toString()).toBe("AggregateError");
  34. expect(new AggregateError([], "Foo").toString()).toBe("AggregateError: Foo");
  35. expect(new AggregateError([]).hasOwnProperty("errors")).toBeTrue();
  36. const errors = [1, 2, 3];
  37. expect(new AggregateError(errors).errors).toEqual(errors);
  38. expect(new AggregateError(errors).errors).not.toBe(errors);
  39. expect(
  40. new AggregateError({
  41. [Symbol.iterator]: (i = 0) => ({
  42. next() {
  43. if (i < 3) {
  44. i++;
  45. return { value: i };
  46. }
  47. return { value: null, done: true };
  48. },
  49. }),
  50. }).errors
  51. ).toEqual(errors);
  52. });
  53. test("supports options object with cause", () => {
  54. const cause = new Error();
  55. const error = new AggregateError([], "test", { cause });
  56. expect(error.hasOwnProperty("cause")).toBeTrue();
  57. expect(error.cause).toBe(cause);
  58. });
  59. });