@@ -36,7 +36,7 @@ export class Extension implements StringifyExtension {
36
36
out . appendLine ( "" )
37
37
38
38
// Test
39
- out . appendLine ( `test(${ JSON . stringify ( flow . title ) } , async () => {` )
39
+ out . appendLine ( `test(${ formatAsJSLiteral ( flow . title ) } , async () => {` )
40
40
out . startBlock ( )
41
41
}
42
42
@@ -52,7 +52,7 @@ export class Extension implements StringifyExtension {
52
52
out . appendLine (
53
53
`await userEvent.type(${ stringifySelector (
54
54
step . selectors [ 0 ] ,
55
- ) } , ${ JSON . stringify ( step . value ) } )`,
55
+ ) } , ${ formatAsJSLiteral ( step . value ) } )`,
56
56
)
57
57
break
58
58
case "click" :
@@ -76,12 +76,12 @@ export class Extension implements StringifyExtension {
76
76
break
77
77
case "keyDown" :
78
78
out . appendLine (
79
- `await userEvent.keyboard(${ JSON . stringify ( `{${ step . key } >}` ) } )` ,
79
+ `await userEvent.keyboard(${ formatAsJSLiteral ( `{${ step . key } >}` ) } )` ,
80
80
)
81
81
break
82
82
case "keyUp" :
83
83
out . appendLine (
84
- `await userEvent.keyboard(${ JSON . stringify ( `{/${ step . key } }` ) } )` ,
84
+ `await userEvent.keyboard(${ formatAsJSLiteral ( `{/${ step . key } }` ) } )` ,
85
85
)
86
86
break
87
87
case "navigate" :
@@ -95,7 +95,7 @@ export class Extension implements StringifyExtension {
95
95
}
96
96
if ( title ) {
97
97
out . appendLine (
98
- `expect(document.title).toBe(${ JSON . stringify ( title ) } )` ,
98
+ `expect(document.title).toBe(${ formatAsJSLiteral ( title ) } )` ,
99
99
)
100
100
}
101
101
}
@@ -121,13 +121,17 @@ export class Extension implements StringifyExtension {
121
121
}
122
122
}
123
123
124
+ function formatAsJSLiteral ( value : string ) {
125
+ return `"${ value . replace ( / " / g, '\\"' ) } "`
126
+ }
127
+
124
128
export function stringifySelector ( selector : Selector ) {
125
129
const selectorString = Array . isArray ( selector ) ? selector [ 0 ] : selector
126
130
127
131
if ( selectorString . startsWith ( "aria/" ) ) {
128
- return `screen.getByText(${ JSON . stringify ( selectorString . slice ( 5 ) ) } )`
132
+ return `screen.getByText(${ formatAsJSLiteral ( selectorString . slice ( 5 ) ) } )`
129
133
} else {
130
- return `document.querySelector(${ JSON . stringify ( selectorString ) } )`
134
+ return `document.querySelector(${ formatAsJSLiteral ( selectorString ) } )`
131
135
}
132
136
}
133
137
0 commit comments