Skip to content

Commit

Permalink
fix: errors in worker handling (#7236)
Browse files Browse the repository at this point in the history
  • Loading branch information
poyoho authored Mar 25, 2022
1 parent 01bdac9 commit e24dc85
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions scripts/jestPerTestSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { resolve, dirname } from 'path'
import sirv from 'sirv'
import type {
ViteDevServer,
UserConfig,
InlineConfig,
PluginOption,
ResolvedConfig,
Logger
} from 'vite'
import { createServer, build } from 'vite'
import { createServer, build, mergeConfig } from 'vite'
import type { Page, ConsoleMessage } from 'playwright-chromium'
import type { RollupError, RollupWatcher, RollupWatcherEvent } from 'rollup'

Expand Down Expand Up @@ -90,9 +90,16 @@ beforeAll(async () => {
}
}

const testCustomConfig = resolve(dirname(testPath), 'vite.config.js')
let config: InlineConfig | undefined
if (fs.existsSync(testCustomConfig)) {
// test has custom server configuration.
config = require(testCustomConfig)
}

const serverLogs: string[] = []

const options: UserConfig = {
const options: InlineConfig = {
root: rootDir,
logLevel: 'silent',
server: {
Expand Down Expand Up @@ -120,7 +127,9 @@ beforeAll(async () => {

if (!isBuildTest) {
process.env.VITE_INLINE = 'inline-serve'
server = await (await createServer(options)).listen()
server = await (
await createServer(mergeConfig(options, config || {}))
).listen()
// use resolved port/base from server
const base = server.config.base === '/' ? '' : server.config.base
const url =
Expand All @@ -137,14 +146,14 @@ beforeAll(async () => {
}
})
options.plugins = [resolvedPlugin()]
const rollupOutput = await build(options)
const rollupOutput = await build(mergeConfig(options, config || {}))
const isWatch = !!resolvedConfig!.build.watch
// in build watch,call startStaticServer after the build is complete
if (isWatch) {
global.watcher = rollupOutput as RollupWatcher
await notifyRebuildComplete(global.watcher)
}
const url = (global.viteTestUrl = await startStaticServer())
const url = (global.viteTestUrl = await startStaticServer(config))
await page.goto(url)
}
}
Expand Down Expand Up @@ -172,13 +181,15 @@ afterAll(async () => {
}
})

function startStaticServer(): Promise<string> {
// check if the test project has base config
const configFile = resolve(rootDir, 'vite.config.js')
let config: UserConfig | undefined
try {
config = require(configFile)
} catch (e) {}
function startStaticServer(config?: InlineConfig): Promise<string> {
if (!config) {
// check if the test project has base config
const configFile = resolve(rootDir, 'vite.config.js')
try {
config = require(configFile)
} catch (e) {}
}

// fallback internal base to ''
const base = (config?.base ?? '/') === '/' ? '' : config?.base ?? ''

Expand Down

0 comments on commit e24dc85

Please sign in to comment.