-
-
Notifications
You must be signed in to change notification settings - Fork 534
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(setupServer): set max listeners on the "request.signal" (#1765)
- Loading branch information
1 parent
078e541
commit 17d5f3a
Showing
3 changed files
with
76 additions
and
3 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
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,58 @@ | ||
/** | ||
* @jest-environment node | ||
*/ | ||
import { graphql, http, HttpResponse } from 'msw' | ||
import { setupServer } from 'msw/node' | ||
|
||
// Create a large number of request handlers. | ||
const restHandlers = new Array(100).fill(null).map((_, index) => { | ||
return http.post( | ||
`https://example.com/resource/${index}`, | ||
async ({ request }) => { | ||
const text = await request.text() | ||
return HttpResponse.text(text + index.toString()) | ||
}, | ||
) | ||
}) | ||
|
||
const graphqlHanlers = new Array(100).fill(null).map((_, index) => { | ||
return graphql.query(`Get${index}`, () => { | ||
return HttpResponse.json({ data: { index } }) | ||
}) | ||
}) | ||
|
||
const server = setupServer(...restHandlers, ...graphqlHanlers) | ||
|
||
beforeAll(() => { | ||
server.listen() | ||
jest.spyOn(process.stderr, 'write') | ||
}) | ||
|
||
afterAll(() => { | ||
server.close() | ||
jest.restoreAllMocks() | ||
}) | ||
|
||
it('does not print a memory leak warning when having many request handlers', async () => { | ||
const httpResponse = await fetch('https://example.com/resource/42', { | ||
method: 'POST', | ||
body: 'request-body-', | ||
}).then((response) => response.text()) | ||
|
||
const graphqlResponse = await fetch('https://example.com', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
query: `query Get42 { index }`, | ||
}), | ||
}).then((response) => response.json()) | ||
|
||
// Must not print any memory leak warnings. | ||
expect(process.stderr.write).not.toHaveBeenCalled() | ||
|
||
// Must return the mocked response. | ||
expect(httpResponse).toBe('request-body-42') | ||
expect(graphqlResponse).toEqual({ data: { index: 42 } }) | ||
}) |