Skip to content

Commit

Permalink
test(server): vite server restart flow (vitejs#12777)
Browse files Browse the repository at this point in the history
  • Loading branch information
sun0day committed Apr 25, 2023
1 parent 061e48b commit ef7b720
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 11 deletions.
17 changes: 15 additions & 2 deletions playground/cli/__tests__/cli.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, test } from 'vitest'
import { port } from './serve'
import { page } from '~utils'
import { port, streams } from './serve'
import { editFile, page, withRetry } from '~utils'

test('cli should work', async () => {
// this test uses a custom serve implementation, so regular helpers for browserLogs and goto don't work
Expand All @@ -12,9 +12,22 @@ test('cli should work', async () => {
try {
page.on('console', onConsole)
await page.goto(`http://localhost:${port}/`)

expect(await page.textContent('.app')).toBe('vite cli works!')
expect(logs.some((msg) => msg.match('vite cli works!'))).toBe(true)
} finally {
page.off('console', onConsole)
}
})

test('should restart', async () => {
editFile('./vite.config.js', (content) => content)
await withRetry(async () => {
expect(streams.server.out).toEqual(
expect.arrayContaining([expect.stringMatching('server restarted')]),
)
expect(streams.server.out).not.toEqual(
expect.arrayContaining([expect.stringMatching('error')]),
)
})
})
9 changes: 6 additions & 3 deletions playground/cli/__tests__/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ import {
} from '~utils'

export const port = ports.cli

export const streams = {} as {
build: { out: string[]; err: string[] }
server: { out: string[]; err: string[] }
}
export async function serve() {
// collect stdout and stderr streams from child processes here to avoid interfering with regular vitest output
const streams = {
Object.assign(streams, {
build: { out: [], err: [] },
server: { out: [], err: [] },
}
})
// helpers to collect streams
const collectStreams = (name, process) => {
process.stdout.on('data', (d) => streams[name].out.push(d.toString()))
Expand Down
10 changes: 8 additions & 2 deletions playground/ssr/__tests__/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@

import path from 'node:path'
import kill from 'kill-port'
import { hmrPorts, ports, rootDir } from '~utils'
import { createInMemoryLogger, hmrPorts, ports, rootDir } from '~utils'

export const port = ports.ssr

export const serverLogs = []

export async function serve(): Promise<{ close(): Promise<void> }> {
await kill(port)

const { createServer } = await import(path.resolve(rootDir, 'server.js'))
const { app, vite } = await createServer(rootDir, hmrPorts.ssr)
const { app, vite } = await createServer(
rootDir,
hmrPorts.ssr,
createInMemoryLogger(serverLogs),
)

return new Promise((resolve, reject) => {
try {
Expand Down
16 changes: 14 additions & 2 deletions playground/ssr/__tests__/ssr.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, test } from 'vitest'
import { port } from './serve'
import { page } from '~utils'
import { port, serverLogs } from './serve'
import { editFile, page, withRetry } from '~utils'

const url = `http://localhost:${port}`

Expand All @@ -17,3 +17,15 @@ test(`deadlock doesn't happen`, async () => {

expect(await page.textContent('.forked-deadlock')).toMatch('rendered')
})

test('should restart ssr', async () => {
editFile('./vite.config.ts', (content) => content)
await withRetry(async () => {
expect(serverLogs).toEqual(
expect.arrayContaining([expect.stringMatching('server restarted')]),
)
expect(serverLogs).not.toEqual(
expect.arrayContaining([expect.stringMatching('error')]),
)
})
})
7 changes: 6 additions & 1 deletion playground/ssr/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import express from 'express'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const isTest = process.env.VITEST

export async function createServer(root = process.cwd(), hmrPort) {
export async function createServer(
root = process.cwd(),
hmrPort,
customLogger,
) {
const resolve = (p) => path.resolve(__dirname, p)

const app = express()
Expand All @@ -32,6 +36,7 @@ export async function createServer(root = process.cwd(), hmrPort) {
},
},
appType: 'custom',
customLogger,
})
// use vite's connect instance as middleware
app.use(vite.middlewares)
Expand Down
4 changes: 4 additions & 0 deletions playground/ssr/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { defineConfig } from 'vite'

// in order to trigger ssr server restart
export default defineConfig({})
2 changes: 1 addition & 1 deletion playground/vitestSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ export async function notifyRebuildComplete(
return watcher.off('event', callback)
}

function createInMemoryLogger(logs: string[]): Logger {
export function createInMemoryLogger(logs: string[]): Logger {
const loggedErrors = new WeakSet<Error | RollupError>()
const warnedMessages = new Set<string>()

Expand Down

0 comments on commit ef7b720

Please sign in to comment.