1+ /**
2+ * Express Route Detection
3+ *
4+ * This approach checks routes without making HTTP requests by
5+ * directly inspecting the Express app's routing table.
6+ */
7+
18import request from "supertest"
2- import { jest } from "@jest/globals"
39import api_routes from "../routes/api-routes.js"
410import app from "../app.js"
5- import fs from 'fs'
11+ import fs from "fs"
612
7- let app_stack = app . _router . stack
13+ let app_stack = app . router . stack
814let api_stack = api_routes . stack
915
16+ /**
17+ * Check if a route exists in the Express app
18+ * @param {Array } stack - The router stack to search
19+ * @param {string } testPath - The path to test for
20+ * @returns {boolean } - True if the route exists
21+ */
22+ function routeExists ( stack , testPath ) {
23+ for ( const layer of stack ) {
24+ // Check if layer has matchers (Express 5)
25+ if ( layer . matchers && layer . matchers . length > 0 ) {
26+ const matcher = layer . matchers [ 0 ]
27+ const match = matcher ( testPath )
28+ if ( match && match . path ) return true
29+ }
30+ // Also check route.path directly if it exists
31+ if ( layer . route && layer . route . path ) {
32+ if ( layer . route . path === testPath || layer . route . path . includes ( testPath ) ) return true
33+ }
34+ }
35+ return false
36+ }
37+
1038describe ( 'Check to see that all expected top level route patterns exist.' , ( ) => {
1139
1240 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 )
41+ expect ( routeExists ( app_stack , '/v1' ) ) . toBe ( true )
2142 } )
2243
2344 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 )
45+ expect ( routeExists ( app_stack , '/client' ) ) . toBe ( true )
3246 } )
3347
3448 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 )
49+ expect ( routeExists ( api_stack , '/id' ) ) . toBe ( true )
4350 } )
4451
4552 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 )
53+ expect ( routeExists ( api_stack , '/since' ) ) . toBe ( true )
5454 } )
5555
5656 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 )
57+ expect ( routeExists ( api_stack , '/history' ) ) . toBe ( true )
6558 } )
6659
6760} )
6861
6962describe ( 'Check to see that all /v1/api/ route patterns exist.' , ( ) => {
7063
7164 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 )
65+ expect ( routeExists ( api_stack , '/api/query' ) ) . toBe ( true )
8266 } )
8367
8468 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 )
69+ expect ( routeExists ( api_stack , '/api/create' ) ) . toBe ( true )
9570 } )
9671
9772 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 )
73+ expect ( routeExists ( api_stack , '/api/bulkCreate' ) ) . toBe ( true )
10874 } )
10975
11076 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 )
77+ expect ( routeExists ( api_stack , '/api/update' ) ) . toBe ( true )
12178 } )
12279
12380 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 )
81+ expect ( routeExists ( api_stack , '/api/bulkUpdate' ) ) . toBe ( true )
82+ } )
83+
84+ it ( '/v1/api/overwrite -- mounted ' , ( ) => {
85+ expect ( routeExists ( api_stack , '/api/overwrite' ) ) . toBe ( true )
13486 } )
13587
13688 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 )
89+ expect ( routeExists ( api_stack , '/api/patch' ) ) . toBe ( true )
14790 } )
14891
14992 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 )
93+ expect ( routeExists ( api_stack , '/api/set' ) ) . toBe ( true )
16094 } )
16195
16296 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 )
97+ expect ( routeExists ( api_stack , '/api/unset' ) ) . toBe ( true )
17398 } )
17499
175100 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 )
101+ expect ( routeExists ( api_stack , '/api/delete' ) ) . toBe ( true )
186102 } )
187103
188104 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 )
105+ expect ( routeExists ( api_stack , '/api/release' ) ) . toBe ( true )
199106 } )
200107
201108} )
@@ -223,5 +130,8 @@ describe('Check to see that critical repo files are present', () => {
223130 expect ( fs . existsSync ( filePath + "CONTRIBUTING.md" ) ) . toBeTruthy ( )
224131 expect ( fs . existsSync ( filePath + "README.md" ) ) . toBeTruthy ( )
225132 expect ( fs . existsSync ( filePath + "LICENSE" ) ) . toBeTruthy ( )
133+ expect ( fs . existsSync ( filePath + ".gitignore" ) ) . toBeTruthy ( )
134+ expect ( fs . existsSync ( filePath + "jest.config.js" ) ) . toBeTruthy ( )
135+ expect ( fs . existsSync ( filePath + "package.json" ) ) . toBeTruthy ( )
226136 } )
227137} )
0 commit comments