@@ -41,13 +41,26 @@ describe("metaData", () => {
4141 openapi . mockImplementation ( ( ) => {
4242 throw new Error ( "OpenApi error" ) ;
4343 } ) ;
44- try {
45- await getMetadata ( url ) ;
46- } catch ( error ) {
47- expect ( error . message ) . toBe ( "OpenApi error" ) ;
48- }
44+
45+ await expect ( getMetadata ( url ) ) . rejects . toThrow ( "OpenApi error" ) ;
4946 } ) ;
5047
48+ test ( "getMetadata should raise error when get openapi failed" , async ( ) => {
49+ const url = "/ord/v1/sap.test.cdsrc.sample:apiResource:AdminService:v1/AdminService.edmx" ;
50+ jest . spyOn ( cds , "compile" ) . mockImplementation ( ( ) => {
51+ return {
52+ to : {
53+ edmx : ( ) => {
54+ throw new Error ( "EDMX error" ) ;
55+ }
56+ }
57+ }
58+ } ) ;
59+
60+ await expect ( getMetadata ( url ) ) . rejects . toThrow ( "EDMX error" ) ;
61+ } ) ;
62+
63+
5164 test ( "getMetadata should return asyncapi content for a given URL" , async ( ) => {
5265 const url = "/ord/v1/sap.test.cdsrc.sample:eventResource:AdminService:v1/AdminService.asyncapi2.json" ;
5366 const expectedResponse = {
@@ -58,48 +71,38 @@ describe("metaData", () => {
5871 return "Asyncapi content" ;
5972 } ) ;
6073
61- const result = await getMetadata ( url ) ;
62-
63- expect ( result ) . toEqual ( expectedResponse ) ;
74+ await expect ( getMetadata ( url ) ) . resolves . toEqual ( expectedResponse ) ;
6475 } ) ;
6576
6677 test ( "getMetadata should raise error when get asyncapi failed" , async ( ) => {
6778 const url = "/ord/v1/sap.test.cdsrc.sample:eventResource:AdminService:v1/AdminService.asyncapi2.json" ;
6879 asyncapi . mockImplementation ( ( ) => {
6980 throw new Error ( "AsyncApi error" ) ;
7081 } ) ;
71- try {
72- await getMetadata ( url ) ;
73- } catch ( error ) {
74- expect ( error . message ) . toBe ( "AsyncApi error" ) ;
75- }
82+
83+ await expect ( getMetadata ( url ) ) . rejects . toThrow ( "AsyncApi error" ) ;
7684 } ) ;
7785
7886 test ( "getMetadata should return csn content for a given URL" , async ( ) => {
7987 const url = "/ord/v1/sap.test.cdsrc.sample:apiResource:AdminService:v1/AdminService.csn.json" ;
8088 const expectedResponse = {
81- contentType : "application/json" ,
8289 response : "Csn content" ,
90+ contentType : "application/json" ,
8391 } ;
8492 jest . spyOn ( cdsc . for , "effective" ) . mockImplementation ( ( ) => {
8593 return expectedResponse . response ;
8694 } ) ;
8795
88- const result = await getMetadata ( url , "Csn content" ) ;
89-
90- expect ( result ) . toEqual ( expectedResponse ) ;
96+ await expect ( getMetadata ( url , "Csn content" ) ) . resolves . toEqual ( expectedResponse ) ;
9197 } ) ;
9298
9399 test ( "getMetadata should raise error when get csn failed" , async ( ) => {
94100 const url = "/ord/v1/sap.test.cdsrc.sample:apiResource:AdminService:v1/AdminService.csn.json" ;
95101 jest . spyOn ( cdsc . for , "effective" ) . mockImplementation ( ( ) => {
96102 throw new Error ( "Csn error" ) ;
97103 } ) ;
98- try {
99- await getMetadata ( url , "" ) ;
100- } catch ( error ) {
101- expect ( error . message ) . toContain ( "Csn error" ) ;
102- }
104+
105+ await expect ( getMetadata ( url , "" ) ) . rejects . toThrow ( "Csn error" ) ;
103106 } ) ;
104107
105108 test ( "getMetadata should return edmx content for a given URL" , async ( ) => {
@@ -116,21 +119,16 @@ describe("metaData", () => {
116119 } ;
117120 } ) ;
118121
119- const result = await getMetadata ( url ) ;
120-
121- expect ( result ) . toEqual ( expectedResponse ) ;
122+ await expect ( getMetadata ( url ) ) . resolves . toEqual ( expectedResponse ) ;
122123 } ) ;
123124
124- test ( "getMetadata should raise error when get edmx failed" , async ( ) => {
125- const url = "/ord/v1/sap.test.cdsrc.sample:apiResource:CinemaService:v1/CinemaService.edmx " ;
125+ test ( "getMetadata should raise error when get OpenAPI failed" , async ( ) => {
126+ const url = "/ord/v1/sap.test.cdsrc.sample:apiResource:CinemaService:v1/CinemaService.oas3.json " ;
126127 openapi . mockImplementation ( ( ) => {
127- throw new Error ( "Edmx error" ) ;
128+ throw new Error ( "OpenAPI error" ) ;
128129 } ) ;
129- try {
130- await getMetadata ( url ) ;
131- } catch ( error ) {
132- expect ( error . message ) . toBe ( "Edmx error" ) ;
133- }
130+
131+ await expect ( getMetadata ( url ) ) . rejects . toThrow ( "OpenAPI error" ) ;
134132 } ) ;
135133
136134 test ( "getMetadata should return mcp content for a given URL" , async ( ) => {
@@ -153,9 +151,7 @@ describe("metaData", () => {
153151 } ,
154152 } ) ) ;
155153
156- const result = await getMetadata ( url ) ;
157-
158- expect ( result ) . toEqual ( expectedResponse ) ;
154+ await expect ( getMetadata ( url ) ) . resolves . toEqual ( expectedResponse ) ;
159155 } ) ;
160156
161157 test ( "getMetadata should return GraphQL SDL content with text/plain content type" , async ( ) => {
@@ -178,11 +174,7 @@ describe("metaData", () => {
178174 throw new Error ( "GraphQL schema error" ) ;
179175 } ) ;
180176
181- try {
182- await getMetadata ( url ) ;
183- } catch ( error ) {
184- expect ( error . message ) . toBe ( "GraphQL schema error" ) ;
185- }
177+ await expect ( getMetadata ( url ) ) . rejects . toThrow ( "GraphQL schema error" ) ;
186178 } ) ;
187179
188180 test ( "getMetadata should raise error when get mcp failed" , async ( ) => {
@@ -194,50 +186,34 @@ describe("metaData", () => {
194186 } ,
195187 } ,
196188 } ) ) ;
197- try {
198- await getMetadata ( url ) ;
199- } catch ( error ) {
200- expect ( error . message ) . toBe ( "MCP error" ) ;
201- }
202- } ) ;
203189
204- test ( "getMetadata should handle invalid URL format" , async ( ) => {
205- const url = "/invalid/url/format" ;
206- try {
207- await getMetadata ( url ) ;
208- } catch ( error ) {
209- expect ( error . message ) . toContain ( "Invalid URL format" ) ;
210- }
190+ await expect ( getMetadata ( url ) ) . rejects . toThrow ( "MCP error" ) ;
211191 } ) ;
212192
213193 test ( "getMetadata should handle missing service name in URL" , async ( ) => {
214194 const url = "/ord/v1/sap.test.cdsrc.sample:apiResource::v1/Service.oas3.json" ;
215195 openapi . mockImplementation ( ( ) => "Mock content" ) ;
216196
217- const result = await getMetadata ( url ) ;
218- expect ( result ) . toEqual ( {
219- contentType : "application/json" ,
197+ await expect ( getMetadata ( url ) ) . resolves . toEqual ( {
220198 response : "Mock content" ,
199+ contentType : "application/json" ,
221200 } ) ;
222201 } ) ;
223202
224203 test ( "getMetadata should handle unknown file extension" , async ( ) => {
225204 const url = "/ord/v1/sap.test.cdsrc.sample:apiResource:TestService:v1/TestService.unknown" ;
226- try {
227- await getMetadata ( url ) ;
228- } catch ( error ) {
229- expect ( error . message ) . toContain ( "Unsupported format" ) ;
230- }
205+
206+ await expect ( getMetadata ( url ) ) . rejects . toThrow ( "Unsupported format" ) ;
231207 } ) ;
232208
233209 test ( "getMetadata should return correct content type for asyncapi" , async ( ) => {
234210 const url = "/ord/v1/sap.test.cdsrc.sample:eventResource:TestService:v1/TestService.asyncapi2.json" ;
235211 asyncapi . mockImplementation ( ( ) => ( { test : "asyncapi content" } ) ) ;
236212
237- const result = await getMetadata ( url ) ;
238-
239- expect ( result . contentType ) . toBe ( "application/json" ) ;
240- expect ( result . response ) . toEqual ( { test : "asyncapi content" } ) ;
213+ await expect ( getMetadata ( url ) ) . resolves . toEqual ( {
214+ contentType : "application/json" ,
215+ response : { test : "asyncapi content" } ,
216+ } ) ;
241217 } ) ;
242218
243219 test ( "getMetadata should handle edmx compilation with correct content type" , async ( ) => {
@@ -248,31 +224,31 @@ describe("metaData", () => {
248224 } ,
249225 } ) ) ;
250226
251- const result = await getMetadata ( url ) ;
252-
253- expect ( result . contentType ) . toBe ( "application/xml" ) ;
254- expect ( result . response ) . toBe ( "<edmx>content</edmx>" ) ;
227+ await expect ( getMetadata ( url ) ) . resolves . toEqual ( {
228+ contentType : "application/xml" ,
229+ response : "<edmx>content</edmx>" ,
230+ } ) ;
255231 } ) ;
256232
257233 test ( "getMetadata should handle complex service names with dots" , async ( ) => {
258234 const url =
259235 "/ord/v1/sap.test.cdsrc.sample:apiResource:my.complex.ServiceName:v1/my.complex.ServiceName.oas3.json" ;
260236 openapi . mockImplementation ( ( ) => "Complex service content" ) ;
261237
262- const result = await getMetadata ( url ) ;
263-
264- expect ( result . contentType ) . toBe ( "application/json" ) ;
265- expect ( result . response ) . toBe ( "Complex service content" ) ;
238+ await expect ( getMetadata ( url ) ) . resolves . toEqual ( {
239+ contentType : "application/json" ,
240+ response : "Complex service content" ,
241+ } ) ;
266242 } ) ;
267243
268244 test ( "getMetadata should handle version variations in URL" , async ( ) => {
269245 const url = "/ord/v1/sap.test.cdsrc.sample:apiResource:TestService:v2/TestService.csn.json" ;
270246 jest . spyOn ( cdsc . for , "effective" ) . mockImplementation ( ( ) => "Version 2 CSN" ) ;
271247
272- const result = await getMetadata ( url , "input csn" ) ;
273-
274- expect ( result . contentType ) . toBe ( "application/json" ) ;
275- expect ( result . response ) . toBe ( "Version 2 CSN" ) ;
248+ await expect ( getMetadata ( url ) ) . resolves . toEqual ( {
249+ response : "Version 2 CSN" ,
250+ contentType : "application/json" ,
251+ } ) ;
276252 } ) ;
277253
278254 test ( "getMetadata should pass compile options from cds.env" , async ( ) => {
@@ -285,10 +261,10 @@ describe("metaData", () => {
285261 return "Content with compile options" ;
286262 } ) ;
287263
288- const result = await getMetadata ( url ) ;
289-
290- expect ( result . contentType ) . toBe ( "application/json" ) ;
291- expect ( result . response ) . toBe ( "Content with compile options" ) ;
264+ await expect ( getMetadata ( url ) ) . resolves . toEqual ( {
265+ contentType : "application/json" ,
266+ response : "Content with compile options" ,
267+ } ) ;
292268 } ) ;
293269
294270 describe ( "@OpenAPI.servers annotation" , ( ) => {
0 commit comments