MCPcopy
hub / github.com/vitest-dev/vitest / buildTestTree

Function buildTestTree

test/test-utils/index.ts:625–666  ·  view source on GitHub ↗
(
  testModules: TestModule[],
  onTestCase?: (result: TestCase) => unknown,
  onTestSuite?: (testSuite: TestSuite, suiteChildren: Record<string, any>) => unknown,
  onTestModule?: (testModule: TestModule, moduleChildren: Record<string, any>) => unknown,
)

Source from the content-addressed store, hash-verified

623}
624
625export function buildTestTree(
626 testModules: TestModule[],
627 onTestCase?: (result: TestCase) => unknown,
628 onTestSuite?: (testSuite: TestSuite, suiteChildren: Record<string, any>) => unknown,
629 onTestModule?: (testModule: TestModule, moduleChildren: Record<string, any>) => unknown,
630) {
631 type TestTree = Record<string, any>
632
633 function walkCollection(collection: TestCollection): TestTree {
634 const node: TestTree = {}
635
636 for (const child of collection) {
637 if (child.type === 'suite') {
638 // Recursively walk suite children
639 const suiteChildren = walkCollection(child.children)
640 node[child.name] = onTestSuite ? onTestSuite(child, suiteChildren) : suiteChildren
641 }
642 else if (child.type === 'test') {
643 const result = child.result()
644 if (onTestCase) {
645 node[child.name] = onTestCase(child)
646 }
647 else {
648 node[child.name] = result.state
649 }
650 }
651 }
652
653 return node
654 }
655
656 const tree: TestTree = {}
657
658 for (const module of testModules) {
659 // Use relative module ID for cleaner output
660 const key = module.relativeModuleId
661 const moduleChildren = walkCollection(module.children)
662 tree[key] = onTestModule ? onTestModule(module, moduleChildren) : moduleChildren
663 }
664
665 return tree
666}
667
668export function buildTestProjectTree(testModules: TestModule[], onTestCase?: (result: TestCase) => unknown) {
669 const projectTree: Record<string, Record<string, any>> = {}

Callers 5

runner.test.tsFile · 0.90
testTreeFunction · 0.85
buildTreeFunction · 0.85
buildErrorTreeFunction · 0.85
buildTestProjectTreeFunction · 0.85

Calls 1

walkCollectionFunction · 0.85

Tested by

no test coverage detected