Skip to content

Commit ae34c48

Browse files
committed
chore: add debugging to ast collection
1 parent 8349d27 commit ae34c48

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

src/worker/collect.ts

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,29 @@ export interface FileInformation {
4747
definitions: LocalCallDefinition[]
4848
}
4949

50+
const log = process.env.VITEST_VSCODE_DEBUG
51+
? (...args: any[]) => {
52+
// eslint-disable-next-line no-console
53+
console.info(...args)
54+
}
55+
: undefined
56+
57+
const verbose = process.env.VITEST_VSCODE_DEBUG === 'verbose'
58+
? (...args: any[]) => {
59+
// eslint-disable-next-line no-console
60+
console.info(...args)
61+
}
62+
: undefined
63+
5064
export async function astCollectTests(
5165
ctx: WorkspaceProject,
5266
filepath: string,
5367
): Promise<null | FileInformation> {
5468
const request = await ctx.vitenode.transformRequest(filepath, filepath, 'web')
5569
// TODO: error cannot parse
70+
const testFilepath = relative(ctx.config.root, filepath)
5671
if (!request) {
72+
log?.('Cannot parse', testFilepath, '(vite didn\'t return anything)')
5773
return null
5874
}
5975
const ast = parse(request.code, {
@@ -62,7 +78,6 @@ export async function astCollectTests(
6278
allowHashBang: true,
6379
allowImportExportEverywhere: true,
6480
})
65-
const testFilepath = relative(ctx.config.root, filepath)
6681
const file: ParsedFile = {
6782
filepath,
6883
type: 'suite',
@@ -77,6 +92,7 @@ export async function astCollectTests(
7792
file: null!,
7893
}
7994
file.file = file
95+
log?.('Collecting', testFilepath)
8096
const definitions: LocalCallDefinition[] = []
8197
const getName = (callee: any): string | null => {
8298
if (!callee) {
@@ -108,14 +124,17 @@ export async function astCollectTests(
108124
const name = getName(callee)
109125
let unknown = false
110126
if (!name) {
127+
verbose?.('Unknown call', callee)
111128
return
112129
}
113130
if (!['it', 'test', 'describe', 'suite'].includes(name)) {
131+
verbose?.(`Skipping ${name} (unknown call)`)
114132
return
115133
}
116134
const property = callee?.property?.name
117135
let mode = !property || property === name ? 'run' : property
118136
if (mode === 'each') {
137+
log?.('Skipping `.each` (support not implemented yet)', name)
119138
return
120139
}
121140

@@ -156,6 +175,7 @@ export async function astCollectTests(
156175
if (mode === 'skipIf' || mode === 'runIf') {
157176
mode = 'skip'
158177
}
178+
log?.('Found', name, message, `(${mode})`)
159179
definitions.push({
160180
start,
161181
end,
@@ -193,8 +213,20 @@ export async function astCollectTests(
193213
column: processedLocation.column,
194214
})
195215
if (originalLocation.column != null) {
216+
verbose?.(
217+
`Found location`,
218+
`${processedLocation.column}:${processedLocation.line}`,
219+
'->',
220+
`${originalLocation.column}:${originalLocation.line}`,
221+
)
196222
location = originalLocation
197223
}
224+
else {
225+
log?.('Cannot find original location', `${processedLocation.column}:${processedLocation.line}`)
226+
}
227+
}
228+
else {
229+
log?.('Cannot find original location', `${definition.start}`)
198230
}
199231
if (definition.type === 'suite') {
200232
const task: ParsedSuite = {
@@ -224,7 +256,7 @@ export async function astCollectTests(
224256
suite: latestSuite,
225257
file,
226258
mode,
227-
context: {} as any, // not used in typecheck
259+
context: {} as any, // not used on the server
228260
name: definition.name,
229261
end: definition.end,
230262
start: definition.start,
@@ -245,6 +277,17 @@ export async function astCollectTests(
245277
false,
246278
ctx.config.allowOnly,
247279
)
280+
if (!file.tasks.length) {
281+
file.result = {
282+
state: 'fail',
283+
errors: [
284+
{
285+
name: 'Error',
286+
message: `No test suite found in file ${filepath}`,
287+
},
288+
],
289+
}
290+
}
248291
return {
249292
file,
250293
parsed: request.code,
@@ -268,7 +311,7 @@ function mergeTemplateLiteral(node: TemplateLiteral): string {
268311
return result
269312
}
270313

271-
export function createIndexMap(source: string) {
314+
function createIndexMap(source: string) {
272315
const map = new Map<number, { line: number; column: number }>()
273316
let index = 0
274317
let line = 1

src/worker/setupFile.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const workerState = globalThis.__vitest_worker__ as WorkerGlobalState
88

99
const testFile = workerState.filepath!
1010

11-
assert(testFile, () => 'Expected workerState.filepath to be set')
11+
assert(testFile, 'Expected workerState.filepath to be set')
1212

1313
// don't run tests that are not watched if rerun was triggered - only collect those tests
1414
if (rerunTriggered) {

0 commit comments

Comments
 (0)