Skip to content
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

fix: handler delay not being respected #49

Merged
merged 2 commits into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
67 changes: 67 additions & 0 deletions packages/example/tests/playwright/specs/delay.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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');

// wait a little bit just to make sure DOM has had time to update
await page.waitForTimeout(10);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe one of the waitUntil option on page.goto would suffice?

https://playwright.dev/docs/api/class-page#page-goto

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, I didn't know about this. I've updated the test to use it :)

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);
});
};