Skip to content

Commit 2268158

Browse files
authored
Merge pull request #6 from cloudnc/fix/ensure-page-route-is-registered
fix(mockGrpcUnary): ensure route registration is awaited to avoid race conditions where a route might be attempted before registration completes
2 parents 42a9d0e + e5f32ed commit 2268158

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ test.describe('Some test wrapper', () => {
4848
test('Make sure a grpc call is made and is successful', async ({ page }) => {
4949
// start by building a mock for the unary call that will be done
5050
// for example as soon as a given page is loaded
51-
const mock = mockGrpcUnary(page, YourUnaryCall, {
51+
const mock = await mockGrpcUnary(page, YourUnaryCall, {
5252
message: YourUnaryCallResponse.encode({
5353
// all the content of the response goes here as a classic JS object
5454
// this is the mock data that will be passed in the response

src/playwright/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ export function readGrpcRequest(request: Request): Uint8Array | null {
3939
return !requestBody ? null : unframeRequest(requestBody);
4040
}
4141

42-
export function mockGrpcUnary(
42+
export async function mockGrpcUnary(
4343
page: Page,
4444
rpc: UnaryMethodDefinitionish,
4545
response: GrpcResponse | ((request: Uint8Array | null) => GrpcResponse)
46-
): MockedGrpcCall {
46+
): Promise<MockedGrpcCall> {
4747
const url = `/${rpc.service.serviceName}/${rpc.methodName}`;
4848

4949
// note this wildcard route url base is done in order to match both localhost and deployed service usages.
50-
page.route("**" + url, (route) => {
50+
await page.route("**" + url, (route) => {
5151
expect(
5252
route.request().method(),
5353
"ALL gRPC requests should be a POST request"

0 commit comments

Comments
 (0)