Skip to content

Commit

Permalink
feat: use ws to send spec results asap
Browse files Browse the repository at this point in the history
  • Loading branch information
agoldis committed May 30, 2023
1 parent 0599cba commit e521048
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 7 deletions.
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/cypress-cloud/lib/cypress/cypress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import Debug from "debug";
import _ from "lodash";
import { getCypressRunAPIParams } from "../config";
import { getWSSPort } from "../ws";

const debug = Debug("currents:cypress");
interface RunCypressSpecFile {
Expand Down Expand Up @@ -46,7 +47,7 @@ export async function runSpecFile(
},
env: {
...runAPIOptions.env,
currents_ws: true,
currents_ws: getWSSPort(),
},
spec,
};
Expand Down
24 changes: 21 additions & 3 deletions packages/cypress-cloud/lib/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ import { getGitInfo } from "./git";
import { setAPIBaseUrl, setRunId } from "./httpClient";
import { bold, divider, info, spacer, title } from "./log";
import { getPlatform } from "./platform";
import { pubsub } from "./pubsub";
import { summarizeTestResults, summaryTable } from "./results";
import { runTillDoneOrCancelled, summary, uploadTasks } from "./runner";
import { getSpecFiles } from "./specMatcher";
import { startWSS } from "./ws";

const debug = Debug("currents:run");

Expand Down Expand Up @@ -88,12 +90,13 @@ export async function run(params: CurrentsRunParameters = {}) {
autoCancelAfterFailures,
});

info("🎥 Run URL:", bold(run.runUrl));

setRunId(run.runId);

info("🎥 Run URL:", bold(run.runUrl));
cutInitialOutput();

await startWSS();
listenToSpecEnd();

await runTillDoneOrCancelled(
{
runId: run.runId,
Expand Down Expand Up @@ -124,3 +127,18 @@ export async function run(params: CurrentsRunParameters = {}) {
}
return _summary;
}

function listenToSpecEnd() {
pubsub.on(
"after:spec",
async ({ spec, results }: { spec: Cypress.Spec; results: any }) => {
console.log(spec, results);
// // TODO: what about errored specs?
// setSpecResult(
// spec.name,
// results,
// getInitialOutput() + getCapturedOutput()
// );
}
);
}
2 changes: 0 additions & 2 deletions packages/cypress-cloud/lib/ws.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/cypress-cloud/lib/ws/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./ws";
36 changes: 36 additions & 0 deletions packages/cypress-cloud/lib/ws/ws.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import http from "http";
import { match, P } from "ts-pattern";
import WebSocket from "ws";
import { pubsub } from "../pubsub";

let server: http.Server | null = null;
let wss: WebSocket.Server | null = null;

export const getWSSPort = () =>
match(server?.address())
.with({ port: P.number }, (address) => address.port)
.run();

export const startWSS = () => {
if (wss) {
return;
}
server = http
.createServer()
.on("listening", () => {
if (!server) {
throw new Error("Server not initialized");
}
wss = new WebSocket.Server({
server,
});
console.log("starting wss on port %d", getWSSPort());
wss.on("connection", function connection(ws) {
ws.on("message", function incoming(event) {
const message = JSON.parse(event.toString());
pubsub.emit(message.type, message.payload);
});
});
})
.listen();
};
1 change: 1 addition & 0 deletions packages/cypress-cloud/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"source-map-support": "^0.5.21",
"table": "^6.8.1",
"tmp-promise": "^3.0.3",
"ts-pattern": "^4.3.0",
"ws": "^8.12.1"
},
"peerDependencies": {
Expand Down
33 changes: 32 additions & 1 deletion packages/cypress-cloud/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import fs from "fs";
import { format } from "util";
import WebSocket from "ws";

export async function cloudPlugin(
_on: Cypress.PluginEvents,
on: Cypress.PluginEvents,
config: Cypress.PluginConfigOptions
) {
function debug(...args: unknown[]) {
Expand All @@ -13,6 +14,29 @@ export async function cloudPlugin(
}
}

let ws: WebSocket | null = null;
function sendToWS(message: unknown) {
if (ws) {
ws.send(JSON.stringify(message));
}
}

if (config.env.currents_ws) {
debug("setting port to %s", config.env.currents_ws);
await new Promise((resolve) => {
ws = new WebSocket(`ws://localhost:${config.env.currents_ws}`);
ws.on("open", () => {
resolve(null);
sendToWS({
type: "currents:config",
payload: {
config,
},
});
});
});
}

debug("currents plugin loaded");

if (config.env.currents_temp_file) {
Expand All @@ -21,6 +45,13 @@ export async function cloudPlugin(
debug("config is availabe at '%s'", config.env.currents_temp_file);
}

on("before:spec", (spec) => {
sendToWS({ type: "before:spec", payload: { spec } });
});
on("after:spec", (spec, results) => {
sendToWS({ type: "after:spec", payload: { spec, results } });
});

return config;
}

Expand Down

0 comments on commit e521048

Please sign in to comment.