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

feat: --watch, -w, watch is prior to config's watch #736

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
54 changes: 29 additions & 25 deletions packages/vitest/src/node/plugins/index.ts
Expand Up @@ -8,7 +8,18 @@ import { GlobalSetupPlugin } from './globalSetup'
import { MocksPlugin } from './mock'
import { EnvReplacerPlugin } from './envReplacer'

export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest()): Promise<VitePlugin[]> {
function isWatchArg() {
return (
process.argv.includes('-w')
|| process.argv.includes('--watch')
|| process.argv.includes('watch')
)
}

export async function VitestPlugin(
options: UserConfig = {},
ctx = new Vitest(),
): Promise<VitePlugin[]> {
let haveStarted = false

async function UIPlugin() {
Expand Down Expand Up @@ -39,9 +50,10 @@ export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest())
},
server: {
...preOptions.api,
open: preOptions.ui && preOptions.open
? preOptions.uiBase ?? '/__vitest__/'
: undefined,
open:
preOptions.ui && preOptions.open
? preOptions.uiBase ?? '/__vitest__/'
: undefined,
preTransformRequests: false,
},
// disable deps optimization
Expand All @@ -50,18 +62,15 @@ export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest())
},
async configResolved(viteConfig) {
const viteConfigTest = (viteConfig.test as any) || {}
if (viteConfigTest.watch === false)
viteConfigTest.run = true
if (viteConfigTest.watch === false) viteConfigTest.run = true

// viteConfig.test is final now, merge it for real
options = deepMerge(
{},
configDefaults,
viteConfigTest,
options,
)
options = deepMerge({}, configDefaults, viteConfigTest, options)
options.api = resolveApiConfig(options)
options.watch = options.watch && !options.run
if (isWatchArg())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having the plugin coupled with CLI argv is probably not a good idea. I will take another try

options.watch = true
else
options.watch = options.watch && !options.run

process.env.BASE_URL ??= viteConfig.base
process.env.MODE ??= viteConfig.mode
Expand All @@ -76,29 +85,24 @@ export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest())
if (key.startsWith('import.meta.env.')) {
const val = viteConfig.define[key]
const envKey = key.slice('import.meta.env.'.length)
process.env[envKey] = typeof val === 'string' ? JSON.parse(val) : val
process.env[envKey]
= typeof val === 'string' ? JSON.parse(val) : val
}
}
},
async configureServer(server) {
if (haveStarted)
await ctx.report('onServerRestart')
if (haveStarted) await ctx.report('onServerRestart')
await ctx.setServer(options, server)
haveStarted = true
if (options.api)
(await import('../../api/setup')).setup(ctx)
if (options.api) (await import('../../api/setup')).setup(ctx)

// #415, in run mode we don't need the watcher, close it would improve the performance
if (!options.watch)
await server.watcher.close()
if (!options.watch) await server.watcher.close()
},
},
EnvReplacerPlugin(),
MocksPlugin(),
GlobalSetupPlugin(ctx),
options.ui
? await UIPlugin()
: null,
]
.filter(notNullish)
options.ui ? await UIPlugin() : null,
].filter(notNullish)
}