@data-eden/network
Advanced tools
Comparing version 0.3.1 to 0.3.2
@@ -167,2 +167,50 @@ import { | ||
test('allows globalThis.fetch to be mocked per test (e.g. Pretender / MirageJS / msw style)', async () => { | ||
const fetch = buildFetch([]); | ||
let response = await fetch(server.buildUrl('/resource')); | ||
expect(await response.json(), 'precond - uses globalThis.fetch by default') | ||
.toMatchInlineSnapshot(` | ||
{ | ||
"customOriginalRequestHeaders": {}, | ||
"status": "success", | ||
} | ||
`); | ||
const originalFetch = globalThis.fetch; | ||
try { | ||
/* eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-explicit-any*/ | ||
delete (globalThis as any).fetch; | ||
const customFetch = ( | ||
_input: URL | RequestInfo, | ||
_init?: RequestInit | ||
): Promise<Response> => { | ||
// does not call `next` at all, just resolves with some specific response | ||
return Promise.resolve(new Response('We overrode fetch!')); | ||
}; | ||
// overrides the global `fetch` **after** `buildFetch` has been called | ||
globalThis.fetch = customFetch; | ||
response = await fetch(server.buildUrl('/resource')); | ||
expect( | ||
await response.text(), | ||
'overriden fetch was used' | ||
).toMatchInlineSnapshot('"We overrode fetch!"'); | ||
} finally { | ||
globalThis.fetch = originalFetch; | ||
} | ||
response = await fetch(server.buildUrl('/resource')); | ||
expect( | ||
response.status, | ||
'original fetch was used after globalThis.fetch was reset' | ||
).toEqual(200); | ||
}); | ||
test('should be able to override fetch', async () => { | ||
@@ -187,2 +235,30 @@ expect.assertions(1); | ||
test('can not change options.fetch lazily', async () => { | ||
const options = { | ||
fetch: ( | ||
_input: URL | RequestInfo, | ||
_init?: RequestInit | ||
): Promise<Response> => { | ||
return Promise.resolve(new Response('custom fetch here!')); | ||
}, | ||
}; | ||
const fetch = buildFetch([noopMiddleware], options); | ||
let response = await fetch(server.buildUrl('/resource')); | ||
expect(await response.text()).toMatchInlineSnapshot('"custom fetch here!"'); | ||
options.fetch = ( | ||
_input: URL | RequestInfo, | ||
_init?: RequestInit | ||
): Promise<Response> => { | ||
return Promise.resolve(new Response('We overrode fetch!')); | ||
}; | ||
response = await fetch(server.buildUrl('/resource')); | ||
expect( | ||
await response.text(), | ||
'overridden `options.fetch` is not used' | ||
).toMatchInlineSnapshot('"custom fetch here!"'); | ||
}); | ||
test('should be able to change the http method for a given request', async () => { | ||
@@ -189,0 +265,0 @@ expect.assertions(2); |
@@ -6,2 +6,5 @@ function combine(next, middleware) { | ||
} | ||
function globalFetch(request) { | ||
return fetch(request); | ||
} | ||
/** | ||
@@ -17,3 +20,3 @@ * | ||
} | ||
const _fetch = options?.fetch || fetch; | ||
const _fetch = options?.fetch || globalFetch; | ||
const curriedMiddlewares = [...middlewares] | ||
@@ -20,0 +23,0 @@ .reverse() |
{ | ||
"name": "@data-eden/network", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"repository": { | ||
@@ -27,3 +27,3 @@ "type": "git", | ||
"devDependencies": { | ||
"@data-eden/shared-test-utilities": "0.3.1", | ||
"@data-eden/shared-test-utilities": "0.3.2", | ||
"cross-fetch": "^3.1.5" | ||
@@ -30,0 +30,0 @@ }, |
@@ -44,2 +44,6 @@ export type Fetch = typeof fetch; | ||
function globalFetch(request: Request): Promise<Response> { | ||
return fetch(request); | ||
} | ||
/** | ||
@@ -60,5 +64,4 @@ * | ||
} | ||
const _fetch: NormalizedFetch = options?.fetch || globalFetch; | ||
const _fetch: NormalizedFetch = options?.fetch || fetch; | ||
const curriedMiddlewares: NormalizedFetch = [...middlewares] | ||
@@ -65,0 +68,0 @@ .reverse() |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
112529
555