Skip to content

Commit 6a6794b

Browse files
committed
Handle TS 6 new defaults
1 parent dbacf76 commit 6a6794b

File tree

5 files changed

+30
-15
lines changed

5 files changed

+30
-15
lines changed

playground/react-emotion/__tests__/react.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ test.runIf(isServe)('should hmr', async () => {
3232
test('should update button style', async () => {
3333
function getButtonBorderStyle() {
3434
return page.evaluate(() => {
35-
return window.getComputedStyle(document.querySelector('button')).border
35+
return window.getComputedStyle(document.querySelector('button')!).border
3636
})
3737
}
3838

3939
await page.evaluate(() => {
40-
return document.querySelector('button').style
40+
return document.querySelector('button')!.style
4141
})
4242

4343
expect(await getButtonBorderStyle()).toMatch('2px solid rgb(0, 0, 0)')

playground/test-utils.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function rgbToHex(rgb: string): string {
3939

4040
async function toEl(el: string | ElementHandle): Promise<ElementHandle> {
4141
if (typeof el === 'string') {
42-
return await page.$(el)
42+
return (await page.$(el))!
4343
}
4444
return el
4545
}
@@ -117,14 +117,9 @@ async function untilBrowserLog(
117117
target?: string | RegExp | Array<string | RegExp>,
118118
expectOrder = true,
119119
): Promise<string[]> {
120-
let resolve: () => void
121-
let reject: (reason: any) => void
122-
const promise = new Promise<void>((_resolve, _reject) => {
123-
resolve = _resolve
124-
reject = _reject
125-
})
120+
const { promise, resolve, reject } = promiseWithResolvers<void>()
126121

127-
const logs = []
122+
const logs: string[] = []
128123

129124
try {
130125
const isMatch = (matcher: string | RegExp) => (text: string) =>
@@ -139,6 +134,9 @@ async function untilBrowserLog(
139134
const remainingTargets = [...target]
140135
processMsg = (text: string) => {
141136
const nextTarget = remainingTargets.shift()
137+
if (!nextTarget) {
138+
expect.fail('Received more logs than expected. Extra log: ' + text)
139+
}
142140
expect(text).toMatch(nextTarget)
143141
return remainingTargets.length === 0
144142
}
@@ -186,6 +184,22 @@ export function escapeRegex(str: string): string {
186184
return str.replace(escapeRegexRE, '\\$&')
187185
}
188186

187+
// TODO: Replace with Promise.withResolvers when bumping to Node 22
188+
export interface PromiseWithResolvers<T> {
189+
promise: Promise<T>
190+
resolve: (value: T | PromiseLike<T>) => void
191+
reject: (reason?: any) => void
192+
}
193+
export function promiseWithResolvers<T>(): PromiseWithResolvers<T> {
194+
let resolve: any
195+
let reject: any
196+
const promise = new Promise<T>((_resolve, _reject) => {
197+
resolve = _resolve
198+
reject = _reject
199+
})
200+
return { promise, resolve, reject }
201+
}
202+
189203
/**
190204
* Before implementing a new util, check if it's not available in core https://github.com/vitejs/vite/blob/main/playground/test-utils.ts
191205
*/

playground/tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
"target": "ES2023",
66
"module": "preserve",
77
"outDir": "dist",
8-
"baseUrl": ".",
98
"allowJs": true,
109
"esModuleInterop": true,
1110
"resolveJsonModule": true,

playground/vitestSetup.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,10 @@ export function setViteUrl(url: string): void {
8181
// eslint-disable-next-line no-empty-pattern
8282
beforeAll(async ({}, suite) => {
8383
testPath = suite.file.filepath!
84-
testName = slash(testPath).match(/playground\/([\w-]+)\//)?.[1]
84+
const playgroundDir = slash(testPath).match(/playground\/([\w-]+)\//)?.[1]
8585
testDir = dirname(testPath)
86-
if (testName) {
86+
if (playgroundDir) {
87+
testName = playgroundDir
8788
testDir = path.resolve(workspaceRoot, 'playground-temp', testName)
8889
}
8990

@@ -231,7 +232,7 @@ export async function startDefaultServe(): Promise<void> {
231232
const config = await loadConfig({ command: 'serve', mode: 'development' })
232233
viteServer = server = await (await createServer(config)).listen()
233234
viteTestUrl = stripTrailingSlashIfNeeded(
234-
server.resolvedUrls.local[0],
235+
server.resolvedUrls!.local[0],
235236
server.config.base,
236237
)
237238
await page.goto(viteTestUrl)
@@ -277,7 +278,7 @@ export async function startDefaultServe(): Promise<void> {
277278
// prevent preview change NODE_ENV
278279
process.env.NODE_ENV = _nodeEnv
279280
viteTestUrl = stripTrailingSlashIfNeeded(
280-
previewServer.resolvedUrls.local[0],
281+
previewServer.resolvedUrls!.local[0],
281282
previewServer.config.base,
282283
)
283284
await page.goto(viteTestUrl)

scripts/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"$schema": "https://json.schemastore.org/tsconfig",
33
"include": ["."],
44
"compilerOptions": {
5+
"types": ["node"],
56
"target": "esnext",
67
"module": "nodenext",
78
"allowImportingTsExtensions": true,

0 commit comments

Comments
 (0)