Skip to content

fix(allure-cypress): don't reset spec context on duplicate cypress_run_start#1503

Open
todti wants to merge 4 commits into
mainfrom
fix/cypress-double-run-start
Open

fix(allure-cypress): don't reset spec context on duplicate cypress_run_start#1503
todti wants to merge 4 commits into
mainfrom
fix/cypress-double-run-start

Conversation

@todti
Copy link
Copy Markdown
Contributor

@todti todti commented May 29, 2026

Problem

Closes #1329.

When a Cypress spec uses cy.visit to navigate to multiple URLs (especially cross-origin), Allure reports some tests as skipped / pending even though they passed in Cypress. Extra result files with pending content also appear.

Root cause

The bug lives in AllureCypress#startRun.

When a spec triggers a cross-origin navigation, Cypress can re-initialise the browser-side support code in the secondary origin context. If Cypress.env("allure") is absent there, initializeAllure() runs again, re-registers Mocha event listeners, and the Mocha runner emits another "start" event. This enqueues a second cypress_run_start message for the same spec path while the first context is still live.

Previously #startRun unconditionally called #initializeSpecContext, which overwrites the existing SpecContext with a fresh empty one. All in-progress test state (open scopes, test UUID, suite names) is lost. Subsequent lifecycle messages (cypress_test_end, cypress_suite_end) that arrive after the reset are applied to the new empty context, and any tests that were already started never receive a proper stop message — leaving them in Stage.RUNNING, which Allure renders as skipped/pending.

Fix

Skip re-initialisation when the context for the spec path already exists:

#startRun = (absolutePath: string) => {
  if (!this.specContextByAbsolutePath.has(absolutePath)) {
    this.#initializeSpecContext(absolutePath);
  }
};

cypress open (interactive) is unaffected. When the user re-runs a spec, endSpec has already deleted the context before the next cypress_run_start arrives, so the guard evaluates to false and a fresh context is created as before.

Testing

Two new unit tests in doubleRunStart.test.ts:

  1. preserves spec context when cypress_run_start is received a second time — confirms the context object is not replaced when a duplicate message arrives mid-run. This test fails on the unpatched code and passes with the fix, acting as a regression guard.
  2. creates a fresh context for a spec that has already finished (interactive re-run) — confirms the interactive re-run path still works: after endSpec removes the context, a new cypress_run_start correctly creates a fresh one.

…n_start

When a spec navigates to a cross-origin URL via cy.visit, Cypress can
re-initialise the browser-side support code in the secondary origin
context. If the Allure env is not yet present there, initializeAllure()
re-registers Mocha event listeners and the Mocha runner fires another
"start" event, which enqueues a second cypress_run_start message for
the same spec path.

Previously #startRun always called #initializeSpecContext, throwing
away the live context and all accumulated test state. This left
in-progress tests without a corresponding stop message, resulting in
duplicate or pending/skipped result files in the Allure report even
though the tests actually passed in Cypress.

Fix: skip re-initialisation when the context already exists.  In
cypress open the spec context is deleted by endSpec before the user
can trigger a re-run, so the guard does not affect that mode.

Fixes: #1329
@todti todti force-pushed the fix/cypress-double-run-start branch from 3c23fa9 to 20b2d8b Compare May 29, 2026 15:52
@todti todti mentioned this pull request May 29, 2026
@todti todti added type:bug Something isn't working theme:cypress labels May 29, 2026
todti and others added 2 commits May 29, 2026 17:36
…start guard

Add 10 more targeted unit tests grouped by concern:

- context identity: same object, idempotent across many duplicates, videoScope
  and suite scopes preserved, mid-batch duplicate handled correctly
- test results: exactly the expected results are written, no phantom pending
  results created
- spec path isolation: duplicate for spec A does not touch spec B
- reportFinalAllureCypressSpecMessages: endSpec not called in non-interactive
  mode, called in interactive mode
- interactive re-run: fresh context and new videoScope after endSpec

The two tests that drive the full test lifecycle (cypress_test_start → end)
need several filesystem/env helpers to be stubbed out; a vi.mock at the top
of the file provides lightweight stand-ins so the reporter under test can
call #addNewTestResult without hitting getProjectName() etc. at call time.
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 29, 2026

Allure Report Summary

Name Duration Stats New Flaky Retry Report
Allure JS – Pull request #1503 2h 2m Passed tests 4451   Failed tests 1 36 0 0 View

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

theme:cypress type:bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug with two cy.visit

1 participant