Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 14, 2025

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 EndpointHarness with request capture

All MSW endpoints now automatically capture requests with:

  • getCapturedRequests() / getLastCapturedRequest() - Access captured request data (URL, method, headers, parsed body, timestamp)
  • getCapturedRequestCount() - Get request count
  • waitForNextRequest(timeout) - Async wait for next request
  • clearCapturedRequests() - Clear captured requests

Added MockAnalyticsApi

New mock for analytics endpoint *.analytics.org.coveo.com/rest/organizations/:orgId/events/v1:

import { MockAnalyticsApi } from '@/storybook-utils/api/analytics/mock';

const analyticsHarness = new MockAnalyticsApi();

const meta: Meta = {
  parameters: {
    msw: {
      handlers: [...analyticsHarness.handlers],
    },
  },
};

export const TracksClick: Story = {
  play: async ({ canvasElement }) => {
    analyticsHarness.clearAllRequests();
    
    await userEvent.click(element);
    
    const request = await analyticsHarness.eventsEndpoint.waitForNextRequest();
    expect(request.body).toMatchObject({
      eventType: 'ec.productClick',
      eventValue: 'expected-value',
    });
  },
};

Added AnalyticsHelper for Playwright e2e tests

New helper class for accessing captured requests in Playwright e2e tests that load stories in an iframe:

import { AnalyticsHelper } from '@/playwright-utils/analytics-helper';

test('should send analytics', async ({ productLink, page }) => {
  const analyticsHelper = new AnalyticsHelper(page);
  await analyticsHelper.clearRequests();
  
  await productLink.anchor().first().click();
  
  const request = await analyticsHelper.waitForRequest();
  expect(request.body).toMatchObject({
    eventType: 'ec.productClick',
  });
});

The AnalyticsHelper bridges the MSW harness running in the Storybook iframe to Playwright test context via page.evaluate(), providing async methods: clearRequests(), getRequests(), getLastRequest(), getRequestCount(), and waitForRequest().

Documentation and examples

  • ANALYTICS_TRACKING.md - Comprehensive guide with migration examples for both Storybook play functions and Playwright e2e tests
  • README.md / USAGE.md - Updated with request tracking patterns
  • storybook-pages/examples/analytics-tracking.new.stories.tsx - Working examples
  • atomic-product-link.e2e.ts - Added new test demonstrating Playwright e2e usage

Request 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

In our Storybook+Playwright test, we'd like to validate whether the UA calls are properly sent. However we do not have means or ways to asserts request, and this seems to de discouraged by MSW.

What do you suggest?


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 3 commits November 14, 2025 22:09
- 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 AI changed the title [WIP] Add validation for UA calls in Storybook+Playwright tests Add request capture to MSW API harness for analytics assertion in Storybook tests Nov 14, 2025
Copilot AI requested a review from louis-bompart November 14, 2025 22:17
Copilot finished work on behalf of louis-bompart November 14, 2025 22:17
@louis-bompart
Copy link
Collaborator

@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]>
Copy link
Contributor Author

Copilot AI commented Nov 14, 2025

@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?

Good point! I've adjusted the solution to work with Playwright e2e tests that load stories in an iframe.

Changes in commit 560f6af:

  1. Created AnalyticsHelper class (playwright-utils/analytics-helper.ts) - Bridges the MSW harness running in the iframe to Playwright test context via page.evaluate()

  2. Updated atomic-product-link story - Exposes analytics harness to window.__mswAnalyticsHarness for Playwright access

  3. Added new e2e test demonstrating full payload access:

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:

  • Storybook play functions - Direct harness access
  • Playwright e2e tests - Via AnalyticsHelper that queries the iframe

Documentation updated with both usage patterns.

Copilot AI changed the title Add request capture to MSW API harness for analytics assertion in Storybook tests Add request capture to MSW API harness for analytics assertion in Storybook and Playwright e2e tests Nov 14, 2025
Copilot finished work on behalf of louis-bompart November 14, 2025 22:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants