Skip to content

Commit

Permalink
feat: add logLevel option
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Aug 20, 2024
1 parent 016067b commit 6702c6a
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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_internals>/**\", \"**/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)

Expand Down
11 changes: 11 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ export function getConfig(workspaceFolder?: WorkspaceFolder) {
)
: vitestPackagePath

const logLevel = get<string>('logLevel', 'info')

return {
env: get<null | Record<string, string>>('nodeEnv', null),
debugExclude: get<string[]>('debugExclude', []),
Expand All @@ -59,6 +61,7 @@ export function getConfig(workspaceFolder?: WorkspaceFolder) {
disableWorkspaceWarning: get<boolean>('disableWorkspaceWarning', false),
debuggerPort: get<number>('debuggerPort') || undefined,
debuggerAddress: get<string>('debuggerAddress', undefined) || undefined,
logLevel,
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/debug/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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`
Expand Down
3 changes: 2 additions & 1 deletion src/log.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable no-console */

import { window } from 'vscode'
import { getConfig } from './config'

const _log = window.createOutputChannel('Vitest')
export const log = {
Expand All @@ -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()
Expand Down
16 changes: 8 additions & 8 deletions src/worker/collect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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, {
Expand All @@ -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 => {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 = {
Expand Down

0 comments on commit 6702c6a

Please sign in to comment.