Skip to content

Commit

Permalink
test: add restart test
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Jul 13, 2022
1 parent a22277c commit 0473264
Show file tree
Hide file tree
Showing 5 changed files with 248 additions and 89 deletions.
76 changes: 54 additions & 22 deletions tests/cases/backend-server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test, expect } from '@playwright/test'
import type { ChildProcessWithoutNullStreams } from 'child_process'
import type { Page } from '@playwright/test'
import { spawn } from 'cross-spawn'
import {
editFile,
Expand All @@ -14,13 +14,12 @@ import {
const workspaceFileURL = getWorkspaceFileURL('backend-server')
const accessURL = `http://localhost:${ports.backendServer}/`

let viteDevProcess: ChildProcessWithoutNullStreams
let backendProcess: ChildProcessWithoutNullStreams

test.beforeAll(async () => {
const startVite = async () => {
// pnpm run dev cannot be used because killing process does not work
viteDevProcess = spawn('pnpm', ['run', 'dev:vite'], { cwd: workspaceFileURL })
backendProcess = spawn('pnpm', ['run', 'dev:backend'], {
const viteDevProcess = spawn('pnpm', ['run', 'dev:vite'], {
cwd: workspaceFileURL
})
const backendProcess = spawn('pnpm', ['run', 'dev:backend'], {
cwd: workspaceFileURL
})
await Promise.all([
Expand All @@ -35,32 +34,65 @@ test.beforeAll(async () => {
'Open your browser.'
)
])
})

test('backend-server test', async ({ page }) => {
return async () => {
try {
await killProcess(viteDevProcess)
} catch {}
try {
await killProcess(backendProcess)
} catch {}
}
}

const setupAndGotoPage = async (page: Page) => {
outputError(page)
await gotoAndWaitForHMRConnection(page, accessURL, { timeout: 10000 })
}

const title = page.locator('h1')
await expect(title).toHaveText('Hello Vite!')
test('hmr test', async ({ page }) => {
const finishVite = await startVite()
try {
await setupAndGotoPage(page)

await editFile('./frontend-src/main.js', workspaceFileURL, (content) =>
content.replace('Vite!</h1>', 'Vite!!!</h1>')
)
const title = page.locator('h1')
await expect(title).toHaveText('Hello Vite!')

await expect(title).toHaveText('Hello Vite!!!')
await editFile('./frontend-src/main.js', workspaceFileURL, (content) =>
content.replace('Vite!</h1>', 'Vite!!!</h1>')
)

await expect(title).toHaveText('Hello Vite!!!')
} finally {
await finishVite()
}
})

test('restart test', async ({ page }) => {
let finishVite1: (() => Promise<void>) | undefined
let finishVite2: (() => Promise<void>) | undefined

try {
finishVite1 = await startVite()
await setupAndGotoPage(page)

const navigationPromise = page.waitForNavigation()

await finishVite1()
finishVite1 = undefined

finishVite2 = await startVite()

await navigationPromise
} finally {
await finishVite1?.()
await finishVite2?.()
}
})

test.afterAll(async () => {
// cleanup
await editFile('./frontend-src/main.js', workspaceFileURL, (content) =>
content.replace('Vite!!!</h1>', 'Vite!</h1>')
)

try {
await killProcess(viteDevProcess)
} catch {}
try {
await killProcess(backendProcess)
} catch {}
})
66 changes: 49 additions & 17 deletions tests/cases/basic.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test, expect } from '@playwright/test'
import type { ChildProcessWithoutNullStreams } from 'child_process'
import type { Page } from '@playwright/test'
import { spawn } from 'cross-spawn'
import {
editFile,
Expand All @@ -14,38 +14,70 @@ import {
const workspaceFileURL = getWorkspaceFileURL('basic')
const accessURL = `http://localhost:${ports.basic}/`

let viteDevProcess: ChildProcessWithoutNullStreams

test.beforeAll(async () => {
viteDevProcess = spawn('pnpm', ['run', 'dev'], { cwd: workspaceFileURL })
const startVite = async () => {
const viteDevProcess = spawn('pnpm', ['run', 'dev'], {
cwd: workspaceFileURL
})
await collectAndWaitUntilOutput(
viteDevProcess.stdout,
viteDevProcess.stderr,
'use --host to expose'
)
})
return async () => {
try {
await killProcess(viteDevProcess)
} catch {}
}
}

test('basic test', async ({ page }) => {
const setupAndGotoPage = async (page: Page) => {
outputError(page)
await gotoAndWaitForHMRConnection(page, accessURL, { timeout: 10000 })
}

const title = page.locator('h1')
await expect(title).toHaveText('Hello Vite!')
test('hmr test', async ({ page }) => {
const finishVite = await startVite()
try {
await setupAndGotoPage(page)

await editFile('./main.js', workspaceFileURL, (content) =>
content.replace('Vite!</h1>', 'Vite!!!</h1>')
)
const title = page.locator('h1')
await expect(title).toHaveText('Hello Vite!')

await editFile('./main.js', workspaceFileURL, (content) =>
content.replace('Vite!</h1>', 'Vite!!!</h1>')
)

await expect(title).toHaveText('Hello Vite!!!')
} finally {
await finishVite()
}
})

await expect(title).toHaveText('Hello Vite!!!')
test('restart test', async ({ page }) => {
let finishVite1: (() => Promise<void>) | undefined
let finishVite2: (() => Promise<void>) | undefined

try {
finishVite1 = await startVite()
await setupAndGotoPage(page)

const navigationPromise = page.waitForNavigation()

await finishVite1()
finishVite1 = undefined

finishVite2 = await startVite()

await navigationPromise
} finally {
await finishVite1?.()
await finishVite2?.()
}
})

test.afterAll(async () => {
// cleanup
await editFile('./main.js', workspaceFileURL, (content) =>
content.replace('Vite!!!</h1>', 'Vite!</h1>')
)

try {
await killProcess(viteDevProcess)
} catch {}
})
69 changes: 51 additions & 18 deletions tests/cases/middleware-mode.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test, expect } from '@playwright/test'
import type { ChildProcessWithoutNullStreams } from 'child_process'
import type { Page } from '@playwright/test'
import { spawn } from 'cross-spawn'
import {
editFile,
Expand All @@ -14,38 +14,71 @@ import {
const workspaceFileURL = getWorkspaceFileURL('middleware-mode')
const accessURL = `http://localhost:${ports.middlewareMode}/`

let viteDevProcess: ChildProcessWithoutNullStreams

test.beforeAll(async () => {
viteDevProcess = spawn('pnpm', ['run', 'dev'], { cwd: workspaceFileURL })
const startVite = async () => {
const viteDevProcess = spawn('pnpm', ['run', 'dev'], {
cwd: workspaceFileURL
})
await collectAndWaitUntilOutput(
viteDevProcess.stdout,
viteDevProcess.stderr,
'Open your browser.'
)
})

test('middleware-mode test', async ({ page }) => {
return async () => {
try {
await killProcess(viteDevProcess)
} catch {}
}
}

const setupAndGotoPage = async (page: Page) => {
outputError(page)
await gotoAndWaitForHMRConnection(page, accessURL, { timeout: 10000 })
await gotoAndWaitForHMRConnection(page, accessURL, { timeout: 1000 })
}

const title = page.locator('h1')
await expect(title).toHaveText('Hello Vite!')
test('hmr test', async ({ page }) => {
const finishVite = await startVite()
try {
await setupAndGotoPage(page)

await editFile('./src/main.js', workspaceFileURL, (content) =>
content.replace('Vite!</h1>', 'Vite!!!</h1>')
)
const title = page.locator('h1')
await expect(title).toHaveText('Hello Vite!')

await editFile('./src/main.js', workspaceFileURL, (content) =>
content.replace('Vite!</h1>', 'Vite!!!</h1>')
)

await expect(title).toHaveText('Hello Vite!!!')
await expect(title).toHaveText('Hello Vite!!!')
} finally {
await finishVite()
}
})

test('restart test', async ({ page }) => {
let finishVite1: (() => Promise<void>) | undefined
let finishVite2: (() => Promise<void>) | undefined

try {
finishVite1 = await startVite()
await setupAndGotoPage(page)

const navigationPromise = page.waitForNavigation()

await finishVite1()
finishVite1 = undefined

finishVite2 = await startVite()

await navigationPromise
} finally {
await finishVite1?.()
await finishVite2?.()
}
})

test.afterAll(async () => {
// cleanup
await editFile('./src/main.js', workspaceFileURL, (content) =>
content.replace('Vite!!!</h1>', 'Vite!</h1>')
)

try {
await killProcess(viteDevProcess)
} catch {}
})
63 changes: 47 additions & 16 deletions tests/cases/with-proxy-no-websocket.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test, expect } from '@playwright/test'
import type { DockerComposeProcess } from '../utils/index.js'
import type { Page } from '@playwright/test'
import {
editFile,
getWorkspaceFileURL,
Expand All @@ -14,14 +14,12 @@ import {
const workspaceFileURL = getWorkspaceFileURL('with-proxy-no-websocket')
const accessURL = `http://localhost:${ports.withProxyNoWebSocket}/`

let dockerComposeProcess: DockerComposeProcess

test.beforeAll(async () => {
const startVite = async () => {
const overrideFile = useNodeModulesOutsideContainer
? ' -f ../../tests/fixtures/compose.with-proxy-no-websocket.yaml'
: ''

dockerComposeProcess = runDockerCompose(
const dockerComposeProcess = runDockerCompose(
`-p with-proxy-no-websocket-dev -f compose.dev.yaml${overrideFile}`,
workspaceFileURL
)
Expand All @@ -31,20 +29,55 @@ test.beforeAll(async () => {
'Network:',
{ timeout: 20000 }
)
})

test('with-proxy-no-websocket test', async ({ page }) => {
return async () => {
await dockerComposeProcess.down()
}
}

const setupAndGotoPage = async (page: Page) => {
outputError(page)
await gotoAndWaitForHMRConnection(page, accessURL, { timeout: 10000 })
await gotoAndWaitForHMRConnection(page, accessURL, { timeout: 1000 })
}

const title = page.locator('h1')
await expect(title).toHaveText('Hello Vite!')
test('hmr test', async ({ page }) => {
const finishVite = await startVite()
try {
await setupAndGotoPage(page)

await editFile('./src/main.js', workspaceFileURL, (content) =>
content.replace('Vite!</h1>', 'Vite!!!</h1>')
)
const title = page.locator('h1')
await expect(title).toHaveText('Hello Vite!')

await editFile('./src/main.js', workspaceFileURL, (content) =>
content.replace('Vite!</h1>', 'Vite!!!</h1>')
)

await expect(title).toHaveText('Hello Vite!!!')
await expect(title).toHaveText('Hello Vite!!!')
} finally {
await finishVite()
}
})

test('restart test', async ({ page }) => {
let finishVite1: (() => Promise<void>) | undefined
let finishVite2: (() => Promise<void>) | undefined

try {
finishVite1 = await startVite()
await setupAndGotoPage(page)

const navigationPromise = page.waitForNavigation()

await finishVite1()
finishVite1 = undefined

finishVite2 = await startVite()

await navigationPromise
} finally {
await finishVite1?.()
await finishVite2?.()
}
})

test.afterAll(async ({}, testInfo) => {
Expand All @@ -56,6 +89,4 @@ test.afterAll(async ({}, testInfo) => {
await editFile('./src/main.js', workspaceFileURL, (content) =>
content.replace('Vite!!!</h1>', 'Vite!</h1>')
)

await dockerComposeProcess.down()
})

0 comments on commit 0473264

Please sign in to comment.