From ae34c48e83a4471dfa1ab7889aa2f5af9dedff84 Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Mon, 22 Jul 2024 11:09:32 +0200 Subject: [PATCH] chore: add debugging to ast collection --- src/worker/collect.ts | 49 ++++++++++++++++++++++++++++++++++++++--- src/worker/setupFile.ts | 2 +- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/src/worker/collect.ts b/src/worker/collect.ts index 8f508247..36b00623 100644 --- a/src/worker/collect.ts +++ b/src/worker/collect.ts @@ -47,13 +47,29 @@ export interface FileInformation { definitions: LocalCallDefinition[] } +const log = process.env.VITEST_VSCODE_DEBUG + ? (...args: any[]) => { + // eslint-disable-next-line no-console + console.info(...args) + } + : undefined + +const verbose = process.env.VITEST_VSCODE_DEBUG === 'verbose' + ? (...args: any[]) => { + // eslint-disable-next-line no-console + console.info(...args) + } + : undefined + export async function astCollectTests( ctx: WorkspaceProject, filepath: string, ): Promise { const request = await ctx.vitenode.transformRequest(filepath, filepath, 'web') // TODO: error cannot parse + const testFilepath = relative(ctx.config.root, filepath) if (!request) { + log?.('Cannot parse', testFilepath, '(vite didn\'t return anything)') return null } const ast = parse(request.code, { @@ -62,7 +78,6 @@ export async function astCollectTests( allowHashBang: true, allowImportExportEverywhere: true, }) - const testFilepath = relative(ctx.config.root, filepath) const file: ParsedFile = { filepath, type: 'suite', @@ -77,6 +92,7 @@ export async function astCollectTests( file: null!, } file.file = file + log?.('Collecting', testFilepath) const definitions: LocalCallDefinition[] = [] const getName = (callee: any): string | null => { if (!callee) { @@ -108,14 +124,17 @@ export async function astCollectTests( const name = getName(callee) let unknown = false if (!name) { + verbose?.('Unknown call', callee) return } if (!['it', 'test', 'describe', 'suite'].includes(name)) { + verbose?.(`Skipping ${name} (unknown call)`) return } const property = callee?.property?.name let mode = !property || property === name ? 'run' : property if (mode === 'each') { + log?.('Skipping `.each` (support not implemented yet)', name) return } @@ -156,6 +175,7 @@ export async function astCollectTests( if (mode === 'skipIf' || mode === 'runIf') { mode = 'skip' } + log?.('Found', name, message, `(${mode})`) definitions.push({ start, end, @@ -193,8 +213,20 @@ export async function astCollectTests( column: processedLocation.column, }) if (originalLocation.column != null) { + verbose?.( + `Found location`, + `${processedLocation.column}:${processedLocation.line}`, + '->', + `${originalLocation.column}:${originalLocation.line}`, + ) location = originalLocation } + else { + log?.('Cannot find original location', `${processedLocation.column}:${processedLocation.line}`) + } + } + else { + log?.('Cannot find original location', `${definition.start}`) } if (definition.type === 'suite') { const task: ParsedSuite = { @@ -224,7 +256,7 @@ export async function astCollectTests( suite: latestSuite, file, mode, - context: {} as any, // not used in typecheck + context: {} as any, // not used on the server name: definition.name, end: definition.end, start: definition.start, @@ -245,6 +277,17 @@ export async function astCollectTests( false, ctx.config.allowOnly, ) + if (!file.tasks.length) { + file.result = { + state: 'fail', + errors: [ + { + name: 'Error', + message: `No test suite found in file ${filepath}`, + }, + ], + } + } return { file, parsed: request.code, @@ -268,7 +311,7 @@ function mergeTemplateLiteral(node: TemplateLiteral): string { return result } -export function createIndexMap(source: string) { +function createIndexMap(source: string) { const map = new Map() let index = 0 let line = 1 diff --git a/src/worker/setupFile.ts b/src/worker/setupFile.ts index cc9ee794..fdd5cecb 100644 --- a/src/worker/setupFile.ts +++ b/src/worker/setupFile.ts @@ -8,7 +8,7 @@ const workerState = globalThis.__vitest_worker__ as WorkerGlobalState const testFile = workerState.filepath! -assert(testFile, () => 'Expected workerState.filepath to be set') +assert(testFile, 'Expected workerState.filepath to be set') // don't run tests that are not watched if rerun was triggered - only collect those tests if (rerunTriggered) {