Skip to content

Commit 63926ed

Browse files
committed
Flush messages continuously
This allows @cucumber/query (built into @cucumber/html-formatter) to maintain state [1] and fixes #931 [2]. [1] https://github.com/cucumber/query/blob/v12.0.1/javascript/src/Query.ts#L46-L59 [2] #931
1 parent afbdd81 commit 63926ed

File tree

3 files changed

+107
-18
lines changed

3 files changed

+107
-18
lines changed

features/issues/931.feature

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# https://github.com/badeball/cypress-cucumber-preprocessor/issues/931
2+
3+
Feature: screenshot of last, failed attempt
4+
Background:
5+
Given additional preprocessor configuration
6+
"""
7+
{
8+
"html": {
9+
"enabled": true
10+
}
11+
}
12+
"""
13+
And a file named "cypress/e2e/a.feature" with:
14+
"""
15+
Feature: a feature
16+
Scenario: a scenario
17+
Given a failing step
18+
"""
19+
And a file named "cypress/support/step_definitions/steps.js" with:
20+
"""
21+
const { Given } = require("@badeball/cypress-cucumber-preprocessor");
22+
Given("a failing step", function() {
23+
throw new Error("foobar")
24+
})
25+
"""
26+
27+
Scenario: retries: 0
28+
When I run cypress
29+
Then it fails
30+
And the report should have an image attachment
31+
32+
Scenario: retries: 1
33+
Given additional Cypress configuration
34+
"""
35+
{
36+
"retries": 1
37+
}
38+
"""
39+
When I run cypress
40+
Then it fails
41+
# «Still have», despite having been retried
42+
And the report should have an image attachment

features/step_definitions/html_steps.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ import path from "path";
44
import { promises as fs } from "fs";
55
import assert from "assert";
66

7+
function assertAndReturn<T>(value: T | null | undefined, msg?: string): T {
8+
assert(value, msg);
9+
return value;
10+
}
11+
712
Then("there should be a HTML report", async function () {
813
await assert.doesNotReject(
914
() => fs.access(path.join(this.tmpDir, "cucumber-report.html")),
@@ -33,3 +38,36 @@ Then("the report should display when last run", async function () {
3338

3439
assert.match(lastRunText, /\d+ seconds? ago/);
3540
});
41+
42+
Then("the report should have an image attachment", async function () {
43+
const dom = await JSDOM.fromFile(
44+
path.join(this.tmpDir, "cucumber-report.html"),
45+
{ runScripts: "dangerously" }
46+
);
47+
48+
const AccordionItemButton = assertAndReturn(
49+
dom.window.document.querySelector(
50+
'[data-accordion-component="AccordionItemButton"]'
51+
),
52+
"Expected to find an AccordionItemButton"
53+
);
54+
55+
assert(AccordionItemButton instanceof dom.window.HTMLElement);
56+
57+
AccordionItemButton.click();
58+
59+
const AccordionItemPanel = assertAndReturn(
60+
dom.window.document.querySelector(
61+
'[data-accordion-component="AccordionItemPanel"]'
62+
),
63+
"Expected to find an AccordionItemPanel"
64+
);
65+
66+
assert.match(
67+
assertAndReturn(
68+
AccordionItemPanel.textContent,
69+
"Expected AccordionItemPanel to have textContent"
70+
),
71+
/Attached Image/
72+
);
73+
});

lib/create-tests.ts

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,16 @@ function retrieveInternalSuiteProperties():
145145
return Cypress.env(INTERNAL_SUITE_PROPERTIES);
146146
}
147147

148+
function flushMessages(messages: CompositionContext["messages"]) {
149+
if (messages.enabled) {
150+
cy.task(
151+
TASK_APPEND_MESSAGES,
152+
messages.stack.splice(0, messages.stack.length),
153+
{ log: false }
154+
);
155+
}
156+
}
157+
148158
function findPickleById(context: CompositionContext, astId: string) {
149159
return assertAndReturn(
150160
context.pickles.find(
@@ -372,6 +382,8 @@ function createPickle(
372382
},
373383
});
374384

385+
flushMessages(context.messages);
386+
375387
window.testState = {
376388
gherkinDocument,
377389
pickles,
@@ -652,22 +664,21 @@ export default function createTests(
652664
? parse(environmentTags)
653665
: noopNode;
654666

667+
const context: CompositionContext = {
668+
registry,
669+
gherkinDocument,
670+
pickles,
671+
testFilter,
672+
omitFiltered,
673+
messages: {
674+
enabled: messagesEnabled,
675+
stack: messages,
676+
},
677+
stepDefinitionHints,
678+
};
679+
655680
if (gherkinDocument.feature) {
656-
createFeature(
657-
{
658-
registry,
659-
gherkinDocument,
660-
pickles,
661-
testFilter,
662-
omitFiltered,
663-
messages: {
664-
enabled: messagesEnabled,
665-
stack: messages,
666-
},
667-
stepDefinitionHints,
668-
},
669-
gherkinDocument.feature
670-
);
681+
createFeature(context, gherkinDocument.feature);
671682
}
672683

673684
const isHooksAttached = globalThis[INTERNAL_PROPERTY_NAME];
@@ -806,9 +817,7 @@ export default function createTests(
806817
});
807818

808819
after(function () {
809-
if (messagesEnabled) {
810-
cy.task(TASK_APPEND_MESSAGES, messages, { log: false });
811-
}
820+
flushMessages(context.messages);
812821
});
813822
}
814823

0 commit comments

Comments
 (0)