diff --git a/packages/playwright-msw/src/utils.test.ts b/packages/playwright-msw/src/utils.test.ts index 0c7f111..7673fa6 100644 --- a/packages/playwright-msw/src/utils.test.ts +++ b/packages/playwright-msw/src/utils.test.ts @@ -82,6 +82,37 @@ describe('utils', () => { }); }); + describe('getHandlerPath for GraphQL handler', () => { + it.each<{ path: Path; expected: Path }>` + path | expected + ${'/api/users'} | ${'/api/users'} + ${/^\/api\/.*/} | ${/^\/api\/.*/} + `( + 'return "$expected" for graphql.link() with "$path"', + ({ path, expected }) => { + expect( + getHandlerPath( + graphql + .link(path) + .query('Query', () => HttpResponse.json({ data: {} })), + {}, + ), + ).toStrictEqual(expected); + }, + ); + + it('return the default endpoint path for graphql handler without link() call', () => { + const defaultHandler = graphql.query('Query', () => + HttpResponse.json({ data: {} }), + ); + expect( + getHandlerPath(defaultHandler, { + graphqlUrl: '/defaultGraphqlEndpoint', + }), + ).toStrictEqual('/defaultGraphqlEndpoint'); + }); + }); + describe('convertMswPathToPlaywrightUrl', () => { it.each<{ mswPath: string; playwrightUrl: string; expected: boolean }>` mswPath | playwrightUrl | expected diff --git a/packages/playwright-msw/src/utils.ts b/packages/playwright-msw/src/utils.ts index e998029..77a4155 100644 --- a/packages/playwright-msw/src/utils.ts +++ b/packages/playwright-msw/src/utils.ts @@ -26,6 +26,10 @@ export const getHandlerPath = ( config: Config, ): Path => { if (getHandlerType(handler) === 'graphql') { + const handlerInternal = handler as unknown as { endpoint: Path }; // private GraphQLHandler.endpoint + if (handlerInternal.endpoint !== '*') { + return handlerInternal.endpoint; + } const { graphqlUrl } = config; if (!graphqlUrl) { throw new Error(