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

Setup more robust @replayio/playwright tests #542

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
"@types/ws": "^8",
"prettier": "^2.7.1",
"replayio": "workspace:^",
"typescript": "^5.4.5",
"ws": "^8.17.0"
"typescript": "^5.4.5"
},
"dependencies": {
"node-fetch": "^3.3.1"
Expand Down
8 changes: 8 additions & 0 deletions packages/playwright/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
testTimeout: 20000,
preset: "ts-jest",
testEnvironment: "node",
testMatch: ["**/(*.)+(spec|test).[jt]s?(x)", "!**/fixtures-app/**"],
// TODO: implement dependencyExtractor: the whole src directory and the whole fixture app should be added here
};
12 changes: 10 additions & 2 deletions packages/playwright/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
"scripts": {
"prepare": "yarn run build",
"build": "rm -rf dist/ tsconfig.tsbuildinfo && tsc",
"test": "echo \"Error: no test specified\"",
"typecheck": "tsc --noEmit"
"test": "jest",
"typecheck": "tsc --noEmit",
"fixtures-app": "next dev tests/fixtures-app"
},
"devDependencies": {
"@playwright/test": "^1.40.1",
Expand All @@ -28,6 +29,12 @@
"@types/stack-utils": "^2.0.3",
"@types/uuid": "^8.3.4",
"@types/ws": "^8.5.10",
"jest": "^28.1.3",
"msw": "2.3.0-ws.rc-6",
"next": "^14.2.4",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"ts-jest": "^28.0.6",
"typescript": "^5.4.5"
},
"repository": {
Expand All @@ -45,6 +52,7 @@
"@replayio/test-utils": "workspace:^",
"debug": "^4.3.4",
"stack-utils": "^2.0.6",
"undici": "^5.28.4",
"uuid": "^8.3.2",
"ws": "^8.13.0"
},
Expand Down
36 changes: 36 additions & 0 deletions packages/playwright/tests/fixtures-app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js
.yarn/install-state.gz

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env*.local

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the core of the mocking logic. We can continue to refine this further. Right now, the test doesn't have control over WS "responses" (nor over regular requests). So It can't, for example, error any of those.

I'm looking for inspiration right now in Cypress, MSW, and Playwright. I think we could establish some conventions for aliases (like @recording[1] and more). We could set up a queue of "route" mocks where each of the mocks would mock the respective request once. I'd really like to find a way to avoid those be sensitive to the order and timing.

Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import type { FullConfig } from "@playwright/test";
import type {
addOriginalSourceResult,
addSourceMapResult,
beginRecordingUploadParameters,
beginRecordingUploadResult,
endRecordingUploadResult,
existsResult,
setAccessTokenResult,
setRecordingMetadataResult,
} from "@replayio/protocol";
import { WebSocket } from "undici";

async function globalSetup(_config: FullConfig) {
Copy link
Member Author

Choose a reason for hiding this comment

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

I think it will be best to pass a function through this config, any given test could pass a testing function in .use within its own playwright.config.mts and we could call this function from here with the appropriate context object.

This way each test could keep its test and mocking body close to itself (colocation, yay!).

This might require us to stringify the said function but I think it's fine. It's a standard constraint when dealing with page.evaluate and similar APIs as those pass "functions" (in a form of string) across runtime boundaries

(globalThis as any).WebSocket = WebSocket;

const { http, ws, HttpResponse } = await import("msw");
const { setupServer } = await import("msw/node");

const server = setupServer(
http.get("*", async ({ request }) => {
switch (request.url) {
case "https://dispatch.replay.io/": {
return new HttpResponse("", {
status: 200,
});
}
default:
throw new Error(`Unexpected GET to: ${request.url}`);
}
}),
http.post("*", async ({ request }) => {
switch (request.url) {
case "https://api.replay.io/v1/graphql": {
const body = JSON.parse(await request.text());

switch (body.name) {
case "AddTestsToShard":
// TODO: we are interested in the data that we sent out here
return new HttpResponse(JSON.stringify({}));
case "CompleteTestRunShard":
return new HttpResponse(JSON.stringify({}));
case "CreateTestRunShard":
return new HttpResponse(
JSON.stringify({
data: {
startTestRunShard: {
testRunShardId: "test-run-shard-id",
},
},
})
);
default:
throw new Error(`Unexpected graphql operation name: ${body.name}`);
}
}
case "https://webhooks.replay.io/api/metrics":
return new HttpResponse(JSON.stringify({}));
default:
throw new Error(`Unexpected POST to: ${request.url}`);
}
}),
http.put("*", async ({ request }) => {
if (request.url.startsWith("https://app.replay.io/recording/")) {
return new HttpResponse(JSON.stringify({}));
}
throw new Error(`Unexpected PUT to: ${request.url}`);
})
);

const wsHandler = ws.link("wss://dispatch.replay.io").on("connection", ({ client, server }) => {
server.connect();

client.addEventListener("message", event => {
event.preventDefault();
const data = JSON.parse(String(event.data));
switch (data.method) {
case "Authentication.setAccessToken":
case "Internal.endRecordingUpload":
case "Internal.setRecordingMetadata":
case "Recording.addOriginalSource":
case "Recording.addSourceMap":
case "Resource.exists":
client.send(
JSON.stringify({
id: data.id,
result: {} satisfies
| addOriginalSourceResult
| addSourceMapResult
| endRecordingUploadResult
| existsResult
| setAccessTokenResult
| setRecordingMetadataResult,
})
);
return;
case "Internal.beginRecordingUpload": {
const params: beginRecordingUploadParameters = data.params;
client.send(
JSON.stringify({
id: data.id,
result: {
recordingId: params.recordingId!,
uploadLink: `https://app.replay.io/recording/${params.recordingId}`,
} satisfies beginRecordingUploadResult,
})
);
return;
}
default:
throw new Error(`Unexpected protocol method: ${data.method}`);
}
});
});

server.use(wsHandler);

server.listen();
}

export default globalSetup;
Loading
Loading