Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: use preview server #9992

Merged
merged 3 commits into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions playground/optimize-deps/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ module.exports = {
res.statusCode = 200
res.end('pong')
})
},
configurePreviewServer({ middlewares }) {
middlewares.use('/ping', (_, res) => {
res.statusCode = 200
res.end('pong')
})
}
},
{
Expand Down
70 changes: 18 additions & 52 deletions playground/vitestSetup.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as http from 'node:http'
import type * as http from 'node:http'
import path, { dirname, join, resolve } from 'node:path'
import os from 'node:os'
import sirv from 'sirv'
import fs from 'fs-extra'
import { chromium } from 'playwright-chromium'
import type {
Expand All @@ -12,7 +11,13 @@ import type {
UserConfig,
ViteDevServer
} from 'vite'
import { build, createServer, loadConfigFromFile, mergeConfig } from 'vite'
import {
build,
createServer,
loadConfigFromFile,
mergeConfig,
preview
} from 'vite'
import type { Browser, Page } from 'playwright-chromium'
import type { RollupError, RollupWatcher, RollupWatcherEvent } from 'rollup'
import type { File } from 'vitest'
Expand Down Expand Up @@ -263,57 +268,18 @@ export async function startDefaultServe(): Promise<void> {
watcher = rollupOutput as RollupWatcher
await notifyRebuildComplete(watcher)
}
viteTestUrl = await startStaticServer(testConfig)
await page.goto(viteTestUrl)
}
}

function startStaticServer(config: UserConfig): Promise<string> {
// fallback internal base to ''
let base = config?.base
if (!base || base === '/' || base === './') {
base = ''
}

// @ts-ignore
if (config && config.__test__) {
// @ts-ignore
config.__test__()
}

// start static file server
const serve = sirv(resolve(rootDir, 'dist'), {
dev: !!config?.build?.watch
})
// @ts-ignore
const baseDir = config?.testConfig?.baseRoute
const httpServer = (server = http.createServer((req, res) => {
if (req.url === '/ping') {
res.statusCode = 200
res.end('pong')
} else {
if (baseDir) {
req.url = path.posix.join(baseDir, req.url)
}
serve(req, res)
}
}))
let port = 4173
return new Promise((resolve, reject) => {
const onError = (e: any) => {
if (e.code === 'EADDRINUSE') {
httpServer.close()
httpServer.listen(++port)
} else {
reject(e)
}
if (config && config.__test__) {
// @ts-ignore
config.__test__()
}
httpServer.on('error', onError)
httpServer.listen(port, () => {
httpServer.removeListener('error', onError)
resolve(`http://localhost:${port}${base}`)
})
})
const _nodeEnv = process.env.NODE_ENV
const previewServer = await preview(testConfig)
// prevent preview change NODE_ENV
process.env.NODE_ENV = _nodeEnv
viteTestUrl = previewServer.resolvedUrls.local[0]
await page.goto(viteTestUrl)
}
}

/**
Expand Down