Skip to content

Commit

Permalink
fix: handler delay not being respected (#49)
Browse files Browse the repository at this point in the history
* fix: handler delay not being respected

* chore: use waitUntil instead
  • Loading branch information
valendres committed Dec 14, 2022
1 parent b0c44ca commit 3d08816
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 2 deletions.
64 changes: 64 additions & 0 deletions packages/example/tests/playwright/specs/delay.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { expect, test } from '../test';
import { rest } from 'msw';

test.describe.parallel('delay', () => {
test('should send mock response after specified delay in the handler', async ({
page,
worker,
}) => {
await worker.use(
rest.get('/api/users', (_, response, context) =>
response(
context.delay(500),
context.json([
{
id: 'fake',
firstName: 'Delayed',
lastName: 'Response',
},
])
)
)
);

await page.goto('/users');

// Assert that loading text is still visible at 200ms
await page.waitForTimeout(200);
await expect(page.locator('text="Loading..."')).toBeVisible({ timeout: 0 });

// Assert that loading text is still visible at 400ms
await page.waitForTimeout(200);
await expect(page.locator('text="Loading..."')).toBeVisible({ timeout: 0 });

// Assert that the query has resolved by the 600ms mark (> 500ms delay)
await page.waitForTimeout(200);
await expect(page.locator('text="Delayed Response"')).toBeVisible({
timeout: 0,
});
});

test('should send mock response straight away if no delay has been specified in the handler', async ({
page,
worker,
}) => {
await worker.use(
rest.get('/api/users', (_, response, context) =>
response(
context.json([
{
id: 'fake',
firstName: 'Instant',
lastName: 'Response',
},
])
)
)
);

await page.goto('/users', { waitUntil: 'networkidle' });
await expect(page.locator('text="Instant Response"')).toBeVisible({
timeout: 0,
});
});
});
14 changes: 12 additions & 2 deletions packages/playwright-msw/src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Route } from '@playwright/test';
import type { MockedResponse, RequestHandler } from 'msw';
import { handleRequest, MockedRequest } from 'msw';
import EventEmitter from 'events';
import { wait } from './utils';

const emitter = new EventEmitter();

Expand All @@ -18,8 +19,17 @@ export const handleRoute = async (route: Route, handlers: RequestHandler[]) => {
body: postData ? Buffer.from(postData) : undefined,
});

const handleMockResponse = ({ status, headers, body }: MockedResponse) => {
route.fulfill({
const handleMockResponse = async ({
status,
headers,
body,
delay,
}: MockedResponse) => {
if (delay) {
await wait(delay);
}

return route.fulfill({
status,
body: body ?? undefined,
contentType: headers.get('content-type') ?? undefined,
Expand Down
6 changes: 6 additions & 0 deletions packages/playwright-msw/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,9 @@ export const convertMswPathToPlaywrightUrl = (path: Path): RegExp => {
].join('')
);
};

export const wait = (delay: number): Promise<void> => {
return new Promise((resolve) => {
setTimeout(resolve, delay);
});
};

0 comments on commit 3d08816

Please sign in to comment.