Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 76 additions & 25 deletions packages/vitest/src/node/ast-collect.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { File, Suite, Task, Test } from '@vitest/runner'
import type { Property } from 'estree'
import type { SerializedConfig } from '../runtime/config'
import type { TestError } from '../types/general'
import type { TestProject } from './project'
Expand Down Expand Up @@ -46,6 +45,8 @@ interface LocalCallDefinition {
mode: 'run' | 'skip' | 'only' | 'todo' | 'queued'
task: ParsedSuite | ParsedFile | ParsedTest
dynamic: boolean
concurrent: boolean
sequential: boolean
tags: string[]
}

Expand Down Expand Up @@ -103,8 +104,8 @@ function astParseFile(filepath: string, code: string) {
) {
return getName(callee.property)
}
// call as `__vite_ssr__.test.skip()`
return getName(callee.object?.property)
// call as `__vite_ssr__.test.skip()` or `describe.concurrent.each()`
return getName(callee.object)
}
// unwrap (0, ...)
if (callee.type === 'SequenceExpression' && callee.expressions.length === 2) {
Expand All @@ -116,6 +117,29 @@ function astParseFile(filepath: string, code: string) {
return null
}

const getProperties = (callee: any): string[] => {
if (!callee) {
return []
}
if (callee.type === 'Identifier') {
return []
}
if (callee.type === 'CallExpression') {
return getProperties(callee.callee)
}
if (callee.type === 'TaggedTemplateExpression') {
return getProperties(callee.tag)
}
if (callee.type === 'MemberExpression') {
const props = getProperties(callee.object)
if (callee.property?.name) {
props.push(callee.property.name)
}
return props
}
return []
}

walkAst(ast as any, {
CallExpression(node) {
const { callee } = node as any
Expand All @@ -127,12 +151,24 @@ function astParseFile(filepath: string, code: string) {
verbose?.(`Skipping ${name} (unknown call)`)
return
}
const properties = getProperties(callee)
const property = callee?.property?.name
let mode = !property || property === name ? 'run' : property
// they will be picked up in the next iteration
if (['each', 'for', 'skipIf', 'runIf', 'extend', 'scoped', 'override'].includes(mode)) {
// intermediate calls like .each(), .for() will be picked up in the next iteration
if (property && ['each', 'for', 'skipIf', 'runIf', 'extend', 'scoped', 'override'].includes(property)) {
return
}
// derive mode from the full chain (handles any order like .skip.concurrent or .concurrent.skip)
let mode: 'run' | 'skip' | 'only' | 'todo' = 'run'
for (const prop of properties) {
if (prop === 'skip' || prop === 'only' || prop === 'todo') {
mode = prop
}
else if (prop === 'skipIf' || prop === 'runIf') {
mode = 'skip'
}
}
let isConcurrent = properties.includes('concurrent')
let isSequential = properties.includes('sequential')

let start: number
const end = node.end
Expand Down Expand Up @@ -179,39 +215,46 @@ function astParseFile(filepath: string, code: string) {
// Vitest module mocker injects these
.replace(/__vi_import_\d+__\./g, '')

// cannot statically analyze, so we always skip it
if (mode === 'skipIf' || mode === 'runIf') {
mode = 'skip'
}

const parentCalleeName = typeof callee?.callee === 'object' && callee?.callee.type === 'MemberExpression' && callee?.callee.property?.name
let isDynamicEach = parentCalleeName === 'each' || parentCalleeName === 'for'
if (!isDynamicEach && callee.type === 'TaggedTemplateExpression') {
const property = callee.tag?.property?.name
isDynamicEach = property === 'each' || property === 'for'
}

// Extract tags from the second argument if it's an options object
// Extract options from the second argument if it's an options object
const tags: string[] = []
const secondArg = node.arguments?.[1]
if (secondArg?.type === 'ObjectExpression') {
const tagsProperty = secondArg.properties?.find(
(p: any) => p.type === 'Property' && p.key?.type === 'Identifier' && p.key.name === 'tags',
) as Property | undefined
if (tagsProperty) {
const tagsValue = tagsProperty.value
if (tagsValue?.type === 'Literal' && typeof tagsValue.value === 'string') {
// tags: 'single-tag'
tags.push(tagsValue.value)
for (const prop of (secondArg.properties || []) as any[]) {
if (prop.type !== 'Property' || prop.key?.type !== 'Identifier') {
continue
}
else if (tagsValue?.type === 'ArrayExpression') {
// tags: ['tag1', 'tag2']
for (const element of tagsValue.elements || []) {
if (element?.type === 'Literal' && typeof element.value === 'string') {
tags.push(element.value)
const keyName = prop.key.name
if (keyName === 'tags') {
const tagsValue = prop.value
if (tagsValue?.type === 'Literal' && typeof tagsValue.value === 'string') {
tags.push(tagsValue.value)
}
else if (tagsValue?.type === 'ArrayExpression') {
for (const element of tagsValue.elements || []) {
if (element?.type === 'Literal' && typeof element.value === 'string') {
tags.push(element.value)
}
}
}
}
else if (prop.value?.type === 'Literal' && prop.value.value === true) {
if (keyName === 'skip' || keyName === 'only' || keyName === 'todo') {
mode = keyName
}
else if (keyName === 'concurrent') {
isConcurrent = true
}
else if (keyName === 'sequential') {
isSequential = true
}
}
}
}

Expand All @@ -224,6 +267,8 @@ function astParseFile(filepath: string, code: string) {
mode,
task: null as any,
dynamic: isDynamicEach,
concurrent: isConcurrent,
sequential: isSequential,
tags,
} satisfies LocalCallDefinition)
},
Expand Down Expand Up @@ -366,6 +411,10 @@ function createFileTask(
// Inherit tags from parent suite and merge with own tags
const parentTags = latestSuite.tags || []
const taskTags = unique([...parentTags, ...definition.tags])
// resolve concurrent/sequential: sequential cancels inherited concurrent
const concurrent = definition.sequential
? undefined
: (definition.concurrent || latestSuite.concurrent || undefined)

if (definition.type === 'suite') {
const task: ParsedSuite = {
Expand All @@ -376,6 +425,7 @@ function createFileTask(
tasks: [],
mode,
each: definition.dynamic,
concurrent,
name: definition.name,
fullName: createTaskName([latestSuite.fullName, definition.name]),
fullTestName: createTaskName([latestSuite.fullTestName, definition.name]),
Expand All @@ -398,6 +448,7 @@ function createFileTask(
suite: latestSuite,
file,
each: definition.dynamic,
concurrent,
mode,
context: {} as any, // not used on the server
name: definition.name,
Expand Down
Loading
Loading