-
Notifications
You must be signed in to change notification settings - Fork 9
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
}; |
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 |
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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 |
||
(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; |
There was a problem hiding this comment.
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.