vite/scripts/jestPerTestSetup.ts

98 lines
2.5 KiB
TypeScript
Raw Normal View History

2020-12-20 03:33:13 +00:00
import fs from 'fs-extra'
import * as http from 'http'
import { resolve } from 'path'
import slash from 'slash'
import sirv from 'sirv'
2020-12-22 03:00:54 +00:00
import { createServer, build, ViteDevServer, UserConfig } from 'vite'
2020-12-20 03:33:13 +00:00
import { Page } from 'playwright-chromium'
const isBuildTest = !!process.env.VITE_TEST_BUILD
// injected by the test env
declare const page: Page
let server: ViteDevServer | http.Server
let tempDir: string
let err: Error
beforeAll(async () => {
try {
const testPath = expect.getState().testPath
const testName = slash(testPath).match(/playground\/(\w+)\//)?.[1]
// if this is a test placed under playground/xxx/__tests__
// start a vite server in that directory.
if (testName) {
const playgroundRoot = resolve(__dirname, '../packages/playground')
const srcDir = resolve(playgroundRoot, testName)
tempDir = resolve(__dirname, '../temp', testName)
await fs.copy(srcDir, tempDir, {
2020-12-21 23:37:04 +00:00
dereference: true,
2020-12-20 03:33:13 +00:00
filter(file) {
return !file.includes('__tests__')
}
})
2020-12-22 03:00:54 +00:00
const options: UserConfig = {
root: tempDir,
2020-12-22 04:15:38 +00:00
logLevel: 'error',
server: {
watch: {
2020-12-22 20:34:02 +00:00
// During tests we edit the files too fast and sometimes chokidar
// misses change events, so enforce polling for consistency
usePolling: true,
interval: 50
2020-12-22 04:15:38 +00:00
}
}
}
2020-12-20 03:33:13 +00:00
if (!isBuildTest) {
2020-12-22 03:00:54 +00:00
server = await (await createServer(options)).listen()
2020-12-20 03:33:13 +00:00
// use resolved port from server
const url = `http://localhost:${server.config.server.port}`
await page.goto(url)
} else {
2020-12-22 03:00:54 +00:00
await build(options)
2020-12-20 03:33:13 +00:00
const url = await startStaticServer()
await page.goto(url)
}
}
} catch (e) {
// jest doesn't exit if our setup has error here
// https://github.com/facebook/jest/issues/2713
err = e
}
})
afterAll(async () => {
if (server) {
await server.close()
}
if (err) {
throw err
}
})
function startStaticServer(): Promise<string> {
// start static file server
const httpServer = (server = http.createServer(
sirv(resolve(tempDir, 'dist'))
))
let port = 5000
return new Promise((resolve, reject) => {
const onError = (e: any) => {
if (e.code === 'EADDRINUSE') {
httpServer.close()
httpServer.listen(++port)
} else {
reject(e)
}
}
httpServer.on('error', onError)
httpServer.listen(port, () => {
httpServer.removeListener('error', onError)
resolve(`http://localhost:${port}`)
})
})
}