Skip to content

Commit d652b28

Browse files
committed
tests: more e2e tests
1 parent e81d155 commit d652b28

File tree

12 files changed

+311
-52
lines changed

12 files changed

+311
-52
lines changed

e2e/openapi.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ paths:
9191
application/json:
9292
schema:
9393
$ref: '#/components/schemas/Enumerations'
94+
/responses/500:
95+
get:
96+
tags:
97+
- validation
98+
responses:
99+
500:
100+
description: Internal Server Error
94101
/responses/empty:
95102
get:
96103
tags:

e2e/src/express.entrypoint.ts

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {type NextFunction, type Request, type Response, Router} from "express"
33
import {bootstrap} from "./generated/server/express"
44
import {createRequestHeadersRouter} from "./routes/express/request-headers"
55
import {createValidationRouter} from "./routes/express/validation"
6+
import {createErrorResponse} from "./shared"
67

78
function createRouter() {
89
const router = Router()
@@ -32,23 +33,7 @@ export async function startExpressServer() {
3233
return next(err)
3334
}
3435

35-
const status = ExpressRuntimeError.isExpressError(err) ? 400 : 500
36-
const body =
37-
err instanceof Error
38-
? {
39-
message: err.message,
40-
phase: ExpressRuntimeError.isExpressError(err)
41-
? err.phase
42-
: undefined,
43-
cause:
44-
err.cause instanceof Error
45-
? {
46-
message: err.cause.message,
47-
}
48-
: undefined,
49-
}
50-
: {message: "non error thrown", value: err}
51-
36+
const {status, body} = createErrorResponse(err)
5237
res.status(status).json(body)
5338
})
5439

e2e/src/generated/client/axios/client.ts

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

e2e/src/generated/client/fetch/client.ts

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

e2e/src/generated/server/express/routes/validation.ts

Lines changed: 62 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

e2e/src/generated/server/koa/routes/validation.ts

Lines changed: 47 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

e2e/src/index.axios.spec.ts

Lines changed: 70 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type {Server} from "node:http"
22
import {beforeAll, describe, expect, it} from "@jest/globals"
3+
import type {AxiosError} from "axios"
34
import {ApiClient} from "./generated/client/axios/client"
45
import {E2ETestClientServers} from "./generated/client/fetch/client"
56
import {startServerFunctions} from "./index"
@@ -57,6 +58,25 @@ describe.each(startServerFunctions)(
5758
})
5859
})
5960

61+
describe("404 handling", () => {
62+
it("should handle 404s", async () => {
63+
const err = await client
64+
.getResponsesEmpty(undefined, {url: "/not-found"})
65+
.then(
66+
() => {
67+
throw new Error("expected request to fail")
68+
},
69+
(err: unknown) => err,
70+
)
71+
72+
expect(err).toMatchObject({
73+
message: "Request failed with status code 404",
74+
name: "AxiosError",
75+
status: 404,
76+
})
77+
})
78+
})
79+
6080
describe("GET /headers/undeclared", () => {
6181
it("provides the default headers", async () => {
6282
const {data} = await client.getHeadersUndeclared()
@@ -174,22 +194,15 @@ describe.each(startServerFunctions)(
174194
() => {
175195
throw new Error("expected request to fail")
176196
},
177-
(err) => err,
197+
(err: AxiosError) => err,
178198
)
179199

180-
expect(err).toMatchObject({
181-
message: "Request failed with status code 400",
182-
name: "AxiosError",
183-
status: 400,
184-
response: expect.objectContaining({
185-
data: {
186-
message: "Request validation failed parsing request header",
187-
phase: "request_validation",
188-
cause: {
189-
message: expect.stringMatching("Expected number, received nan"),
190-
},
191-
},
192-
}),
200+
expect(err.message).toMatch("Request failed with status code 400")
201+
expect(err.status).toBe(400)
202+
expect(err.response?.data).toMatchObject({
203+
message: "Request validation failed parsing request header",
204+
phase: "request_validation",
205+
cause: expect.stringContaining("Expected number, received nan"),
193206
})
194207
})
195208
})
@@ -208,6 +221,30 @@ describe.each(startServerFunctions)(
208221
})
209222
})
210223

224+
it("returns 400 when parameters fail validation", async () => {
225+
const err = await client
226+
.getValidationNumbersRandomNumber({
227+
// @ts-expect-error: testing runtime validation
228+
min: "one",
229+
// @ts-expect-error: testing runtime validation
230+
max: "ten",
231+
})
232+
.then(
233+
() => {
234+
throw new Error("expected request to fail")
235+
},
236+
(err: AxiosError) => err,
237+
)
238+
239+
expect(err.message).toMatch("Request failed with status code 400")
240+
expect(err.status).toBe(400)
241+
expect(err.response?.data).toMatchObject({
242+
message: "Request validation failed parsing querystring",
243+
phase: "request_validation",
244+
cause: expect.stringContaining("Expected number, received nan"),
245+
})
246+
})
247+
211248
it("handles a query param array of 1 element", async () => {
212249
const {data} = await client.getValidationNumbersRandomNumber({
213250
forbidden: [1],
@@ -252,5 +289,24 @@ describe.each(startServerFunctions)(
252289
expect(data).toEqual("")
253290
})
254291
})
292+
293+
describe("GET /responses/500", () => {
294+
it("returns response from error middleware", async () => {
295+
const err = await client.getResponses500().then(
296+
() => {
297+
throw new Error("expected request to fail")
298+
},
299+
(err: AxiosError) => err,
300+
)
301+
302+
expect(err.message).toMatch("Request failed with status code 500")
303+
expect(err.status).toBe(500)
304+
expect(err.response?.data).toMatchObject({
305+
message: "Request handler threw unhandled exception",
306+
phase: "request_handler",
307+
cause: expect.stringContaining("something went wrong"),
308+
})
309+
})
310+
})
255311
},
256312
)

0 commit comments

Comments
 (0)