Skip to content

Commit

Permalink
fix: review issues
Browse files Browse the repository at this point in the history
  • Loading branch information
2eha0 committed Jan 8, 2025
1 parent 9ddda00 commit 96fd000
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 155 deletions.
7 changes: 5 additions & 2 deletions .github/actions/setup-pnpm-with-dependencies/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,19 @@ runs:
key: ${{ steps.node-version.outputs.cache-key }}

- name: Set Playwright path
id: playwright-path
shell: bash
run: echo "PLAYWRIGHT_BROWSERS_PATH=$HOME/.cache/playwright-bin" >> $GITHUB_ENV
run: echo "PLAYWRIGHT_BROWSERS_PATH=$HOME/.cache/playwright-bin" >> $GITHUB_OUTPUT

- name: Cache Playwright's binary
id: playwright-cache
uses: actions/cache@v4
with:
key: playwright-bin-v1
path: ${{ env.PLAYWRIGHT_BROWSERS_PATH }}
path: ${{ steps.playwright-path.outputs.PLAYWRIGHT_BROWSERS_PATH }}

- name: Install Playwright
id: playwright-install
shell: bash
# does not need to explicitly set chromium after https://github.com/microsoft/playwright/issues/14862 is solved
run: pnpm playwright install chromium
12 changes: 3 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,14 @@ pnpm lint:fix
Unit tests are run with [Vitest](https://vitest.dev/).

```shell
# Run tests both serve and build mode
# Run tests in both server and build modes of Vite
pnpm test
# Run serve mode tests
# Run tests in server mode of Vite
pnpm test-serve
# Run build mode tests
# Run tests in build mode of Vite
pnpm test-build
# Debug serve mode tests
pnpm debug-serve
# Debug build mode tests and keep build artifacts
pnpm debug-build
```

### Build
Expand Down
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
"test": "run-s test-serve test-build",
"test-serve": "vitest run -c vitest.config.e2e.ts",
"test-build": "VITE_TEST_BUILD=1 vitest run -c vitest.config.e2e.ts",
"debug-serve": "VITE_DEBUG_SERVE=1 vitest run -c vitest.config.e2e.ts",
"debug-build": "VITE_TEST_BUILD=1 VITE_PRESERVE_BUILD_ARTIFACTS=1 vitest run -c vitest.config.e2e.ts",
"typecheck": "vue-tsc --noEmit",
"build": "unbuild",
"commit": "cz"
Expand Down
46 changes: 23 additions & 23 deletions playground/vitestGlobalSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,31 @@ export async function setup(): Promise<void> {
await fs.mkdirp(DIR)
await fs.writeFile(path.join(DIR, 'wsEndpoint'), browserServer.wsEndpoint())

const tempDir = path.resolve(__dirname, '../playground-temp')
await fs.ensureDir(tempDir)
await fs.emptyDir(tempDir)
await fs
.copy(path.resolve(__dirname, '../playground'), tempDir, {
dereference: false,
filter(file) {
file = file.replace(/\\/g, '/')
return !file.includes('__tests__') && !file.match(/dist(\/|$)/)
},
})
.catch(async (error) => {
if (error.code === 'EPERM' && error.syscall === 'symlink') {
throw new Error(
'Could not create symlinks. On Windows, consider activating Developer Mode to allow non-admin users to create symlinks by following the instructions at https://docs.microsoft.com/en-us/windows/apps/get-started/enable-your-device-for-development.',
)
} else {
throw error
}
})
// const tempDir = path.resolve(__dirname, '../playground-temp')
// await fs.ensureDir(tempDir)
// await fs.emptyDir(tempDir)
// await fs
// .copy(path.resolve(__dirname, '../playground'), tempDir, {
// dereference: false,
// filter(file) {
// file = file.replace(/\\/g, '/')
// return !file.includes('__tests__') && !file.match(/dist(\/|$)/)
// },
// })
// .catch(async (error) => {
// if (error.code === 'EPERM' && error.syscall === 'symlink') {
// throw new Error(
// 'Could not create symlinks. On Windows, consider activating Developer Mode to allow non-admin users to create symlinks by following the instructions at https://docs.microsoft.com/en-us/windows/apps/get-started/enable-your-device-for-development.',
// )
// } else {
// throw error
// }
// })
}

export async function teardown(): Promise<void> {
await browserServer?.close()
if (!process.env.VITE_PRESERVE_BUILD_ARTIFACTS) {
fs.removeSync(path.resolve(__dirname, '../playground-temp'))
}
// if (!process.env.VITE_PRESERVE_BUILD_ARTIFACTS) {
// fs.removeSync(path.resolve(__dirname, '../playground-temp'))
// }
}
156 changes: 81 additions & 75 deletions playground/vitestSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import { chromium } from 'playwright-chromium'
import type {
InlineConfig,
Logger,
PluginOption,
ResolvedConfig,
UserConfig,
ViteDevServer,
} from 'vite'
Expand All @@ -19,17 +17,23 @@ import {
preview,
} from 'vite'
import type { Browser, Page } from 'playwright-chromium'
import type { RollupError, RollupWatcher, RollupWatcherEvent } from 'rollup'
import type { RollupError } from 'rollup'
import type { File } from 'vitest'
import { beforeAll } from 'vitest'

// #region env

export const workspaceRoot = resolve(__dirname, '../')

/**
* Whether testing vite build mode
*/
export const isBuild = !!process.env.VITE_TEST_BUILD
/**
* Whether testing vite server mode
*/
export const isServe = !isBuild
export const isWindows = process.platform === 'win32'
// export const isWindows = process.platform === 'win32'

// #endregion

Expand Down Expand Up @@ -67,12 +71,12 @@ export const serverLogs: string[] = []
export const browserLogs: string[] = []
export const browserErrors: Error[] = []

export let resolvedConfig: ResolvedConfig = undefined!
// export const resolvedConfig: ResolvedConfig = undefined!

export let page: Page = undefined!
export let browser: Browser = undefined!
export let viteTestUrl: string = ''
export let watcher: RollupWatcher | undefined = undefined
// export const watcher: RollupWatcher | undefined = undefined

declare module 'vite' {
interface InlineConfig {
Expand All @@ -84,9 +88,9 @@ declare module 'vite' {
}
}

export function setViteUrl(url: string): void {
viteTestUrl = url
}
// export function setViteUrl(url: string): void {
// viteTestUrl = url
// }

// #endregion

Expand Down Expand Up @@ -139,33 +143,34 @@ beforeAll(async (s) => {
// if this is a test placed under playground/xxx/__tests__
// start a vite server in that directory.
if (testName) {
testDir = resolve(workspaceRoot, 'playground-temp', testName)
testDir = resolve(workspaceRoot, 'playground', testName)

// when `root` dir is present, use it as vite's root
const testCustomRoot = resolve(testDir, 'root')
rootDir = fs.existsSync(testCustomRoot) ? testCustomRoot : testDir

const testCustomServe = [
resolve(dirname(testPath), 'serve.ts'),
resolve(dirname(testPath), 'serve.js'),
].find((i) => fs.existsSync(i))

if (testCustomServe) {
// test has custom server configuration.
const mod = await import(testCustomServe)
const serve = mod.serve || mod.default?.serve
const preServe = mod.preServe || mod.default?.preServe
if (preServe) {
await preServe()
}
if (serve) {
server = await serve()
viteServer = mod.viteServer
return
}
} else {
await startDefaultServe()
}
// const testCustomRoot = resolve(testDir, 'root')
// rootDir = fs.existsSync(testCustomRoot) ? testCustomRoot : testDir
rootDir = testDir

// const testCustomServe = [
// resolve(dirname(testPath), 'serve.ts'),
// resolve(dirname(testPath), 'serve.js'),
// ].find((i) => fs.existsSync(i))

// if (testCustomServe) {
// // test has custom server configuration.
// const mod = await import(testCustomServe)
// const serve = mod.serve || mod.default?.serve
// const preServe = mod.preServe || mod.default?.preServe
// if (preServe) {
// await preServe()
// }
// if (serve) {
// server = await serve()
// viteServer = mod.viteServer
// return
// }
// } else {
await startDefaultServe()
// }
}
} catch (e) {
// Closing the page since an error in the setup, for example a runtime error
Expand All @@ -181,7 +186,7 @@ beforeAll(async (s) => {
serverLogs.length = 0
await page?.close()
await server?.close()
await watcher?.close()
// await watcher?.close()
if (browser) {
await browser.close()
}
Expand Down Expand Up @@ -219,12 +224,12 @@ export async function startDefaultServe(): Promise<void> {
logLevel: 'silent',
configFile: false,
server: {
watch: {
// During tests we edit the files too fast and sometimes chokidar
// misses change events, so enforce polling for consistency
usePolling: true,
interval: 100,
},
// watch: {
// // During tests we edit the files too fast and sometimes chokidar
// // misses change events, so enforce polling for consistency
// usePolling: true,
// interval: 100,
// },
host: true,
fs: {
strict: !isBuild,
Expand All @@ -240,7 +245,7 @@ export async function startDefaultServe(): Promise<void> {
customLogger: createInMemoryLogger(serverLogs),
}

setupConsoleWarnCollector(serverLogs)
setupConsoleWarnCollector()

if (!isBuild) {
process.env.VITE_INLINE = 'inline-serve'
Expand All @@ -256,27 +261,28 @@ export async function startDefaultServe(): Promise<void> {
} else {
process.env.VITE_INLINE = 'inline-build'
// determine build watch
const resolvedPlugin: () => PluginOption = () => ({
name: 'vite-plugin-watcher',
configResolved(config) {
resolvedConfig = config
},
})
options.plugins = [resolvedPlugin()]
// const resolvedPlugin: () => PluginOption = () => ({
// name: 'vite-plugin-watcher',
// configResolved(config) {
// resolvedConfig = config
// },
// })
// options.plugins = [resolvedPlugin()]
const testConfig = mergeConfig(options, config || {})
viteConfig = testConfig
const rollupOutput = await build(testConfig)
const isWatch = !!resolvedConfig!.build.watch
// in build watch,call startStaticServer after the build is complete
if (isWatch) {
watcher = rollupOutput as RollupWatcher
await notifyRebuildComplete(watcher)
}
// const rollupOutput = await build(testConfig)
await build(testConfig)
// const isWatch = !!resolvedConfig!.build.watch
// // in build watch,call startStaticServer after the build is complete
// if (isWatch) {
// watcher = rollupOutput as RollupWatcher
// await notifyRebuildComplete(watcher)
// }
// @ts-ignore custom test hook
if (config && config.__test__) {
// @ts-ignore custom test hook
config.__test__()
}
// if (config && config.__test__) {
// // @ts-ignore custom test hook
// config.__test__()
// }
const _nodeEnv = process.env.NODE_ENV
const previewServer = await preview(testConfig)
// prevent preview change NODE_ENV
Expand All @@ -289,21 +295,21 @@ export async function startDefaultServe(): Promise<void> {
/**
* Send the rebuild complete message in build watch
*/
export async function notifyRebuildComplete(
watcher: RollupWatcher,
): Promise<RollupWatcher> {
let resolveFn: undefined | (() => void)
const callback = (event: RollupWatcherEvent): void => {
if (event.code === 'END') {
resolveFn?.()
}
}
watcher.on('event', callback)
await new Promise<void>((resolve) => {
resolveFn = resolve
})
return watcher.off('event', callback)
}
// export async function notifyRebuildComplete(
// watcher: RollupWatcher,
// ): Promise<RollupWatcher> {
// let resolveFn: undefined | (() => void)
// const callback = (event: RollupWatcherEvent): void => {
// if (event.code === 'END') {
// resolveFn?.()
// }
// }
// watcher.on('event', callback)
// await new Promise<void>((resolve) => {
// resolveFn = resolve
// })
// return watcher.off('event', callback)
// }

function createInMemoryLogger(logs: string[]): Logger {
const loggedErrors = new WeakSet<Error | RollupError>()
Expand Down
24 changes: 0 additions & 24 deletions playground/vue/.gitignore

This file was deleted.

2 changes: 1 addition & 1 deletion playground/vue/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@kong-vite-plugin/vue",
"name": "@kong-vite-plugin/vue-demo",
"private": true,
"type": "module",
"scripts": {
Expand Down
Loading

0 comments on commit 96fd000

Please sign in to comment.