-
Notifications
You must be signed in to change notification settings - Fork 39
Add request capture to MSW API harness for analytics assertion in Storybook and Playwright e2e tests #6558
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?
Conversation
- Enhanced EndpointHarness with request tracking capabilities - Added MockAnalyticsApi for capturing and asserting on UA calls - Created comprehensive documentation and examples - Added analytics-tracking.new.stories.tsx demonstrating the feature Co-authored-by: louis-bompart <[email protected]>
- Created ANALYTICS_TRACKING.md with detailed usage guide - Documented problem statement, solution, and migration guide - Added API reference and common patterns - Provided before/after comparison examples Co-authored-by: louis-bompart <[email protected]>
- Created PR_SUMMARY.md with quick start guide - Documented benefits and backward compatibility - Added comparison table and examples Co-authored-by: louis-bompart <[email protected]>
|
@copilot , our playwright spec opens the story in the iframe, and usually we run our assertions there, can you adjust your proposal/idea with this in mind? |
- Created AnalyticsHelper class to bridge MSW harness in iframe to Playwright tests - Updated atomic-product-link story to expose analytics harness to window - Added new e2e test demonstrating full payload access - Updated documentation with iframe/Playwright usage patterns Co-authored-by: louis-bompart <[email protected]>
Good point! I've adjusted the solution to work with Playwright e2e tests that load stories in an iframe. Changes in commit 560f6af:
const analyticsHelper = new AnalyticsHelper(page);
await analyticsHelper.clearRequests();
await productLink.anchor().first().click();
const request = await analyticsHelper.waitForRequest();
// Now has full payload access!
expect(request.body).toMatchObject({
eventType: 'ec.productClick',
});The solution now works in both contexts:
Documentation updated with both usage patterns. |
Description
Storybook+Playwright tests cannot validate analytics calls because MSW doesn't expose request assertions and Playwright's
page.waitForRequest()cannot access request payloads due to chromium#6479.Changes
Enhanced
EndpointHarnesswith request captureAll MSW endpoints now automatically capture requests with:
getCapturedRequests()/getLastCapturedRequest()- Access captured request data (URL, method, headers, parsed body, timestamp)getCapturedRequestCount()- Get request countwaitForNextRequest(timeout)- Async wait for next requestclearCapturedRequests()- Clear captured requestsAdded
MockAnalyticsApiNew mock for analytics endpoint
*.analytics.org.coveo.com/rest/organizations/:orgId/events/v1:Added
AnalyticsHelperfor Playwright e2e testsNew helper class for accessing captured requests in Playwright e2e tests that load stories in an iframe:
The
AnalyticsHelperbridges the MSW harness running in the Storybook iframe to Playwright test context viapage.evaluate(), providing async methods:clearRequests(),getRequests(),getLastRequest(),getRequestCount(), andwaitForRequest().Documentation and examples
ANALYTICS_TRACKING.md- Comprehensive guide with migration examples for both Storybook play functions and Playwright e2e testsREADME.md/USAGE.md- Updated with request tracking patternsstorybook-pages/examples/analytics-tracking.new.stories.tsx- Working examplesatomic-product-link.e2e.ts- Added new test demonstrating Playwright e2e usageRequest tracking works for all endpoints, not just analytics. 100% backward compatible. Supports both Storybook play functions and Playwright e2e tests in iframe context.
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.