|
| 1 | +import { Stage, Status } from "allure-js-commons"; |
| 2 | +import { InMemoryWriter, ReporterRuntime } from "allure-js-commons/sdk/reporter"; |
| 3 | +import { expect, it } from "vitest"; |
| 4 | + |
| 5 | +import { AllureCypress } from "../../src/reporter.js"; |
| 6 | +import type { AllureCypressTaskArgs, CypressMessage } from "../../src/types.js"; |
| 7 | + |
| 8 | +const SPEC_PATH = "/cypress/e2e/sample.cy.js"; |
| 9 | + |
| 10 | +const makeReporter = () => { |
| 11 | + const writer = new InMemoryWriter(); |
| 12 | + const reporter = new AllureCypress({}); |
| 13 | + reporter.allureRuntime = new ReporterRuntime({ writer }); |
| 14 | + return { reporter, writer }; |
| 15 | +}; |
| 16 | + |
| 17 | +const captureTaskHandlers = (reporter: AllureCypress) => { |
| 18 | + const tasks: Record<string, (args: AllureCypressTaskArgs) => null> = {}; |
| 19 | + reporter.attachToCypress(((event: string, handlers: Record<string, (args: AllureCypressTaskArgs) => null>) => { |
| 20 | + if (event === "task") { |
| 21 | + Object.assign(tasks, handlers); |
| 22 | + } |
| 23 | + }) as any); |
| 24 | + return tasks; |
| 25 | +}; |
| 26 | + |
| 27 | +const sendMessages = ( |
| 28 | + tasks: Record<string, (args: AllureCypressTaskArgs) => null>, |
| 29 | + messages: CypressMessage[], |
| 30 | + { isFinal = false } = {}, |
| 31 | +) => { |
| 32 | + const args: AllureCypressTaskArgs = { absolutePath: SPEC_PATH, messages, isInteractive: false }; |
| 33 | + if (isFinal) { |
| 34 | + tasks.reportFinalAllureCypressSpecMessages(args); |
| 35 | + } else { |
| 36 | + tasks.reportAllureCypressSpecMessages(args); |
| 37 | + } |
| 38 | +}; |
| 39 | + |
| 40 | +const makeRunMessages = (): CypressMessage[] => [ |
| 41 | + { type: "cypress_run_start", data: {} }, |
| 42 | + { type: "cypress_suite_start", data: { id: "root", name: "", root: true, start: 0 } }, |
| 43 | + { type: "cypress_suite_start", data: { id: "s1", name: "Suite", root: false, start: 0 } }, |
| 44 | +]; |
| 45 | + |
| 46 | +const makeTestMessages = (name: string): CypressMessage[] => [ |
| 47 | + { type: "cypress_test_start", data: { name, fullNameSuffix: name, start: 0, labels: [] } }, |
| 48 | + { type: "cypress_test_pass", data: {} }, |
| 49 | + { type: "cypress_test_end", data: { duration: 1, retries: 0 } }, |
| 50 | +]; |
| 51 | + |
| 52 | +const makeEndMessages = (): CypressMessage[] => [ |
| 53 | + { type: "cypress_suite_end", data: { root: false, stop: 1 } }, |
| 54 | + { type: "cypress_suite_end", data: { root: true, stop: 1 } }, |
| 55 | +]; |
| 56 | + |
| 57 | +it("preserves spec context when cypress_run_start is received a second time", () => { |
| 58 | + const { reporter } = makeReporter(); |
| 59 | + const tasks = captureTaskHandlers(reporter); |
| 60 | + |
| 61 | + sendMessages(tasks, makeRunMessages()); |
| 62 | + |
| 63 | + const contextAfterFirstStart = reporter.specContextByAbsolutePath.get(SPEC_PATH); |
| 64 | + expect(contextAfterFirstStart).toBeDefined(); |
| 65 | + |
| 66 | + // Simulate a second cypress_run_start for the same spec (e.g. from a cross-origin cy.visit |
| 67 | + // causing the browser-side support code to re-initialise mid-run). |
| 68 | + sendMessages(tasks, [{ type: "cypress_run_start", data: {} }]); |
| 69 | + |
| 70 | + const contextAfterSecondStart = reporter.specContextByAbsolutePath.get(SPEC_PATH); |
| 71 | + expect(contextAfterSecondStart).toBe(contextAfterFirstStart); |
| 72 | +}); |
| 73 | + |
| 74 | +it("produces exactly the expected test results when cypress_run_start is duplicated mid-run", () => { |
| 75 | + const { reporter, writer } = makeReporter(); |
| 76 | + const tasks = captureTaskHandlers(reporter); |
| 77 | + |
| 78 | + // Batch 1: run starts, first test runs and completes. |
| 79 | + sendMessages(tasks, [...makeRunMessages(), ...makeTestMessages("test one")]); |
| 80 | + |
| 81 | + // Batch 2: spurious second cypress_run_start (cross-origin scenario), then second test. |
| 82 | + sendMessages(tasks, [{ type: "cypress_run_start", data: {} }, ...makeTestMessages("test two"), ...makeEndMessages()]); |
| 83 | + |
| 84 | + // Finalise (non-interactive: endSpec is called via after:spec, simulate it directly). |
| 85 | + reporter.endSpec(SPEC_PATH); |
| 86 | + |
| 87 | + expect(writer.tests).toHaveLength(2); |
| 88 | + for (const test of writer.tests) { |
| 89 | + expect(test.status).toBe(Status.PASSED); |
| 90 | + expect(test.stage).toBe(Stage.FINISHED); |
| 91 | + } |
| 92 | + expect(writer.tests.map((t) => t.name)).toEqual(expect.arrayContaining(["test one", "test two"])); |
| 93 | +}); |
0 commit comments