Skip to content
Source
Sign inSign up
samwillis/neon-realtime-electric
Open History to see the last commit

error.test.ts

128 lines4.4 KB

1import { describe, it, expect, vi } from 'vitest'
2import { FetchError } from '../src/error'
3​
4describe(`FetchError`, () => {
5 it(`should create a FetchError with the correct properties`, () => {
6 const status = 404
7 const text = `Not Found`
8 const json = undefined
9 const headers = { 'content-type': `text/plain` }
10 const url = `https://example.com/notfound`
11​
12 const error = new FetchError(status, text, json, headers, url)
13​
14 expect(error).toBeInstanceOf(Error)
15 expect(error.name).toBe(`FetchError`)
16 expect(error.status).toBe(status)
17 expect(error.text).toBe(text)
18 expect(error.json).toBe(json)
19 expect(error.headers).toEqual(headers)
20 expect(error.url).toBe(url)
21 expect(error.message).toBe(
22 `HTTP Error 404 at https://example.com/notfound: Not Found`
23 )
24 })
25​
26 it(`should create a FetchError with a JSON response and use the JSON in the message`, () => {
27 const status = 500
28 const text = undefined
29 const json = { error: `Internal Server Error` }
30 const headers = { 'content-type': `application/json` }
31 const url = `https://example.com/servererror`
32​
33 const error = new FetchError(status, text, json, headers, url)
34​
35 expect(error.status).toBe(status)
36 expect(error.text).toBeUndefined()
37 expect(error.json).toEqual(json)
38 expect(error.headers).toEqual(headers)
39 expect(error.message).toBe(
40 `HTTP Error 500 at https://example.com/servererror: {"error":"Internal Server Error"}`
41 )
42 })
43​
44 it(`should create a FetchError with a custom message if provided`, () => {
45 const status = 403
46 const text = `Forbidden`
47 const json = undefined
48 const headers = { 'content-type': `text/plain` }
49 const url = `https://example.com/forbidden`
50 const customMessage = `Custom Error Message`
51​
52 const error = new FetchError(
53 status,
54 text,
55 json,
56 headers,
57 url,
58 customMessage
59 )
60​
61 expect(error.message).toBe(customMessage)
62 })
63​
64 describe(`fromResponse`, () => {
65 it(`should create a FetchError from a text-based response`, async () => {
66 const mockResponse = {
67 status: 404,
68 headers: new Headers({ 'content-type': `text/plain` }),
69 text: vi.fn().mockResolvedValue(`Not Found`),
70 } as unknown as Response
71​
72 const url = `https://example.com/notfound`
73 const error = await FetchError.fromResponse(mockResponse, url)
74​
75 expect(mockResponse.text).toHaveBeenCalled()
76 expect(error).toBeInstanceOf(FetchError)
77 expect(error.status).toBe(404)
78 expect(error.text).toBe(`Not Found`)
79 expect(error.json).toBeUndefined()
80 expect(error.headers).toEqual({ 'content-type': `text/plain` })
81 expect(error.message).toBe(
82 `HTTP Error 404 at https://example.com/notfound: Not Found`
83 )
84 })
85​
86 it(`should create a FetchError from a JSON-based response`, async () => {
87 const mockResponse = {
88 status: 500,
89 headers: new Headers({ 'content-type': `application/json` }),
90 json: vi.fn().mockResolvedValue({ error: `Internal Server Error` }),
91 } as unknown as Response
92​
93 const url = `https://example.com/servererror`
94 const error = await FetchError.fromResponse(mockResponse, url)
95​
96 expect(mockResponse.json).toHaveBeenCalled()
97 expect(error).toBeInstanceOf(FetchError)
98 expect(error.status).toBe(500)
99 expect(error.text).toBeUndefined()
100 expect(error.json).toEqual({ error: `Internal Server Error` })
101 expect(error.headers).toEqual({ 'content-type': `application/json` })
102 expect(error.message).toBe(
103 `HTTP Error 500 at https://example.com/servererror: {"error":"Internal Server Error"}`
104 )
105 })
106​
107 it(`should handle content-type not set in response headers`, async () => {
108 const mockResponse = {
109 status: 500,
110 headers: new Headers(),
111 text: vi.fn().mockResolvedValue(`Server error with no content-type`),
112 } as unknown as Response
113​
114 const url = `https://example.com/no-content-type`
115 const error = await FetchError.fromResponse(mockResponse, url)
116​
117 expect(mockResponse.text).toHaveBeenCalled()
118 expect(error).toBeInstanceOf(FetchError)
119 expect(error.status).toBe(500)
120 expect(error.text).toBe(`Server error with no content-type`)
121 expect(error.json).toBeUndefined()
122 expect(error.headers).toEqual({})
123 expect(error.message).toBe(
124 `HTTP Error 500 at https://example.com/no-content-type: Server error with no content-type`
125 )
126 })
127 })
128})