diff --git a/README.md b/README.md index c221a974..5295e99b 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,7 @@ These options are resolved relative to the [workspace file](https://code.visuals - `vitest.debugExclude`: Excludes files matching specified glob patterns from debugging. Default: `[\"/**\", \"**/node_modules/**\"]` - `vitest.maximumConfigs`: The maximum amount of configs that Vitest extension can load. If exceeded, the extension will show a warning suggesting to use a workspace config file. Default: `3` +- `vitest.logLevel`: How verbose should the logger be in the "Output" channel. Default: `info` ## FAQs (Frequently Asked Questions) diff --git a/package.json b/package.json index e0c4b59d..859ebe63 100644 --- a/package.json +++ b/package.json @@ -158,6 +158,17 @@ ], "default": null, "scope": "window" + }, + "vitest.logLevel": { + "description": "The log level of the Vitest extension.", + "type": "string", + "enum": [ + "info", + "debug", + "verbose" + ], + "default": "info", + "scope": "resource" } } } diff --git a/src/api.ts b/src/api.ts index 3d0e05f2..684e6812 100644 --- a/src/api.ts +++ b/src/api.ts @@ -275,6 +275,7 @@ async function createChildVitestProcess(pkg: VitestPackage) { throw new Error(errorMsg) } log.info('[API]', `Running ${formapPkg(pkg)} with Node.js: ${execPath}`) + const logLevel = getConfig(pkg.folder).logLevel const vitest = fork( // to support pnp, we need to spawn `yarn node` instead of `node` workerPath, @@ -284,6 +285,7 @@ async function createChildVitestProcess(pkg: VitestPackage) { env: { ...process.env, ...env, + VITEST_VSCODE_LOG: env.VITEST_VSCODE_LOG ?? process.env.VITEST_VSCODE_LOG ?? logLevel, VITEST_VSCODE: 'true', // same env var as `startVitest` // https://github.com/vitest-dev/vitest/blob/5c7e9ca05491aeda225ce4616f06eefcd068c0b4/packages/vitest/src/node/cli/cli-api.ts diff --git a/src/config.ts b/src/config.ts index 50d1f64c..099cc84f 100644 --- a/src/config.ts +++ b/src/config.ts @@ -47,6 +47,8 @@ export function getConfig(workspaceFolder?: WorkspaceFolder) { ) : vitestPackagePath + const logLevel = get('logLevel', 'info') + return { env: get>('nodeEnv', null), debugExclude: get('debugExclude', []), @@ -59,6 +61,7 @@ export function getConfig(workspaceFolder?: WorkspaceFolder) { disableWorkspaceWarning: get('disableWorkspaceWarning', false), debuggerPort: get('debuggerPort') || undefined, debuggerAddress: get('debuggerAddress', undefined) || undefined, + logLevel, } } diff --git a/src/debug/api.ts b/src/debug/api.ts index 0a50576d..6db2293a 100644 --- a/src/debug/api.ts +++ b/src/debug/api.ts @@ -35,6 +35,7 @@ export async function debugTests( vscode.workspace.workspaceFile?.fsPath || pkg.folder.uri.fsPath, ) const env = config.env || {} + const logLevel = config.logLevel const debugConfig = { __name: 'Vitest', @@ -50,6 +51,7 @@ export async function debugTests( env: { ...process.env, ...env, + VITEST_VSCODE_LOG: env.VITEST_VSCODE_LOG ?? process.env.VITEST_VSCODE_LOG ?? logLevel, VITEST_VSCODE: 'true', VITEST_WS_ADDRESS: wsAddress, // same env var as `startVitest` diff --git a/src/log.ts b/src/log.ts index f8fc54a7..2cab4825 100644 --- a/src/log.ts +++ b/src/log.ts @@ -1,6 +1,7 @@ /* eslint-disable no-console */ import { window } from 'vscode' +import { getConfig } from './config' const _log = window.createOutputChannel('Vitest') export const log = { @@ -27,7 +28,7 @@ export const log = { } _log.appendLine(`[Error ${time}] ${args.join(' ')}`) }, - verbose: process.env.VITEST_VSCODE_DEBUG !== 'true' + verbose: getConfig().logLevel === 'verbose' || process.env.VITEST_VSCODE_LOG === 'verbose' ? undefined : (...args: string[]) => { const time = new Date().toLocaleTimeString() diff --git a/src/worker/collect.ts b/src/worker/collect.ts index 0a0730b7..f84c9487 100644 --- a/src/worker/collect.ts +++ b/src/worker/collect.ts @@ -47,14 +47,14 @@ export interface FileInformation { definitions: LocalCallDefinition[] } -const log = process.env.VITEST_VSCODE_DEBUG +const debug = process.env.VITEST_VSCODE_LOG !== 'info' ? (...args: any[]) => { // eslint-disable-next-line no-console console.info(...args) } : undefined -const verbose = process.env.VITEST_VSCODE_DEBUG === 'verbose' +const verbose = process.env.VITEST_VSCODE_LOG === 'verbose' ? (...args: any[]) => { // eslint-disable-next-line no-console console.info(...args) @@ -69,7 +69,7 @@ export async function astCollectTests( // TODO: error cannot parse const testFilepath = relative(ctx.config.root, filepath) if (!request) { - log?.('Cannot parse', testFilepath, '(vite didn\'t return anything)') + debug?.('Cannot parse', testFilepath, '(vite didn\'t return anything)') return null } const ast = parse(request.code, { @@ -96,7 +96,7 @@ export async function astCollectTests( verbose('Collecing', testFilepath, request.code) } else { - log?.('Collecting', testFilepath) + debug?.('Collecting', testFilepath) } const definitions: LocalCallDefinition[] = [] const getName = (callee: any): string | null => { @@ -139,7 +139,7 @@ export async function astCollectTests( const property = callee?.property?.name let mode = !property || property === name ? 'run' : property if (mode === 'each') { - log?.('Skipping `.each` (support not implemented yet)', name) + debug?.('Skipping `.each` (support not implemented yet)', name) return } @@ -180,7 +180,7 @@ export async function astCollectTests( if (mode === 'skipIf' || mode === 'runIf') { mode = 'skip' } - log?.('Found', name, message, `(${mode})`) + debug?.('Found', name, message, `(${mode})`) definitions.push({ start, end, @@ -227,11 +227,11 @@ export async function astCollectTests( location = originalLocation } else { - log?.('Cannot find original location', `${processedLocation.column}:${processedLocation.line}`) + debug?.('Cannot find original location', `${processedLocation.column}:${processedLocation.line}`) } } else { - log?.('Cannot find original location', `${definition.start}`) + debug?.('Cannot find original location', `${definition.start}`) } if (definition.type === 'suite') { const task: ParsedSuite = {