Skip to content

Commit 2b3da79

Browse files
committed
Thanks Claude
1 parent 113101f commit 2b3da79

1 file changed

Lines changed: 39 additions & 70 deletions

File tree

routes/__tests__/mount.test.js

Lines changed: 39 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,44 @@ afterEach(() => {
1616
*/
1717
})
1818

19+
/**
20+
* Helper function to check if a route is registered in Express 5.
21+
* In Express 5, app._router was replaced with app.router.
22+
* Routes are checked by testing the matcher functions on each layer.
23+
*
24+
* @param {Array<string>} routes - Array of route paths to check
25+
* @returns {boolean} - True if all routes are found
26+
*/
27+
function checkRoutesExist(routes) {
28+
const stack = app.router.stack
29+
const foundRoutes = new Set()
30+
31+
for (const layer of stack) {
32+
if (layer.matchers && layer.matchers[0]) {
33+
const matcher = layer.matchers[0]
34+
// Test each route against this layer's matcher
35+
for (const route of routes) {
36+
const result = matcher(route)
37+
if (result) {
38+
foundRoutes.add(route)
39+
}
40+
}
41+
}
42+
}
43+
44+
// Check if all expected routes were found
45+
return routes.every(route => foundRoutes.has(route))
46+
}
47+
1948
/**
2049
* This test suite uses the built app.js app and checks that the expected create endpoints are registered.
2150
* - /create
2251
* - /app/create
2352
*/
2453
describe("Check that the expected TinyNode create route patterns are registered.", () => {
2554
it("'/app/create' and '/create' are registered routes in the app. __exists __core", () => {
26-
let exists = false
27-
let count = 0
28-
const stack = app._router.stack
29-
for (const middleware of stack) {
30-
if (middleware.regexp && middleware.regexp.toString().includes("/app/create")) {
31-
count++
32-
} else if (middleware.regexp && middleware.regexp.toString().includes("/create")) {
33-
count++
34-
}
35-
if (count === 2) {
36-
exists = true
37-
break
38-
}
39-
}
55+
const routes = ["/create", "/app/create"]
56+
const exists = checkRoutesExist(routes)
4057
expect(exists).toBe(true)
4158
})
4259
})
@@ -48,20 +65,8 @@ describe("Check that the expected TinyNode create route patterns are registered.
4865
*/
4966
describe("Check that the expected TinyNode query route patterns are registered.", () => {
5067
it("'/app/query' and '/query' are registered routes in the app. __exists __core", () => {
51-
let exists = false
52-
let count = 0
53-
const stack = app._router.stack
54-
for (const middleware of stack) {
55-
if (middleware.regexp && middleware.regexp.toString().includes("/app/query")) {
56-
count++
57-
} else if (middleware.regexp && middleware.regexp.toString().includes("/query")) {
58-
count++
59-
}
60-
if (count === 2) {
61-
exists = true
62-
break
63-
}
64-
}
68+
const routes = ["/query", "/app/query"]
69+
const exists = checkRoutesExist(routes)
6570
expect(exists).toBe(true)
6671
})
6772
})
@@ -73,20 +78,8 @@ describe("Check that the expected TinyNode query route patterns are registered."
7378
*/
7479
describe("Check that the expected TinyNode update route patterns are registered.", () => {
7580
it("'/app/update' and '/update' are registered routes in the app. __exists __core", () => {
76-
let exists = false
77-
let count = 0
78-
const stack = app._router.stack
79-
for (const middleware of stack) {
80-
if (middleware.regexp && middleware.regexp.toString().includes("/app/update")) {
81-
count++
82-
} else if (middleware.regexp && middleware.regexp.toString().includes("/update")) {
83-
count++
84-
}
85-
if (count === 2) {
86-
exists = true
87-
break
88-
}
89-
}
81+
const routes = ["/update", "/app/update"]
82+
const exists = checkRoutesExist(routes)
9083
expect(exists).toBe(true)
9184
})
9285
})
@@ -99,20 +92,8 @@ describe("Check that the expected TinyNode update route patterns are registered.
9992
*/
10093
describe("Check that the expected TinyNode overwrite route patterns are registered.", () => {
10194
it("'/app/overwrite' and '/overwrite' are registered routes in the app. __exists __core", () => {
102-
let exists = false
103-
let count = 0
104-
const stack = app._router.stack
105-
for (const middleware of stack) {
106-
if (middleware.regexp && middleware.regexp.toString().includes("/app/overwrite")) {
107-
count++
108-
} else if (middleware.regexp && middleware.regexp.toString().includes("/overwrite")) {
109-
count++
110-
}
111-
if (count === 2) {
112-
exists = true
113-
break
114-
}
115-
}
95+
const routes = ["/overwrite", "/app/overwrite"]
96+
const exists = checkRoutesExist(routes)
11697
expect(exists).toBe(true)
11798
})
11899
})
@@ -124,20 +105,8 @@ describe("Check that the expected TinyNode overwrite route patterns are register
124105
*/
125106
describe("Combined unit tests for the '/delete' route.", () => {
126107
it("'/app/delete' and '/delete' are registered routes in the app. __exists __core", () => {
127-
let exists = false
128-
let count = 0
129-
const stack = app._router.stack
130-
for (const middleware of stack) {
131-
if (middleware.regexp && middleware.regexp.toString().includes("/app/delete")) {
132-
count++
133-
} else if (middleware.regexp && middleware.regexp.toString().includes("/delete")) {
134-
count++
135-
}
136-
if (count === 2) {
137-
exists = true
138-
break
139-
}
140-
}
108+
const routes = ["/delete", "/app/delete"]
109+
const exists = checkRoutesExist(routes)
141110
expect(exists).toBe(true)
142111
})
143112
})

0 commit comments

Comments
 (0)