Skip to content

Commit f4f161f

Browse files
committed
Use @testing-library/dom to test HTML reports
Recent changes to `@cucumber/react-components` [1] makes rendering async. This is a good opportunity to switch to a library providing query polling. [1] cucumber/react-components#337
1 parent 531839b commit f4f161f

File tree

2 files changed

+29
-38
lines changed

2 files changed

+29
-38
lines changed

features/step_definitions/html_steps.ts

Lines changed: 28 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { JSDOM } from "jsdom";
33
import path from "path";
44
import { promises as fs } from "fs";
55
import assert from "assert";
6-
import { assertAndReturn } from "../support/helpers";
6+
import { findByText } from "@testing-library/dom";
77
import ICustomWorld from "../support/ICustomWorld";
88

99
Then("there should be a HTML report", async function (this: ICustomWorld) {
@@ -21,24 +21,19 @@ Then(
2121
{ runScripts: "dangerously" }
2222
);
2323

24-
const dt = assertAndReturn(
25-
Array.from(dom.window.document.querySelectorAll("dt")).find(
26-
(el) => el.textContent === "last run"
27-
),
28-
"Expected to find a 'last run' dt"
24+
const dt = await findByText(
25+
dom.window.document.documentElement,
26+
"last run",
27+
{
28+
selector: "dt",
29+
}
2930
);
3031

31-
const dd = assertAndReturn(
32-
dt.parentElement?.querySelector("dd"),
33-
"Expected to find a 'last run' dt's dd"
34-
);
35-
36-
const lastRunText = assertAndReturn(
37-
dd.textContent,
38-
"Expected to find 'XX seconds ago'"
39-
);
32+
const dd = await findByText(dt.parentElement!, /\d+ seconds? ago/, {
33+
selector: "dd",
34+
});
4035

41-
assert.match(lastRunText, /\d+ seconds? ago/);
36+
assert(dd);
4237
}
4338
);
4439

@@ -50,11 +45,12 @@ Then(
5045
{ runScripts: "dangerously" }
5146
);
5247

53-
const dt = assertAndReturn(
54-
Array.from(dom.window.document.querySelectorAll("dt")).find(
55-
(el) => el.textContent && /\d+ executed/.test(el.textContent)
56-
),
57-
"Expected to find a 'XX executed' dt"
48+
const dt = await findByText(
49+
dom.window.document.documentElement,
50+
/\d+ executed/,
51+
{
52+
selector: "dt",
53+
}
5854
);
5955

6056
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
@@ -72,11 +68,12 @@ Then(
7268
{ runScripts: "dangerously" }
7369
);
7470

75-
const dd = assertAndReturn(
76-
Array.from(dom.window.document.querySelectorAll("dd")).find(
77-
(el) => el.textContent && /\d+% passed/.test(el.textContent)
78-
),
79-
"Expected to find a 'XX% passed' dd"
71+
const dd = await findByText(
72+
dom.window.document.documentElement,
73+
/\d+% passed/,
74+
{
75+
selector: "dd",
76+
}
8077
);
8178

8279
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
@@ -94,19 +91,12 @@ Then(
9491
{ runScripts: "dangerously" }
9592
);
9693

97-
const AccordionItemPanel = assertAndReturn(
98-
dom.window.document.querySelector(
99-
'[data-accordion-component="AccordionItemPanel"]'
100-
),
101-
"Expected to find an AccordionItemPanel"
94+
const AccordionItemPanel = await findByText(
95+
dom.window.document.documentElement,
96+
(_, element) => element?.textContent?.includes("Attached Image") ?? false,
97+
{ selector: '[data-accordion-component="AccordionItemPanel"]' }
10298
);
10399

104-
assert.match(
105-
assertAndReturn(
106-
AccordionItemPanel.textContent,
107-
"Expected AccordionItemPanel to have textContent"
108-
),
109-
/Attached Image/
110-
);
100+
assert(AccordionItemPanel);
111101
}
112102
);

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
"@bahmutov/cypress-esbuild-preprocessor": "^2.2.0",
9090
"@cypress/browserify-preprocessor": "^3.0.2",
9191
"@cypress/webpack-preprocessor": "^6.0.1",
92+
"@testing-library/dom": "^10.0.0",
9293
"@types/cli-table": "^0.3.4",
9394
"@types/common-ancestor-path": "^1.0.2",
9495
"@types/debug": "^4.1.12",

0 commit comments

Comments
 (0)