-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Description
Clear and concise description of the problem
As a developer using coding agents (like Cursor, GitHub Copilot, VSCode extensions) with Vitest, I want to be able to run tests programmatically without the process hanging in watch mode, so that my agent workflows can complete successfully and provide immediate feedback.
Current Problem:
When coding agents run vitest in interactive terminal environments, the process defaults to watch mode (!isCI && process.stdin.isTTY = true), runs tests, then waits for file changes. This causes the agent to hang indefinitely, preventing it from receiving test results and continuing its workflow.
Current workarounds are suboptimal:
- Use
vitest runinstead ofvitest(requires agents to know about this command) - Set
CI=trueenvironment variable (misleading, not semantically correct) - Configure
watch: falsein vitest config (global override, not per-invocation)
This builds on the discussion in #7818 where @rhysburnie identified the same problem with AI agents getting stuck in watch mode. While PR #7673 restored the TTY check, it doesn't solve the core issue since coding agents still run in interactive terminals.
Suggested solution
Add environment variable support for explicit watch mode control:
New Environment Variables:
VITEST_WATCH=true- Force watch mode regardless of environmentVITEST_WATCH=false- Force run mode (no watch)VITEST_RUN=true- Force run mode (alias forVITEST_WATCH=false)
Implementation:
// In packages/vitest/src/defaults.ts
function getWatchMode(): boolean {
// Explicit overrides take precedence
if (process.env.VITEST_WATCH === 'false' || process.env.VITEST_RUN === 'true') {
return false
}
if (process.env.VITEST_WATCH === 'true') {
return true
}
// Default detection (current behavior)
return !isCI && process.stdin.isTTY
}
export const configDefaults = Object.freeze({
// ... other defaults
watch: getWatchMode(),
// ... other defaults
})Usage Examples:
# Force run mode for coding agents
VITEST_RUN=true vitest
# Force run mode (alternative)
VITEST_WATCH=false vitest
# Force watch mode (if needed)
VITEST_WATCH=true vitest
# Default behavior (unchanged)
vitestBenefits:
- Backward Compatible - Existing developer workflows unchanged
- Explicit Control - Environmetn variables have clear intent for different scenarios, ie CI vs agents
- Agent-Friendly - Simple environment variable for agents to use
- Semantically Correct -
VITEST_RUN=trueis clearer thanCI=true
Alternative
Alternative 1: Change default to vitest run
- Pros: Solves the agent problem immediately
- Cons: Breaking change that would affect all interactive development workflows
Alternative 2: Agent detection
- Pros: Automatic detection of common agent environments
- Cons: More complex, requires agents to set specific variables, may miss some agents
Alternative 3: Use existing CLI arguments
- Pros: Explicit control via CLI flags (
--runvs--run) - Cons: would need to do one of
- projects update their package.json to have test vs test:watch
- agents need to be trained (ie AGENTS.md or rules or whatever) to call
vitest --rundirectly instead of usingnpm test
Alternative 4: Leave as is
- Pros: No changes needed
- Cons: Coding agents continue to have issues, problem identified in
vitestseems to be an issue now that we have code agents #7818 remains unsolved
Additional context
Additional context
Migration Guide:
For Users: No changes required - existing behavior is preserved.
For Coding Agents:
# Instead of: vitest
# Use: VITEST_RUN=true vitest
# Or in package.json scripts:
{
"scripts": {
"test": "VITEST_RUN=true vitest",
"test:watch": "vitest"
}
}For CI/CD: No changes required - CI detection still works as before.
Testing Strategy:
test('VITEST_WATCH=false forces run mode', async () => {
// Test that tests run and process exits
})
test('VITEST_RUN=true forces run mode', async () => {
// Test that tests run and process exits
})
test('VITEST_WATCH=true forces watch mode', async () => {
// Test that process enters watch mode
})
test('default behavior unchanged', async () => {
// Test without any new env vars
})Documentation Updates Needed:
docs/config/index.md- Document new environment variablesdocs/guide/cli.md- Add examples for agent scenariosdocs/guide/features.md- Mention agent compatibility
This solution addresses the coding agent problem identified in #7818 while maintaining backward compatibility and providing a clear, explicit way to control watch mode behavior. It's a simple, safe addition that improves Vitest's usability in automated environments without affecting the excellent interactive development experience.
I intend to submit a PR for this issue if the approach is approved.
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that request the same feature to avoid creating a duplicate.
Metadata
Metadata
Assignees
Type
Projects
Status