private condenseTestItems()

in src/test-explorer/resolver.ts [476:513]


  private condenseTestItems(parentTest: vscode.TestItem) {
    // Advance through any single-child test items of the same type.
    const getNextItem = (currentItem: vscode.TestItem): vscode.TestItem => {
      let shouldContinue = true
      while (currentItem.children.size === 1 && shouldContinue) {
        // TestItemCollection supports only forEach iteration.
        currentItem.children.forEach(childItem => {
          if (
            this.store.testCaseMetadata.get(currentItem)?.type !==
            this.store.testCaseMetadata.get(childItem)?.type
          ) {
            shouldContinue = false
            return
          }
          currentItem = childItem
        })
      }
      return currentItem
    }

    // Recursively filter the current item and its children, and update their label relative to the parent.
    const condense = (item: vscode.TestItem): vscode.TestItem => {
      let currentParent = getNextItem(item)

      currentParent.children.forEach(child => {
        const filteredChild = condense(child)
        this.store.testCaseMetadata
          .get(filteredChild)
          ?.setDisplayName(this.store.testCaseMetadata.get(currentParent))
        if (filteredChild !== child) {
          currentParent.children.delete(child.id)
          currentParent.children.add(filteredChild)
        }
      })
      return currentParent
    }
    return condense(parentTest)
  }