test: use isPreview instead of previewBase (#14878)

This commit is contained in:
翠 / green 2023-11-06 17:32:24 +09:00 committed by GitHub
parent e5094e5bf2
commit 1426f57415
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 40 additions and 65 deletions

View File

@ -1,9 +1,9 @@
import { defineConfig } from 'vite'
import baseConfig from './vite.config.js'
export default defineConfig({
export default defineConfig(({ isPreview }) => ({
...baseConfig,
base: './', // relative base to make dist portable
base: !isPreview ? './' : '/relative-base/', // relative base to make dist portable
build: {
...baseConfig.build,
outDir: 'dist/relative-base',
@ -18,8 +18,5 @@ export default defineConfig({
},
},
},
testConfig: {
previewBase: '/relative-base/',
},
cacheDir: 'node_modules/.vite-relative-base',
})
}))

View File

@ -1,9 +1,9 @@
import { defineConfig } from 'vite'
import baseConfig from './vite.config.js'
export default defineConfig({
export default defineConfig(({ isPreview }) => ({
...baseConfig,
base: './', // relative base to make dist portable
base: !isPreview ? './' : '/relative-base/', // relative base to make dist portable
build: {
...baseConfig.build,
outDir: 'dist/relative-base',
@ -18,8 +18,5 @@ export default defineConfig({
},
},
},
testConfig: {
previewBase: '/relative-base/',
},
cacheDir: 'node_modules/.vite-relative-base',
})
}))

View File

@ -2,8 +2,8 @@ import path from 'node:path'
import legacy from '@vitejs/plugin-legacy'
import { defineConfig } from 'vite'
export default defineConfig({
base: './',
export default defineConfig(({ isPreview }) => ({
base: !isPreview ? './' : '/no-polyfills-no-systemjs/',
plugins: [
legacy({
renderModernChunks: false,
@ -25,7 +25,4 @@ export default defineConfig({
},
},
},
testConfig: {
previewBase: '/no-polyfills-no-systemjs/',
},
})
}))

View File

@ -2,8 +2,8 @@ import path from 'node:path'
import legacy from '@vitejs/plugin-legacy'
import { defineConfig } from 'vite'
export default defineConfig({
base: './',
export default defineConfig(({ isPreview }) => ({
base: !isPreview ? './' : '/no-polyfills/',
plugins: [
legacy({
renderModernChunks: false,
@ -24,7 +24,4 @@ export default defineConfig({
},
},
},
testConfig: {
previewBase: '/no-polyfills/',
},
})
}))

View File

@ -177,14 +177,9 @@ beforeAll(async (s) => {
}
})
export async function startDefaultServe(): Promise<void> {
async function loadConfig(configEnv: ConfigEnv) {
let config: UserConfig | null = null
const configEnv: ConfigEnv = {
command: isBuild ? 'build' : 'serve',
mode: isBuild ? 'production' : 'development',
}
// config file named by convention as the *.spec.ts folder
const variantName = path.basename(dirname(testPath))
if (variantName !== '__tests__') {
@ -231,13 +226,16 @@ export async function startDefaultServe(): Promise<void> {
},
customLogger: createInMemoryLogger(serverLogs),
}
return mergeConfig(options, config || {})
}
export async function startDefaultServe(): Promise<void> {
setupConsoleWarnCollector(serverLogs)
if (!isBuild) {
process.env.VITE_INLINE = 'inline-serve'
const testConfig = mergeConfig(options, config || {})
viteServer = server = await (await createServer(testConfig)).listen()
const config = await loadConfig({ command: 'serve', mode: 'development' })
viteServer = server = await (await createServer(config)).listen()
viteTestUrl = server.resolvedUrls.local[0]
if (server.config.base === '/') {
viteTestUrl = viteTestUrl.replace(/\/$/, '')
@ -252,24 +250,30 @@ export async function startDefaultServe(): Promise<void> {
resolvedConfig = config
},
})
options.plugins = [resolvedPlugin()]
const testConfig = mergeConfig(options, config || {})
const rollupOutput = await build(testConfig)
const buildConfig = mergeConfig(
await loadConfig({ command: 'build', mode: 'production' }),
{
plugins: [resolvedPlugin()],
},
)
const rollupOutput = await build(buildConfig)
const isWatch = !!resolvedConfig!.build.watch
// in build watch,call startStaticServer after the build is complete
if (isWatch) {
watcher = rollupOutput as RollupWatcher
await notifyRebuildComplete(watcher)
}
if (config && config.__test__) {
config.__test__()
}
// TODO: use something like ConfigEnv['cmd'] https://github.com/vitejs/vite/pull/12298
if (config?.testConfig?.previewBase) {
testConfig.base = config.testConfig.previewBase
if (buildConfig.__test__) {
buildConfig.__test__()
}
const previewConfig = await loadConfig({
command: 'serve',
mode: 'development',
isPreview: true,
})
const _nodeEnv = process.env.NODE_ENV
const previewServer = await preview(testConfig)
const previewServer = await preview(previewConfig)
// prevent preview change NODE_ENV
process.env.NODE_ENV = _nodeEnv
viteTestUrl = previewServer.resolvedUrls.local[0]
@ -348,16 +352,5 @@ declare module 'vite' {
* runs after build and before preview
*/
__test__?: () => void
/**
* special test only configs
*/
testConfig?: {
/**
* a base used for preview
*
* useful for relative base tests
*/
previewBase?: string
}
}
}

View File

@ -1,8 +1,8 @@
import { defineConfig } from 'vite'
import workerPluginTestPlugin from './worker-plugin-test-plugin'
export default defineConfig({
base: './',
export default defineConfig(({ isPreview }) => ({
base: !isPreview ? './' : '/relative-base-iife/',
resolve: {
alias: {
'@': __dirname,
@ -30,9 +30,6 @@ export default defineConfig({
},
},
},
testConfig: {
previewBase: '/relative-base-iife/',
},
plugins: [workerPluginTestPlugin()],
cacheDir: 'node_modules/.vite-relative-base-iife',
})
}))

View File

@ -1,8 +1,8 @@
import { defineConfig } from 'vite'
import workerPluginTestPlugin from './worker-plugin-test-plugin'
export default defineConfig({
base: './',
export default defineConfig(({ isPreview }) => ({
base: !isPreview ? './' : '/relative-base/',
resolve: {
alias: {
'@': __dirname,
@ -30,9 +30,6 @@ export default defineConfig({
},
},
},
testConfig: {
previewBase: '/relative-base/',
},
plugins: [
workerPluginTestPlugin(),
{
@ -48,4 +45,4 @@ export default defineConfig({
},
],
cacheDir: 'node_modules/.vite-relative-base',
})
}))