-
-
Notifications
You must be signed in to change notification settings - Fork 7
feat: add ignoreCommonAssetRequests option
#14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ import { | |
| RequestHandler, | ||
| WebSocketHandler, | ||
| getResponse, | ||
| isCommonAssetRequest, | ||
| } from 'msw' | ||
| import { | ||
| type WebSocketClientEventMap, | ||
|
|
@@ -24,7 +25,8 @@ import { | |
| } from '@mswjs/interceptors/WebSocket' | ||
|
|
||
| export interface CreateNetworkFixtureArgs { | ||
| initialHandlers: Array<RequestHandler | WebSocketHandler> | ||
| ignoreCommonAssetRequests?: boolean | ||
| initialHandlers?: Array<RequestHandler | WebSocketHandler> | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -57,6 +59,7 @@ export function createNetworkFixture( | |
| async ({ page }, use) => { | ||
| const worker = new NetworkFixture({ | ||
| page, | ||
| ignoreCommonAssetRequests: args?.ignoreCommonAssetRequests ?? false, | ||
| initialHandlers: args?.initialHandlers || [], | ||
| }) | ||
|
|
||
|
|
@@ -70,13 +73,16 @@ export function createNetworkFixture( | |
|
|
||
| export class NetworkFixture extends SetupApi<LifeCycleEventsMap> { | ||
| #page: Page | ||
| #ignoreCommonAssetRequests: boolean | ||
|
|
||
| constructor(args: { | ||
| page: Page | ||
| ignoreCommonAssetRequests: boolean | ||
| initialHandlers: Array<RequestHandler | WebSocketHandler> | ||
| }) { | ||
| super(...args.initialHandlers) | ||
| this.#page = args.page | ||
| this.#ignoreCommonAssetRequests = args.ignoreCommonAssetRequests | ||
| } | ||
|
|
||
| public async start() { | ||
|
|
@@ -88,6 +94,11 @@ export class NetworkFixture extends SetupApi<LifeCycleEventsMap> { | |
| body: request.postDataBuffer(), | ||
| }) | ||
|
|
||
| if (this.#ignoreCommonAssetRequests && isCommonAssetRequest(fetchRequest)) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's one thing I still don't quite understand. By ignoring requests here we are claiming that the performance degradation originates from the I believe we need to profile this problem better before merging a solution.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think this would be slower at scale (e.g. thousands of requests) due to unnecessary overhead, but not meaningful for fewer total requests.
I did some not-very-scientific profiling, and here are the results I am seeing: With With
My conclusions/suspicions are as follows:
|
||
| route.continue() | ||
| return | ||
| } | ||
|
|
||
| const response = await getResponse( | ||
| this.handlersController.currentHandlers().filter((handler) => { | ||
| return handler instanceof RequestHandler | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| import { test as testBase, expect } from '@playwright/test' | ||
| import { http } from 'msw' | ||
| import { http, HttpResponse } from 'msw' | ||
| import { createNetworkFixture, type NetworkFixture } from '../src/index.js' | ||
|
|
||
| interface Fixtures { | ||
|
|
@@ -107,3 +107,41 @@ test('intercepts a POST request with array buffer body', async ({ | |
| expect(request.url).toBe('http://localhost:5173/action') | ||
| await expect(request.text()).resolves.toBe('hello world') | ||
| }) | ||
|
|
||
| test('intercepts an asset request when interceptCommonAssetRequests is false', async ({ network, page }) => { | ||
| network.use( | ||
| http.get('/index.html', () => { | ||
| return HttpResponse.text('NOT VALID HTML') | ||
| }), | ||
| ) | ||
|
|
||
| await page.goto('/') | ||
| const htmlContent = await page.evaluate(async () => { | ||
| const res = await fetch('/index.html') | ||
| return res.text() | ||
| }) | ||
|
|
||
| expect(htmlContent).toContain('NOT VALID HTML') | ||
| }) | ||
|
|
||
| const testIgnoreAssets = testBase.extend<Fixtures>({ | ||
| network: createNetworkFixture({ ignoreCommonAssetRequests: true }), | ||
| }) | ||
|
|
||
| testIgnoreAssets('passes through an asset request when interceptCommonAssetRequests is true', async ({ network, page }) => { | ||
|
||
| network.use( | ||
| http.get('/index.html', () => { | ||
| return HttpResponse.text('NOT VALID HTML') | ||
| }), | ||
| ) | ||
|
|
||
| await page.goto('/') | ||
| const htmlContent = await page.evaluate(async () => { | ||
| const res = await fetch('/index.html') | ||
| return res.text() | ||
| }) | ||
|
|
||
| expect(htmlContent).toContain('DOCTYPE html') | ||
| }) | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I opted to make this an option and default it to
falseso as not to introduce a breaking change.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest drop this and make it
trueby default since you're introducing a breaking change anyway (feat:).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated to make the option
truein 5db5185. Are you suggesting that we remove the option altogether? I'm hesitant to do that unless we implement the logic you suggested that only calls for each passed in MSW handler, since it would prevent mocking assets altogether in that case.