Skip to content

Commit ae79f98

Browse files
committed
Initial stringify extension implementation
1 parent 49eb2a1 commit ae79f98

File tree

3 files changed

+100
-16
lines changed

3 files changed

+100
-16
lines changed

src/fixtures/Example.json

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,11 @@
2222
]
2323
},
2424
{
25-
"type": "click",
26-
"target": "main",
25+
"type": "waitForElement",
26+
"frame": [],
2727
"selectors": [
2828
["aria/More information..."],
2929
["body > div > p:nth-child(3) > a"]
30-
],
31-
"offsetY": 0,
32-
"offsetX": 0,
33-
"assertedEvents": [
34-
{
35-
"type": "navigation",
36-
"url": "https://www.iana.org/domains/reserved",
37-
"title": ""
38-
}
3930
]
4031
}
4132
]

src/fixtures/example.test.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
* @jest-environment url
33
* @jest-environment-options { "url": "https://example.com/" }
44
*/
5-
const { screen } = require("@testing-library/dom")
5+
const { screen, waitFor } = require("@testing-library/dom")
66
require("@testing-library/jest-dom")
77

8-
test("Example", () => {
8+
test("Example", async () => {
99
expect(location.href).toBe("https://example.com/")
10-
expect(screen.getByText("Example Domain")).toBeInTheDocument()
10+
expect(document.title).toBe("Example Domain")
11+
await waitFor(() => screen.getByText("More information..."))
1112
})

src/index.ts

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,98 @@
1-
import { stringify, StringifyExtension, type UserFlow } from "@puppeteer/replay"
1+
import {
2+
Selector,
3+
stringify,
4+
StringifyExtension,
5+
type LineWriter,
6+
type NavigateStep,
7+
type Step,
8+
type UserFlow,
9+
} from "@puppeteer/replay"
210

3-
export class Extension extends StringifyExtension {}
11+
export class Extension extends StringifyExtension {
12+
async beforeAllSteps(out: LineWriter, flow: UserFlow) {
13+
// Jest docblock
14+
out.appendLine("/**")
15+
const step = flow.steps.find((step) => step.type === "navigate") as
16+
| NavigateStep
17+
| undefined
18+
if (step) {
19+
out.appendLine(" * @jest-environment url")
20+
out.appendLine(` * @jest-environment-options { "url": "${step.url}" }`)
21+
} else {
22+
out.appendLine(" * @jest-environment jsdom")
23+
}
24+
out.appendLine(" */")
25+
26+
// Imports
27+
out.appendLine(
28+
'const { screen, waitFor } = require("@testing-library/dom")',
29+
)
30+
out.appendLine('require("@testing-library/jest-dom")')
31+
32+
out.appendLine("")
33+
34+
// Test
35+
out.appendLine(`test(${JSON.stringify(flow.title)}, async () => {`)
36+
out.startBlock()
37+
}
38+
39+
async afterAllSteps(out: LineWriter) {
40+
// Test
41+
out.endBlock()
42+
out.appendLine("})")
43+
}
44+
45+
stringifySelector(selector: Selector) {
46+
const selectorString = Array.isArray(selector) ? selector[0] : selector
47+
48+
if (selectorString.startsWith("aria/")) {
49+
return `screen.getByText("${selectorString.slice(5)}")`
50+
} else {
51+
return `document.querySelector("${selectorString}")`
52+
}
53+
}
54+
55+
async stringifyStep(out: LineWriter, step: Step, flow: UserFlow) {
56+
// Events
57+
switch (step.type) {
58+
case "click":
59+
out.appendLine(
60+
`userEvent.click(${this.stringifySelector(step.selectors[0])}")`,
61+
)
62+
break
63+
case "waitForElement":
64+
out.appendLine(
65+
`await waitFor(() => ${this.stringifySelector(step.selectors[0])})`,
66+
)
67+
break
68+
case "navigate":
69+
case "setViewport":
70+
break
71+
default:
72+
console.log(
73+
`Warning: Testing Library does not currently handle migrating steps of type: ${step.type}. Please check the output to see how this might affect your test.`,
74+
)
75+
}
76+
77+
// Assertions
78+
if (step.type === "navigate") {
79+
if (step === flow.steps.find((step) => step.type === "navigate")) {
80+
for (const { url, title } of step.assertedEvents ?? []) {
81+
if (url) {
82+
out.appendLine(`expect(location.href).toBe("${url}")`)
83+
}
84+
if (title) {
85+
out.appendLine(`expect(document.title).toBe("${title}")`)
86+
}
87+
}
88+
} else {
89+
console.log(
90+
"Warning: Testing Library does not currently handle more than one navigation step per test.",
91+
)
92+
}
93+
}
94+
}
95+
}
496

597
if (process.env.NODE_ENV !== "test") {
698
class RecorderPlugin {

0 commit comments

Comments
 (0)