private async resolveDocumentTestCases()

in src/test-explorer/resolver.ts [422:468]


  private async resolveDocumentTestCases(
    parentTest: vscode.TestItem,
    cancellationToken?: vscode.CancellationToken
  ) {
    const parentTestInfo: SourceFileTestCaseInfo | undefined =
      this.store.testCaseMetadata.get(parentTest) as SourceFileTestCaseInfo
    if (!parentTestInfo?.target || parentTest.uri === undefined) return

    // Convert document contents into generic DocumentTestItem data.
    const testFileContents = await this.languageToolManager
      .getLanguageTools(parentTestInfo.target)
      .getDocumentTestCases(parentTest.uri, this.repoRoot ?? '')

    // If document analysis has determined that it is not to be considered a test file, hide it.
    if (!testFileContents.isTestFile) {
      // If removing this test item leaves the parent empty, clear the parent as well.
      const cleanupEmptyParent = (testItem?: vscode.TestItem) => {
        if (testItem?.children.size === 0) {
          testItem.parent?.children.delete(testItem.id)
          cleanupEmptyParent(testItem.parent)
        }
      }

      parentTest.parent?.children.delete(parentTest.id)
      cleanupEmptyParent(parentTest.parent)
    }

    // Convert the returned test cases into TestItems and add to the tree.
    const newItems = new Map<DocumentTestItem, vscode.TestItem>()
    const directChildren: vscode.TestItem[] = []
    for (const testCase of testFileContents.testCases ?? []) {
      const newTest = this.testItemFactory.createTestCaseTestItem(
        testCase,
        parentTestInfo.target
      )
      newItems.set(testCase, newTest)

      // Maintain the same parent-child relationship as the returned test case data.
      if (testCase.parent) newItems.get(testCase.parent)?.children.add(newTest)
      else directChildren.push(newTest)
    }
    parentTest.children.replace(directChildren)

    // Update the parent test with required information for full-file run.
    if (testFileContents.documentTest)
      parentTestInfo.setDocumentTestItem(testFileContents.documentTest)
  }