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

Wait for trailing events from cypress support file before ending spec #292

Merged
Merged
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
2 changes: 1 addition & 1 deletion packages/cypress/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ function onBeforeSpec(spec: Cypress.Spec) {
function onAfterSpec(spec: Cypress.Spec, result: CypressCommandLine.RunResult) {
debugEvents("Handling after:spec %s", spec.relative);
assertReporter(cypressReporter);
cypressReporter.onAfterSpec(spec, result);
return cypressReporter.onAfterSpec(spec, result);
}

function onReplayTask(value: any) {
Expand Down
27 changes: 24 additions & 3 deletions packages/cypress/src/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import type { StepEvent } from "./support";
import { PluginFeature, getFeatures, isFeatureEnabled } from "./features";

type Test = TestMetadataV2.Test;
export type PluginOptions = ReplayReporterConfig;

const debug = dbg("replay:cypress:reporter");

export type PluginOptions = ReplayReporterConfig;
const MAX_WAIT = 20_000;

function isStepEvent(value: unknown): value is StepEvent {
if (
Expand Down Expand Up @@ -108,9 +108,26 @@ class CypressReporter {
this.reporter.onTestBegin(undefined, getMetadataFilePath());
}

onAfterSpec(spec: Cypress.Spec, result: CypressCommandLine.RunResult) {
async waitForStableStepCount() {
let currentCount = this.getStepCount();
const startTime = Date.now();
while (Date.now() < startTime + MAX_WAIT) {
debug("Waiting for stable step count: %d", currentCount);
const previousCount = currentCount;
await new Promise(resolve => setTimeout(resolve, 250));
currentCount = this.getStepCount();

if (previousCount === currentCount) {
debug("Step count stable at %d after %s ms", Date.now() - startTime);
break;
}
}
}

async onAfterSpec(spec: Cypress.Spec, result: CypressCommandLine.RunResult) {
appendToFixtureFile("spec:end", { spec, result });

await this.waitForStableStepCount();
const tests = this.getTestResults(spec, result);

this.reporter.onTestEnd({
Expand Down Expand Up @@ -142,6 +159,10 @@ class CypressReporter {
this.steps.push(step);
}

getStepCount() {
return this.steps.length;
}

private getTestResults(spec: Cypress.Spec, result: CypressCommandLine.RunResult): Test[] {
const placeholderTest: Test = {
id: 0,
Expand Down
14 changes: 7 additions & 7 deletions packages/cypress/tests/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import plugin, { getCypressReporter } from "../src/index";
import { CONNECT_TASK_NAME } from "../src/constants";

async function driver(
callback: (type: string, value: any) => void,
callback: (type: string, value: any) => Promise<void>,
{
file = path.resolve(__dirname, "./fixtures/fixture.log"),
delay = 100,
Expand All @@ -23,7 +23,7 @@ async function driver(
for await (const line of rl) {
try {
const json = JSON.parse(line);
callback(json.type, json.value);
await callback(json.type, json.value);
} catch (e) {
console.error("Error parsing JSON");
console.error(e);
Expand All @@ -39,7 +39,7 @@ async function driver(
}
}

type EmitterCallback = ((...args: any[]) => void) | Record<string, (value: any) => any>;
type EmitterCallback = ((...args: any[]) => Promise<void>) | Record<string, (value: any) => any>;
const events: Record<string, EmitterCallback[]> = {};
const emitter = (name: string, cb: EmitterCallback) => {
events[name] = events[name] || [];
Expand All @@ -63,7 +63,7 @@ if (typeof connector === "function") {
});

await driver(
(type, value) => {
async (type, value) => {
let DateNow: any = undefined;
switch (type) {
case "spec:start":
Expand All @@ -79,10 +79,10 @@ if (typeof connector === "function") {
});
break;
case "spec:end":
events["after:spec"]?.forEach(f => {
for (const f of events["after:spec"]) {
if (typeof f !== "function") return;
f(value.spec, value.result);
});
await f(value.spec, value.result);
}
break;
case "task":
ws.send(JSON.stringify({ events: [value] }));
Expand Down
Loading