-
-
Notifications
You must be signed in to change notification settings - Fork 535
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: move
spyOnLifeCycleEvents
to node utils
- Loading branch information
1 parent
61c8fe7
commit bc0e0a7
Showing
5 changed files
with
72 additions
and
180 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
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 @@ | ||
import { DeferredPromise } from '@open-draft/deferred-promise' | ||
import { vi, afterEach } from 'vitest' | ||
import { LifeCycleEventsMap, SetupApi } from 'msw' | ||
|
||
export function spyOnLifeCycleEvents(api: SetupApi<LifeCycleEventsMap>) { | ||
const listener = vi.fn() | ||
const requestIdPromise = new DeferredPromise<string>() | ||
|
||
afterEach(() => listener.mockReset()) | ||
|
||
api.events | ||
.on('request:start', ({ request, requestId }) => { | ||
if (request.headers.has('upgrade')) { | ||
return | ||
} | ||
|
||
requestIdPromise.resolve(requestId) | ||
listener(`[request:start] ${request.method} ${request.url} ${requestId}`) | ||
}) | ||
.on('request:match', ({ request, requestId }) => { | ||
listener(`[request:match] ${request.method} ${request.url} ${requestId}`) | ||
}) | ||
.on('request:unhandled', ({ request, requestId }) => { | ||
listener( | ||
`[request:unhandled] ${request.method} ${request.url} ${requestId}`, | ||
) | ||
}) | ||
.on('request:end', ({ request, requestId }) => { | ||
if (request.headers.has('upgrade')) { | ||
return | ||
} | ||
|
||
listener(`[request:end] ${request.method} ${request.url} ${requestId}`) | ||
}) | ||
.on('response:mocked', async ({ response, request, requestId }) => { | ||
listener( | ||
`[response:mocked] ${request.method} ${request.url} ${requestId} ${ | ||
response.status | ||
} ${await response.clone().text()}`, | ||
) | ||
}) | ||
.on('response:bypass', async ({ response, request, requestId }) => { | ||
if (request.headers.has('upgrade')) { | ||
return | ||
} | ||
|
||
listener( | ||
`[response:bypass] ${request.method} ${request.url} ${requestId} ${ | ||
response.status | ||
} ${await response.clone().text()}`, | ||
) | ||
}) | ||
|
||
return { | ||
listener, | ||
requestIdPromise, | ||
} | ||
} |
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