@@ -4,198 +4,112 @@ import api_routes from "../routes/api-routes.js"
44import app from "../app.js"
55import fs from 'fs'
66
7- let app_stack = app . _router . stack
7+ /**
8+ * Express 5 Route Detection
9+ *
10+ * Key changes from Express 4 to Express 5:
11+ * 1. Router is accessed via app.router (not app._router)
12+ * 2. Layers use 'matchers' array instead of 'regexp' property
13+ * 3. Each matcher is a function that tests if a path matches
14+ * 4. Matchers return match info including path and params, or false
15+ *
16+ * This approach checks routes without making HTTP requests by
17+ * directly inspecting the Express app's routing table.
18+ */
19+ let app_stack = app . router . stack
820let api_stack = api_routes . stack
921
22+ /**
23+ * Helper function to check if a route exists in Express 5
24+ * In Express 5, layers use 'matchers' instead of 'regexp'
25+ * @param {Array } stack - The router stack to search
26+ * @param {string } testPath - The path to test for
27+ * @returns {boolean } - True if the route exists
28+ */
29+ function routeExists ( stack , testPath ) {
30+ for ( const layer of stack ) {
31+ // Check if layer has matchers (Express 5)
32+ if ( layer . matchers && layer . matchers . length > 0 ) {
33+ const matcher = layer . matchers [ 0 ]
34+ const match = matcher ( testPath )
35+ if ( match && match . path ) {
36+ return true
37+ }
38+ }
39+ // Also check route.path directly if it exists
40+ if ( layer . route && layer . route . path ) {
41+ if ( layer . route . path === testPath || layer . route . path . includes ( testPath ) ) {
42+ return true
43+ }
44+ }
45+ }
46+ return false
47+ }
48+
1049describe ( 'Check to see that all expected top level route patterns exist.' , ( ) => {
1150
1251 it ( '/v1 -- mounted ' , ( ) => {
13- let exists = false
14- for ( const middleware of app_stack ) {
15- if ( middleware . regexp && middleware . regexp . toString ( ) . includes ( "/v1" ) ) {
16- exists = true
17- break
18- }
19- }
20- expect ( exists ) . toBe ( true )
52+ expect ( routeExists ( app_stack , '/v1' ) ) . toBe ( true )
2153 } )
2254
2355 it ( '/client -- mounted ' , ( ) => {
24- let exists = false
25- for ( const middleware of app_stack ) {
26- if ( middleware . regexp && middleware . regexp . toString ( ) . includes ( "/client" ) ) {
27- exists = true
28- break
29- }
30- }
31- expect ( exists ) . toBe ( true )
56+ expect ( routeExists ( app_stack , '/client' ) ) . toBe ( true )
3257 } )
3358
3459 it ( '/v1/id/{_id} -- mounted' , ( ) => {
35- let exists = false
36- for ( const middleware of api_stack ) {
37- if ( middleware . regexp && middleware . regexp . toString ( ) . includes ( "/id" ) ) {
38- exists = true
39- break
40- }
41- }
42- expect ( exists ) . toBe ( true )
60+ expect ( routeExists ( api_stack , '/id' ) ) . toBe ( true )
4361 } )
4462
4563 it ( '/v1/since/{_id} -- mounted' , ( ) => {
46- let exists = false
47- for ( const middleware of api_stack ) {
48- if ( middleware . regexp && middleware . regexp . toString ( ) . includes ( "/since" ) ) {
49- exists = true
50- break
51- }
52- }
53- expect ( exists ) . toBe ( true )
64+ expect ( routeExists ( api_stack , '/since' ) ) . toBe ( true )
5465 } )
5566
5667 it ( '/v1/history/{_id} -- mounted' , ( ) => {
57- let exists = false
58- for ( const middleware of api_stack ) {
59- if ( middleware . regexp && middleware . regexp . toString ( ) . includes ( "/history" ) ) {
60- exists = true
61- break
62- }
63- }
64- expect ( exists ) . toBe ( true )
68+ expect ( routeExists ( api_stack , '/history' ) ) . toBe ( true )
6569 } )
6670
6771} )
6872
6973describe ( 'Check to see that all /v1/api/ route patterns exist.' , ( ) => {
7074
7175 it ( '/v1/api/query -- mounted ' , ( ) => {
72- let exists = false
73- for ( const middleware of api_stack ) {
74- if ( middleware . regexp
75- && middleware . regexp . toString ( ) . includes ( "/api" )
76- && middleware . regexp . toString ( ) . includes ( "/query" ) ) {
77- exists = true
78- break
79- }
80- }
81- expect ( exists ) . toBe ( true )
76+ expect ( routeExists ( api_stack , '/api/query' ) ) . toBe ( true )
8277 } )
8378
8479 it ( '/v1/api/create -- mounted ' , ( ) => {
85- let exists = false
86- for ( const middleware of api_stack ) {
87- if ( middleware . regexp
88- && middleware . regexp . toString ( ) . includes ( "/api" )
89- && middleware . regexp . toString ( ) . includes ( "/create" ) ) {
90- exists = true
91- break
92- }
93- }
94- expect ( exists ) . toBe ( true )
80+ expect ( routeExists ( api_stack , '/api/create' ) ) . toBe ( true )
9581 } )
9682
9783 it ( '/v1/api/bulkCreate -- mounted ' , ( ) => {
98- let exists = false
99- for ( const middleware of api_stack ) {
100- if ( middleware . regexp
101- && middleware . regexp . toString ( ) . includes ( "/api" )
102- && middleware . regexp . toString ( ) . includes ( "/bulkCreate" ) ) {
103- exists = true
104- break
105- }
106- }
107- expect ( exists ) . toBe ( true )
84+ expect ( routeExists ( api_stack , '/api/bulkCreate' ) ) . toBe ( true )
10885 } )
10986
11087 it ( '/v1/api/update -- mounted ' , ( ) => {
111- let exists = false
112- for ( const middleware of api_stack ) {
113- if ( middleware . regexp
114- && middleware . regexp . toString ( ) . includes ( "/api" )
115- && middleware . regexp . toString ( ) . includes ( "/update" ) ) {
116- exists = true
117- break
118- }
119- }
120- expect ( exists ) . toBe ( true )
88+ expect ( routeExists ( api_stack , '/api/update' ) ) . toBe ( true )
12189 } )
12290
12391 it ( '/v1/api/bulkUpdate -- mounted ' , ( ) => {
124- let exists = false
125- for ( const middleware of api_stack ) {
126- if ( middleware . regexp
127- && middleware . regexp . toString ( ) . includes ( "/api" )
128- && middleware . regexp . toString ( ) . includes ( "/bulkUpdate" ) ) {
129- exists = true
130- break
131- }
132- }
133- expect ( exists ) . toBe ( true )
92+ expect ( routeExists ( api_stack , '/api/bulkUpdate' ) ) . toBe ( true )
13493 } )
13594
13695 it ( '/v1/api/patch -- mounted ' , ( ) => {
137- let exists = false
138- for ( const middleware of api_stack ) {
139- if ( middleware . regexp
140- && middleware . regexp . toString ( ) . includes ( "/api" )
141- && middleware . regexp . toString ( ) . includes ( "/patch" ) ) {
142- exists = true
143- break
144- }
145- }
146- expect ( exists ) . toBe ( true )
96+ expect ( routeExists ( api_stack , '/api/patch' ) ) . toBe ( true )
14797 } )
14898
14999 it ( '/v1/api/set -- mounted ' , ( ) => {
150- let exists = false
151- for ( const middleware of api_stack ) {
152- if ( middleware . regexp
153- && middleware . regexp . toString ( ) . includes ( "/api" )
154- && middleware . regexp . toString ( ) . includes ( "/set" ) ) {
155- exists = true
156- break
157- }
158- }
159- expect ( exists ) . toBe ( true )
100+ expect ( routeExists ( api_stack , '/api/set' ) ) . toBe ( true )
160101 } )
161102
162103 it ( '/v1/api/unset -- mounted ' , ( ) => {
163- let exists = false
164- for ( const middleware of api_stack ) {
165- if ( middleware . regexp
166- && middleware . regexp . toString ( ) . includes ( "/api" )
167- && middleware . regexp . toString ( ) . includes ( "/unset" ) ) {
168- exists = true
169- break
170- }
171- }
172- expect ( exists ) . toBe ( true )
104+ expect ( routeExists ( api_stack , '/api/unset' ) ) . toBe ( true )
173105 } )
174106
175107 it ( '/v1/api/delete/{id} -- mounted ' , ( ) => {
176- let exists = false
177- for ( const middleware of api_stack ) {
178- if ( middleware . regexp
179- && middleware . regexp . toString ( ) . includes ( "/api" )
180- && middleware . regexp . toString ( ) . includes ( "/delete" ) ) {
181- exists = true
182- break
183- }
184- }
185- expect ( exists ) . toBe ( true )
108+ expect ( routeExists ( api_stack , '/api/delete' ) ) . toBe ( true )
186109 } )
187110
188111 it ( '/v1/api/release/{id} -- mounted ' , ( ) => {
189- let exists = false
190- for ( const middleware of api_stack ) {
191- if ( middleware . regexp
192- && middleware . regexp . toString ( ) . includes ( "/api" )
193- && middleware . regexp . toString ( ) . includes ( "/release" ) ) {
194- exists = true
195- break
196- }
197- }
198- expect ( exists ) . toBe ( true )
112+ expect ( routeExists ( api_stack , '/api/release' ) ) . toBe ( true )
199113 } )
200114
201115} )
0 commit comments