|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { isAbortError } from "./isAbortError"; |
| 3 | + |
| 4 | +describe("isAbortError", () => { |
| 5 | + // -- Positive cases -- |
| 6 | + |
| 7 | + it('returns true for "cancel" string', () => { |
| 8 | + expect(isAbortError("cancel")).toBe(true); |
| 9 | + }); |
| 10 | + |
| 11 | + it("returns true for Error with name AbortError", () => { |
| 12 | + const error = Object.assign(new Error("aborted"), { name: "AbortError" }); |
| 13 | + expect(isAbortError(error)).toBe(true); |
| 14 | + }); |
| 15 | + |
| 16 | + it("returns true for Error with ABORT_ERR code", () => { |
| 17 | + const error = Object.assign(new Error("aborted"), { code: "ABORT_ERR" }); |
| 18 | + expect(isAbortError(error)).toBe(true); |
| 19 | + }); |
| 20 | + |
| 21 | + it("returns true for Error with both AbortError name and ABORT_ERR code", () => { |
| 22 | + const error = Object.assign(new Error("aborted"), { |
| 23 | + name: "AbortError", |
| 24 | + code: "ABORT_ERR", |
| 25 | + }); |
| 26 | + expect(isAbortError(error)).toBe(true); |
| 27 | + }); |
| 28 | + |
| 29 | + it("returns true for custom Error subclass with AbortError name", () => { |
| 30 | + class CustomAbortError extends Error { |
| 31 | + constructor() { |
| 32 | + super("aborted"); |
| 33 | + this.name = "AbortError"; |
| 34 | + } |
| 35 | + } |
| 36 | + expect(isAbortError(new CustomAbortError())).toBe(true); |
| 37 | + }); |
| 38 | + |
| 39 | + const hasDOMException = typeof DOMException !== "undefined"; |
| 40 | + |
| 41 | + it.skipIf(!hasDOMException)( |
| 42 | + "returns true for DOMException with AbortError name", |
| 43 | + () => { |
| 44 | + const error = new DOMException("Operation aborted", "AbortError"); |
| 45 | + expect(isAbortError(error)).toBe(true); |
| 46 | + }, |
| 47 | + ); |
| 48 | + |
| 49 | + it("returns true for plain object with name AbortError", () => { |
| 50 | + expect(isAbortError({ name: "AbortError" })).toBe(true); |
| 51 | + }); |
| 52 | + |
| 53 | + it("returns true for frozen object with name AbortError", () => { |
| 54 | + expect(isAbortError(Object.freeze({ name: "AbortError" }))).toBe(true); |
| 55 | + }); |
| 56 | + |
| 57 | + // -- Negative cases: exact match, not substring -- |
| 58 | + |
| 59 | + it("returns false for Error with name containing AbortError as substring", () => { |
| 60 | + const error = Object.assign(new Error("x"), { name: "NetworkAbortError" }); |
| 61 | + expect(isAbortError(error)).toBe(false); |
| 62 | + }); |
| 63 | + |
| 64 | + it("returns false for Error with partial name Abort", () => { |
| 65 | + const error = Object.assign(new Error("x"), { name: "Abort" }); |
| 66 | + expect(isAbortError(error)).toBe(false); |
| 67 | + }); |
| 68 | + |
| 69 | + it("returns false for plain object with AbortError substring in name", () => { |
| 70 | + expect(isAbortError({ name: "AbortErrorWrapper" })).toBe(false); |
| 71 | + }); |
| 72 | + |
| 73 | + // -- Negative cases: cancel is case-sensitive -- |
| 74 | + |
| 75 | + it('returns false for "Cancel" (capital C)', () => { |
| 76 | + expect(isAbortError("Cancel")).toBe(false); |
| 77 | + }); |
| 78 | + |
| 79 | + it('returns false for "CANCEL" (all caps)', () => { |
| 80 | + expect(isAbortError("CANCEL")).toBe(false); |
| 81 | + }); |
| 82 | + |
| 83 | + // -- Negative cases: plain object edge cases -- |
| 84 | + |
| 85 | + it("returns false for plain object with ABORT_ERR code but no AbortError name", () => { |
| 86 | + // code check only applies to instanceof Error, not plain objects |
| 87 | + expect(isAbortError({ code: "ABORT_ERR" })).toBe(false); |
| 88 | + }); |
| 89 | + |
| 90 | + it("returns false for plain object with numeric name", () => { |
| 91 | + expect(isAbortError({ name: 123 })).toBe(false); |
| 92 | + }); |
| 93 | + |
| 94 | + it("returns false for plain object with null name", () => { |
| 95 | + expect(isAbortError({ name: null })).toBe(false); |
| 96 | + }); |
| 97 | + |
| 98 | + it.skipIf(!hasDOMException)( |
| 99 | + "returns false for DOMException with non-AbortError name", |
| 100 | + () => { |
| 101 | + const error = new DOMException("fail", "NetworkError"); |
| 102 | + expect(isAbortError(error)).toBe(false); |
| 103 | + }, |
| 104 | + ); |
| 105 | + |
| 106 | + // -- Negative cases: primitives and nullish -- |
| 107 | + |
| 108 | + it("returns false for null", () => { |
| 109 | + expect(isAbortError(null)).toBe(false); |
| 110 | + }); |
| 111 | + |
| 112 | + it("returns false for undefined", () => { |
| 113 | + expect(isAbortError(undefined)).toBe(false); |
| 114 | + }); |
| 115 | + |
| 116 | + it("returns false for empty string", () => { |
| 117 | + expect(isAbortError("")).toBe(false); |
| 118 | + }); |
| 119 | + |
| 120 | + it("returns false for number", () => { |
| 121 | + expect(isAbortError(42)).toBe(false); |
| 122 | + }); |
| 123 | + |
| 124 | + it("returns false for boolean", () => { |
| 125 | + expect(isAbortError(false)).toBe(false); |
| 126 | + expect(isAbortError(true)).toBe(false); |
| 127 | + }); |
| 128 | + |
| 129 | + // -- Negative cases: regular errors -- |
| 130 | + |
| 131 | + it("returns false for regular Error", () => { |
| 132 | + expect(isAbortError(new Error("network error"))).toBe(false); |
| 133 | + }); |
| 134 | + |
| 135 | + it("returns false for TypeError", () => { |
| 136 | + expect(isAbortError(new TypeError("bad type"))).toBe(false); |
| 137 | + }); |
| 138 | + |
| 139 | + it("returns false for plain object without name", () => { |
| 140 | + expect(isAbortError({ message: "error" })).toBe(false); |
| 141 | + }); |
| 142 | +}); |
0 commit comments