Skip to content

Commit

Permalink
chore: add debugging to ast collection
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Jul 22, 2024
1 parent 8349d27 commit ae34c48
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
49 changes: 46 additions & 3 deletions src/worker/collect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<null | FileInformation> {
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, {
Expand All @@ -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',
Expand All @@ -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) {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -156,6 +175,7 @@ export async function astCollectTests(
if (mode === 'skipIf' || mode === 'runIf') {
mode = 'skip'
}
log?.('Found', name, message, `(${mode})`)
definitions.push({
start,
end,
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -268,7 +311,7 @@ function mergeTemplateLiteral(node: TemplateLiteral): string {
return result
}

export function createIndexMap(source: string) {
function createIndexMap(source: string) {
const map = new Map<number, { line: number; column: number }>()
let index = 0
let line = 1
Expand Down
2 changes: 1 addition & 1 deletion src/worker/setupFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit ae34c48

Please sign in to comment.