aboutsummaryrefslogtreecommitdiffstats
path: root/tests/cypress/support/msw.ts
blob: 829b1479ff4ce8bd06c294101db78efe472caac5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import type { SetupWorker } from 'msw/lib/browser';

export type CustomWindow = {
  msw?: {
    worker: SetupWorker;
  };
} & Window;

Cypress.on('test:before:run:async', async () => {
  window.process = {
    // @ts-expect-error -- window.process type is not NodeJS process type
    env: {
      NEXT_PUBLIC_STAGING_GRAPHQL_API: Cypress.env(
        'NEXT_PUBLIC_STAGING_GRAPHQL_API'
      ),
    },
  };

  if (!('msw' in window) || !window.msw) {
    const { worker } = await import('../../msw/browser');
    await worker
      .start({
        onUnhandledRequest(request) {
          if (
            request.url.includes('/_next/') ||
            request.url.includes('/__next')
          ) {
            return;
          }

          console.warn(
            '[MSW] Warning: intercepted a request without a matching request handler: %s %s',
            request.method,
            request.url
          );
        },
      })
      .then(() => {
        (window as CustomWindow).msw = {
          worker,
        };
      });
  }
});