Skip to content

Testing

Targo testing follows Go's testing model, not Jest or Vitest semantics.

File Naming

Supported test file patterns:

  • *_test.ts
  • *.test.ts

Both compile to *_test.go.

Function Signatures

Unit Tests

typescript
import * as testing from "testing";

export function TestAdd(t: testing.T): void {
  const got = add(2, 3);
  if (got != 5) {
    t.Errorf("add(2, 3) = %d; want 5", got);
  }
}

Benchmarks

typescript
import * as testing from "testing";

export function BenchmarkAdd(b: testing.B): void {
  for (let i = 0; i < b.N; i++) {
    add(1, 2);
  }
}

Running Tests

  • targo test
  • targo test --run <pattern>
  • targo test --bench <pattern>
  • targo test --cover

targo test vs go test

targo test is a Go-oriented wrapper, not a separate test runtime:

  • It type-checks Targo source first.
  • It compiles Targo test files to Go.
  • It then invokes go test on the generated output.

The flag model follows go test closely:

Targo commandGo command it maps toNotes
targo testgo test ./dist/...runs tests in generated packages
targo test --run 'TestAdd'go test -run=TestAdd ./dist/...regex filter for test execution
targo test --bench '.'go test -bench=. ./dist/.... runs all benchmarks
targo test --covergo test -cover ./dist/...enables Go coverage
targo test --coverprofile coverage.outgo test -coverprofile=<path> ./dist/...profile is written relative to generated output

Important --run semantics

  • --run uses Go test regex semantics, not shell globs.
  • --run only filters which tests execute after compilation.
  • It does not skip the earlier Targo type-check or Go code generation stages.
  • If --run feels like it "did not work", the usual cause is that the project was still fully checked and built before the matching test subset ran.

Examples:

bash
targo test --run TestAdd
targo test --run 'Test(Add|Sub)'
targo test --run '^TestAdd$'

Important --bench semantics

  • --bench also uses Go test regex semantics.
  • Use . to run all benchmarks.
  • If you want benchmark-focused runs without ordinary tests, use the same pattern as go test:
bash
targo test --run '^$' --bench .

Practical Rules

  • Test names must start with Test, Benchmark, or Example as appropriate.
  • Use table-driven tests and t.Run() for multiple cases.
  • Import the testing package from Go, not a JavaScript test framework.
  • If the testing package declarations are missing, refresh the environment with targo init or targo test.
  • For file-scoped diagnostics during iteration, prefer targo check <path-to-file> before running the broader targo test pipeline.