Skip to content

Discovery miner: TypeScript/JavaScript test functions (Jest, Vitest) #128

@Dimwiddle

Description

@Dimwiddle

Summary

Extract test blocks from TypeScript/JavaScript test files using tree-sitter. Handles Jest and Vitest patterns.

Depends on: #124, #125, #126

New file

src/specleft/discovery/miners/typescript/tests.py

import uuid
from specleft.discovery.models import SupportedLanguage, MinerResult, DiscoveredItem, ItemKind, TestFunctionMeta
from specleft.discovery.context import MinerContext

class TypeScriptTestMiner:
    miner_id = uuid.UUID("aa5151b6-3805-419c-a726-a56755300dda")
    name = "typescript_test_functions"
    languages = frozenset({SupportedLanguage.TYPESCRIPT, SupportedLanguage.JAVASCRIPT})

    def mine(self, ctx: MinerContext) -> MinerResult: ...

Detection rules

File targeting: use ctx.file_index.files_matching("*.test.ts", "*.spec.ts", "*.test.js", "*.spec.js", "*.test.tsx", "*.spec.tsx") — do NOT walk the filesystem directly.

Tree-sitter query (via ctx.registry.parse(file_path)): call expressions where callee name is it or test. For nested describe("name", () => { it("name", ...) }), capture the describe block name.

Framework detection: read from ctx.frameworks.get(SupportedLanguage.TYPESCRIPT, []) or ctx.frameworks.get(SupportedLanguage.JAVASCRIPT, []) — do NOT re-parse package.json.

Typed metadata

Each item's metadata dict must conform to TestFunctionMeta:

TestFunctionMeta(
    framework      = "jest",              # from ctx.frameworks
    class_name     = "Auth",              # describe block name → class_name field
    call_style     = "it",
    has_todo       = False,
    has_docstring  = False,
    is_parametrized = False,
)

name: the string literal passed to it() / test() (e.g. "returns 401 for invalid token")
language: SupportedLanguage.TYPESCRIPT or SupportedLanguage.JAVASCRIPT based on file extension
confidence: 0.9 for known framework + .spec. filename; 0.7 otherwise

Acceptance criteria

  • describe("Auth", () => { it("logs in valid user", ...) }) → item with name="logs in valid user", class_name="Auth" in metadata
  • .ts files get language=SupportedLanguage.TYPESCRIPT; .js files get SupportedLanguage.JAVASCRIPT
  • Each item's metadata validates against TestFunctionMeta
  • it.todo("pending test")has_todo=True
  • test("does thing", async () => {})call_style="test"
  • Uses ctx.file_index.files_matching() — does not walk filesystem
  • Uses ctx.frameworks — does not re-parse package.json
  • Tests in tests/discovery/miners/test_typescript_tests.py
  • Update scenarios and tests in features/feature-spec-discovery.md to cover the functionality introduced by this issue

Metadata

Metadata

Assignees

No one assigned

    Labels

    minerDiscovery miner implementationnew featureIssues or PRs for a new feature that doesn't currently existtypescriptTypeScript and JavaScript language specific

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions