diff --git a/package.json b/package.json index f5932716..8bf64107 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "url": "https://github.com/vitest-dev/vscode/issues" }, "engines": { - "vscode": "^1.59.0" + "vscode": "^1.63.0" }, "categories": [ "Testing" diff --git a/src/config.ts b/src/config.ts index 6053245b..5b37c150 100644 --- a/src/config.ts +++ b/src/config.ts @@ -5,7 +5,7 @@ export function getConfig() { const config = workspace.getConfiguration('vitest') return { env: config.get('nodeEnv') as null | Record, - commandLine: config.get('commandLine') as string, + commandLine: (config.get('commandLine') || undefined) as string | undefined, include: config.get('include') as string[], exclude: config.get('exclude') as string[], enable: config.get('enable') as boolean, diff --git a/src/extension.ts b/src/extension.ts index 67565a41..fc3e40e4 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -4,7 +4,7 @@ import { effect } from '@vue/reactivity' import { extensionId, getConfig } from './config' import { TestFileDiscoverer } from './discover' import { isVitestEnv } from './pure/isVitestEnv' -import { getVitestCommand, getVitestVersion } from './pure/utils' +import { getVitestCommand, getVitestVersion, stringToCmd } from './pure/utils' import { debugHandler, runHandler, updateSnapshot } from './runHandler' import { TestFile, WEAKMAP_TEST_DATA } from './TestData' import { TestWatcher } from './watch' @@ -53,10 +53,11 @@ export async function activate(context: vscode.ExtensionContext) { const vitestVersion = await getVitestVersion(vitestCmd) console.dir({ vitestVersion }) - if (semver.gte(vitestVersion, '0.8.0')) { + const customTestCmd = getConfig().commandLine + if (semver.gte(vitestVersion, '0.8.0') || customTestCmd) { // enable run/debug/watch tests only if vitest version >= 0.8.0 const testWatcher: undefined | TestWatcher = registerWatchHandler( - vitestCmd, + vitestCmd ?? stringToCmd(customTestCmd!), ctrl, fileDiscoverer, context, diff --git a/src/pure/runner.ts b/src/pure/runner.ts index 0bdafbfb..e20c49ee 100644 --- a/src/pure/runner.ts +++ b/src/pure/runner.ts @@ -67,7 +67,7 @@ export interface FormattedTestResults { export class TestRunner { constructor( private workspacePath: string, - private vitestCommand: { cmd: string; args: string[] } | undefined, + private defaultVitestCommand: { cmd: string; args: string[] } | undefined, ) {} async scheduleRun( @@ -75,8 +75,8 @@ export class TestRunner { testNamePattern: string | undefined, log: (msg: string) => void = () => {}, workspaceEnv: Record = {}, - vitestCommand: { cmd: string; args: string[] } = this.vitestCommand - ? this.vitestCommand + vitestCommand: { cmd: string; args: string[] } = this.defaultVitestCommand + ? this.defaultVitestCommand : { cmd: 'npx', args: ['vitest'] }, updateSnapshot = false, ): Promise { diff --git a/src/pure/utils.ts b/src/pure/utils.ts index cea9f352..1dbbabfe 100644 --- a/src/pure/utils.ts +++ b/src/pure/utils.ts @@ -33,34 +33,46 @@ export function getVitestCommand( projectRoot: string, ): { cmd: string; args: string[] } | undefined { const node_modules = path.resolve(projectRoot, 'node_modules') - if (!existsSync(node_modules)) - return - - const suffixes = [''] - if (isWindows) - suffixes.unshift('.cmd', '.CMD') + try { + if (!existsSync(node_modules)) + return getVitestCommand(path.dirname(projectRoot)) + + const suffixes = [''] + if (isWindows) + suffixes.unshift('.cmd', '.CMD') + + for (const suffix of suffixes) { + if (existsSync(path.resolve(node_modules, '.bin', `vitest${suffix}`))) { + return { + cmd: path.resolve(node_modules, '.bin', `vitest${suffix}`), + args: [], + } + } + } - for (const suffix of suffixes) { - if (existsSync(path.resolve(node_modules, '.bin', `vitest${suffix}`))) { + if (existsSync(path.resolve(node_modules, 'vitest', 'vitest.mjs'))) { return { - cmd: path.resolve(node_modules, '.bin', `vitest${suffix}`), - args: [], + cmd: 'node', + args: [ + sanitizeFilePath(path.resolve(node_modules, 'vitest', 'vitest.mjs')), + ], } } - } - if (existsSync(path.resolve(node_modules, 'vitest', 'vitest.mjs'))) { - return { - cmd: 'node', - args: [ - sanitizeFilePath(path.resolve(node_modules, 'vitest', 'vitest.mjs')), - ], - } + return getVitestCommand(path.dirname(projectRoot)) } + catch (e) { + console.error(e) + } +} + +export interface Cmd { + cmd: string + args: string[] } export async function getVitestVersion( - vitestCommand?: { cmd: string; args: string[] }, + vitestCommand?: Cmd, ): Promise { let process if (vitestCommand == null) { @@ -137,3 +149,11 @@ export function execWithLog( return { child, promise } } + +export function stringToCmd(cmdStr: string): Cmd { + const list = cmdStr.split(' ') + return { + cmd: list[0], + args: list.slice(1), + } +}