Skip to content

Commit

Permalink
Change the add step cy.task to pass the whole eventBuffer array (#198)
Browse files Browse the repository at this point in the history
* Change the add step cy.task to pass the whole array rather than step by step as each cy.task incurs an overhead

* Update index.ts

* always expect an array

* typecheck

---------

Co-authored-by: Ryan Duffy <[email protected]>
  • Loading branch information
SimeonC and ryanjduffy authored Jun 29, 2023
1 parent bd058c1 commit 4e1b6af
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
17 changes: 12 additions & 5 deletions packages/cypress/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { initMetadataFile } from "@replayio/test-utils";
import dbg from "debug";

import { TASK_NAME } from "./constants";
import CypressReporter, { getMetadataFilePath } from "./reporter";
import CypressReporter, { getMetadataFilePath, isStepEvent } from "./reporter";

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

Expand Down Expand Up @@ -55,10 +55,17 @@ const plugin: Cypress.PluginConfig = (on, config) => {
// Events are sent to the plugin by the support adapter which runs in the
// browser context and has access to `Cypress` and `cy` methods.
[TASK_NAME]: value => {
debugTask("Handling %s task: %o", TASK_NAME, value);
if (!value || typeof value !== "object") return;

cypressReporter.addStep(value);
debugTask("Handling %s task", TASK_NAME);
if (!Array.isArray(value)) return;

value.forEach(v => {
if (isStepEvent(v)) {
debugTask("Forwarding event to reporter: %o", v);
cypressReporter.addStep(v);
} else {
debugTask("Unexpected %s payload: %o", TASK_NAME, v);
}
});

return true;
},
Expand Down
15 changes: 15 additions & 0 deletions packages/cypress/src/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ import type { StepEvent } from "./support";

type Test = TestMetadataV2.Test;

function isStepEvent(value: unknown): value is StepEvent {
if (
value &&
typeof value === "object" &&
"event" in value &&
typeof value.event === "string" &&
["step:enqueue", "step:start", "step:end", "test:start", "test:end"].includes(value.event)
) {
return true;
}

return false;
}

class CypressReporter {
reporter: ReplayReporter;
config: Cypress.PluginConfigOptions;
Expand Down Expand Up @@ -153,3 +167,4 @@ export function getMetadataFilePath(workerIndex = 0) {
}

export default CypressReporter;
export { isStepEvent };
4 changes: 1 addition & 3 deletions packages/cypress/src/support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,9 +499,7 @@ export default function register() {
addAnnotation(currentTestScope, "test:end");
handleCypressEvent(currentTestScope, "test:end");

eventBuffer.forEach(arg => {
cy.task(TASK_NAME, arg, { log: false });
});
cy.task(TASK_NAME, eventBuffer, { log: false });

eventBuffer = [];
}
Expand Down

0 comments on commit 4e1b6af

Please sign in to comment.