-
-
Notifications
You must be signed in to change notification settings - Fork 530
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: support async generators as response resolvers (#2108)
Co-authored-by: Jake Bailey <[email protected]>
- Loading branch information
1 parent
6e278b6
commit d38fc3d
Showing
6 changed files
with
253 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,32 @@ | ||
/** | ||
* This is the same as TypeScript's `Iterable`, but with all three type parameters. | ||
* @todo Remove once TypeScript 5.6 is the minimum. | ||
*/ | ||
export interface Iterable<T, TReturn, TNext> { | ||
[Symbol.iterator](): Iterator<T, TReturn, TNext> | ||
} | ||
|
||
/** | ||
* This is the same as TypeScript's `AsyncIterable`, but with all three type parameters. | ||
* @todo Remove once TypeScript 5.6 is the minimum. | ||
*/ | ||
export interface AsyncIterable<T, TReturn, TNext> { | ||
[Symbol.asyncIterator](): AsyncIterator<T, TReturn, TNext> | ||
} | ||
|
||
/** | ||
* Determines if the given function is an iterator. | ||
*/ | ||
export function isIterable<IteratorType>( | ||
fn: any, | ||
): fn is Generator<IteratorType, IteratorType, IteratorType> { | ||
): fn is | ||
| Iterable<IteratorType, IteratorType, IteratorType> | ||
| AsyncIterable<IteratorType, IteratorType, IteratorType> { | ||
if (!fn) { | ||
return false | ||
} | ||
|
||
return typeof (fn as Generator<unknown>)[Symbol.iterator] == 'function' | ||
return ( | ||
Reflect.has(fn, Symbol.iterator) || Reflect.has(fn, Symbol.asyncIterator) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/** | ||
* @vitest-environment node | ||
*/ | ||
import { http, HttpResponse, delay } from 'msw' | ||
import { setupServer } from 'msw/node' | ||
|
||
const server = setupServer() | ||
|
||
async function fetchJson(input: string | URL | Request, init?: RequestInit) { | ||
return fetch(input, init).then((response) => response.json()) | ||
} | ||
|
||
beforeAll(() => { | ||
server.listen() | ||
}) | ||
|
||
afterEach(() => { | ||
server.resetHandlers() | ||
}) | ||
|
||
afterAll(() => { | ||
server.close() | ||
}) | ||
|
||
it('supports generator function as response resolver', async () => { | ||
server.use( | ||
http.get('https://example.com/weather', function* () { | ||
let degree = 10 | ||
|
||
while (degree < 13) { | ||
degree++ | ||
yield HttpResponse.json(degree) | ||
} | ||
|
||
degree++ | ||
return HttpResponse.json(degree) | ||
}), | ||
) | ||
|
||
// Must respond with yielded responses. | ||
await expect(fetchJson('https://example.com/weather')).resolves.toEqual(11) | ||
await expect(fetchJson('https://example.com/weather')).resolves.toEqual(12) | ||
await expect(fetchJson('https://example.com/weather')).resolves.toEqual(13) | ||
// Must respond with the final "done" response. | ||
await expect(fetchJson('https://example.com/weather')).resolves.toEqual(14) | ||
// Must keep responding with the final "done" response. | ||
await expect(fetchJson('https://example.com/weather')).resolves.toEqual(14) | ||
}) | ||
|
||
it('supports async generator function as response resolver', async () => { | ||
server.use( | ||
http.get('https://example.com/weather', async function* () { | ||
await delay(20) | ||
|
||
let degree = 10 | ||
|
||
while (degree < 13) { | ||
degree++ | ||
yield HttpResponse.json(degree) | ||
} | ||
|
||
degree++ | ||
return HttpResponse.json(degree) | ||
}), | ||
) | ||
|
||
await expect(fetchJson('https://example.com/weather')).resolves.toEqual(11) | ||
await expect(fetchJson('https://example.com/weather')).resolves.toEqual(12) | ||
await expect(fetchJson('https://example.com/weather')).resolves.toEqual(13) | ||
await expect(fetchJson('https://example.com/weather')).resolves.toEqual(14) | ||
await expect(fetchJson('https://example.com/weather')).resolves.toEqual(14) | ||
}) | ||
|
||
it('supports generator function as one-time response resolver', async () => { | ||
server.use( | ||
http.get( | ||
'https://example.com/weather', | ||
function* () { | ||
let degree = 10 | ||
|
||
while (degree < 13) { | ||
degree++ | ||
yield HttpResponse.json(degree) | ||
} | ||
|
||
degree++ | ||
return HttpResponse.json(degree) | ||
}, | ||
{ once: true }, | ||
), | ||
http.get('*', () => { | ||
return HttpResponse.json('fallback') | ||
}), | ||
) | ||
|
||
// Must respond with the yielded incrementing responses. | ||
await expect(fetchJson('https://example.com/weather')).resolves.toEqual(11) | ||
await expect(fetchJson('https://example.com/weather')).resolves.toEqual(12) | ||
await expect(fetchJson('https://example.com/weather')).resolves.toEqual(13) | ||
// Must respond with the "done" final response from the iterator. | ||
await expect(fetchJson('https://example.com/weather')).resolves.toEqual(14) | ||
// Must respond with the other handler since the generator one is used. | ||
await expect(fetchJson('https://example.com/weather')).resolves.toEqual( | ||
'fallback', | ||
) | ||
await expect(fetchJson('https://example.com/weather')).resolves.toEqual( | ||
'fallback', | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { it } from 'vitest' | ||
import { http, HttpResponse } from 'msw' | ||
|
||
it('supports generator function as response resolver', () => { | ||
http.get<never, never, { value: number }>('/', function* () { | ||
yield HttpResponse.json({ value: 1 }) | ||
yield HttpResponse.json({ value: 2 }) | ||
return HttpResponse.json({ value: 3 }) | ||
}) | ||
|
||
http.get<never, never, { value: string }>('/', function* () { | ||
yield HttpResponse.json({ value: 'one' }) | ||
yield HttpResponse.json({ | ||
// @ts-expect-error Expected string, got number. | ||
value: 2, | ||
}) | ||
return HttpResponse.json({ value: 'three' }) | ||
}) | ||
}) | ||
|
||
it('supports async generator function as response resolver', () => { | ||
http.get<never, never, { value: number }>('/', async function* () { | ||
yield HttpResponse.json({ value: 1 }) | ||
yield HttpResponse.json({ value: 2 }) | ||
return HttpResponse.json({ value: 3 }) | ||
}) | ||
|
||
http.get<never, never, { value: string }>('/', async function* () { | ||
yield HttpResponse.json({ value: 'one' }) | ||
yield HttpResponse.json({ | ||
// @ts-expect-error Expected string, got number. | ||
value: 2, | ||
}) | ||
return HttpResponse.json({ value: 'three' }) | ||
}) | ||
}) | ||
|
||
it('supports returning nothing from generator resolvers', () => { | ||
http.get<never, never, { value: string }>('/', function* () {}) | ||
http.get<never, never, { value: string }>('/', async function* () {}) | ||
}) | ||
|
||
it('supports returning undefined from generator resolvers', () => { | ||
http.get<never, never, { value: string }>('/', function* () { | ||
return undefined | ||
}) | ||
http.get<never, never, { value: string }>('/', async function* () { | ||
return undefined | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters