Testing
Writing tests
Section titled “Writing tests”Mark any zero-parameter function with @Test to declare it as a test case:
@Testfn add_two_numbers(): Void { assert 1 + 1 == 2}Rules:
@Testfunctions must have no parameters (E136).@Testmay only appear in files whose name ends in_test.vtx(E137).- Non-test modules may not import test modules (E138).
Use assert for inline assertions. A failing assertion reports the source
location and the condition that was false:
---- add_two_numbers ----math_utils_test.vtx:5:5: assertion failed: 1 + 1 == 3Use let assert to destructure an expected variant and fail fast if it
doesn’t match:
@Testfn divide_succeeds(): Void { let result = safe_divide(10, 2) let assert Ok(value) = result assert value == 5}Naming convention
Section titled “Naming convention”Test files must end in _test.vtx. The codegen emits a class-level @IsTest
for every _test.vtx source file, making the class deployable as a Salesforce
Apex test class.
Running tests
Section titled “Running tests”Single file
Section titled “Single file”vertex test path/to/module_test.vtxRuns all @Test functions in the file. Exit code 0 if all pass, 1 on any failure.
Entire project
Section titled “Entire project”Run from the directory that contains sfdx-project.json:
vertex testDiscovers all *_test.vtx files under <projectRoot>/src/ recursively and
runs them in alphabetical order. Exit code 0 only if every test in every file passes.
Filter
Section titled “Filter”vertex test --filter divideRuns only the test functions whose names contain the filter string (substring match). Exit code 1 if no tests match the filter.
CLI flags
Section titled “CLI flags”| Flag | Description |
|---|---|
--filter <text> | Run only tests whose name contains <text> |
--src <path> | Override the source root (default: <root>/src/) |
--no-color | Disable ANSI color in output |
--help, -h | Show usage |
Output format
Section titled “Output format”Test output follows the Cargo/Rust test style:
running 7 teststest add_returns_sum ... oktest subtract_returns_difference ... oktest multiply_returns_product ... oktest safe_divide_returns_ok_for_nonzero_divisor ... oktest safe_divide_returns_error_for_zero_divisor ... oktest clamp_keeps_value_in_range ... oktest is_even_identifies_even_and_odd ... ok
test result: ok. 7 passed; 0 failedWhen a test fails, the failure details appear after the summary:
running 2 teststest passes ... oktest fails_on_bad_value ... FAILED
failures:
---- fails_on_bad_value ----math_utils_test.vtx:14:5: assertion failed: result == 99
test result: FAILED. 1 passed; 1 failedIsolation model
Section titled “Isolation model”Each @Test function runs in a fresh interpreter context. Module-level
const bindings are re-evaluated for every test; no state leaks between tests.
This matches the Apex test isolation model where each test method runs in a
rolled-back transaction.
Salesforce parity
Section titled “Salesforce parity”@Test functions compile to @IsTest private static void Apex methods.
The file-level @IsTest class annotation is added automatically by the
codegen for any *_test.vtx source file:
@IsTestpublic with sharing class Lang_MathUtilsTest { @IsTest private static void add_returns_sum() { System.Assert.isTrue(Lang_MathUtils.add(2, 3) == 5, 'assertion failed at 5:5: add(2, 3) == 5'); }}The assertion message is embedded in the generated System.Assert.isTrue call
so failures reported by the Salesforce Test Runner point back to the original
Vertex source location.
Language compatibility
Section titled “Language compatibility”All Vertex features are supported in test files. The only restriction is that test files cannot be imported from non-test modules. The following constructs are commonly used in tests:
| Construct | Notes |
|---|---|
assert <bool> | Inline assertion; fails with source location |
let assert <pat> = <expr> | Destructuring assertion; fails if pattern doesn’t match |
debug <expr> | Still works in tests; printed to stdout |
import | Full cross-module imports supported |