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 testtargo 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 teston the generated output.
The flag model follows go test closely:
| Targo command | Go command it maps to | Notes |
|---|---|---|
targo test | go 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 --cover | go test -cover ./dist/... | enables Go coverage |
targo test --coverprofile coverage.out | go test -coverprofile=<path> ./dist/... | profile is written relative to generated output |
Important --run semantics
--runuses Go test regex semantics, not shell globs.--runonly filters which tests execute after compilation.- It does not skip the earlier Targo type-check or Go code generation stages.
- If
--runfeels 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
--benchalso 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, orExampleas appropriate. - Use table-driven tests and
t.Run()for multiple cases. - Import the
testingpackage from Go, not a JavaScript test framework. - If the
testingpackage declarations are missing, refresh the environment withtargo initortargo test. - For file-scoped diagnostics during iteration, prefer
targo check <path-to-file>before running the broadertargo testpipeline.