1- import { describe , expect , test , beforeEach , vi , type Mock } from "vitest" ;
1+ import {
2+ afterEach ,
3+ describe ,
4+ expect ,
5+ test ,
6+ beforeEach ,
7+ vi ,
8+ type Mock ,
9+ } from "vitest" ;
210
311import { loadResource } from "./resource-loader" ;
412import type { ResourceRequest } from "./schema/resources" ;
@@ -13,6 +21,10 @@ describe("loadResource", () => {
1321 vi . clearAllMocks ( ) ;
1422 } ) ;
1523
24+ afterEach ( ( ) => {
25+ vi . restoreAllMocks ( ) ;
26+ } ) ;
27+
1628 test ( "should successfully fetch a resource and return a JSON response" , async ( ) => {
1729 const mockResponse = new Response ( JSON . stringify ( { key : "value" } ) , {
1830 status : 200 ,
@@ -156,4 +168,37 @@ describe("loadResource", () => {
156168 ] ) ,
157169 } ) ;
158170 } ) ;
171+
172+ test ( "should log failed resource responses as info" , async ( ) => {
173+ const consoleError = vi
174+ . spyOn ( console , "error" )
175+ . mockImplementation ( ( ) => { } ) ;
176+ const consoleInfo = vi . spyOn ( console , "info" ) . mockImplementation ( ( ) => { } ) ;
177+ const mockResponse = new Response ( "not found" , {
178+ status : 404 ,
179+ } ) ;
180+ mockFetch . mockResolvedValue ( mockResponse ) ;
181+
182+ const resourceRequest : ResourceRequest = {
183+ name : "resource" ,
184+ url : "https://example.com/resource" ,
185+ searchParams : [ ] ,
186+ method : "get" ,
187+ headers : [ ] ,
188+ body : undefined ,
189+ } ;
190+
191+ const result = await loadResource ( mockFetch , resourceRequest ) ;
192+
193+ expect ( result ) . toEqual ( {
194+ data : "not found" ,
195+ ok : false ,
196+ status : 404 ,
197+ statusText : "" ,
198+ } ) ;
199+ expect ( consoleError ) . not . toHaveBeenCalled ( ) ;
200+ expect ( consoleInfo ) . toHaveBeenCalledWith (
201+ 'Failed to load resource: https://example.com/resource - 404: "not found"'
202+ ) ;
203+ } ) ;
159204} ) ;
0 commit comments