Back to Baml

BAML Language Testing Guide

baml_language/TEST_INSTRUCTIONS.md

0.222.03.5 KB
Original Source

BAML Language Testing Guide

Test Suites

SuiteLocationPurpose
baml_testscrates/baml_tests/Snapshot tests with detailed CST/HIR/THIR output
lsp_actions_testscrates/lsp_actions_tests/LSP integration tests with inline expectations

Workflow: Debugging a Failing Test

1. Identify the issue in lsp_actions_tests

bash
cargo test --package lsp_actions_tests

Look for errors in crates/lsp_actions_tests/test_files/syntax/.

2. Create a minimal repro in baml_tests

Create a new project directory:

bash
mkdir -p crates/baml_tests/projects/my_repro/

Add a .baml file with the minimal repro case:

bash
# crates/baml_tests/projects/my_repro/repro.baml

3. Run and generate snapshots

bash
cargo test --package baml_tests my_repro

Accept new snapshots:

bash
cargo insta accept --all

4. Inspect the output

Snapshots are created in crates/baml_tests/snapshots/my_repro/:

SnapshotContents
*_01_lexer_*.snapToken stream from lexer
*_02_parser_*.snapCST (Concrete Syntax Tree)
*_03_hir.snapHIR (High-level IR)
*_04_thir.snapTHIR (Typed HIR) with type inference
*_05_diagnostics.snapAll errors and warnings
*_06_codegen.snapGenerated bytecode

5. Fix the issue

Edit the relevant crate (baml_compiler_parser, baml_compiler_syntax, baml_compiler_hir, etc.).

6. Re-run and update snapshots

bash
# Update baml_tests snapshots
cargo test --package baml_tests my_repro
cargo insta accept --all

# Update lsp_actions_tests inline expectations
UPDATE_EXPECT=1 cargo test --package lsp_actions_tests

7. Verify all tests pass

bash
# Run all tests (can skip slow parser_stress with --skip parser_stress)
cargo test --package baml_tests -- --skip parser_stress
cargo test --package lsp_actions_tests

Quick Commands

bash
# Run specific test project
cargo test --package baml_tests my_project_name

# Run all snapshot tests
cargo test --package baml_tests

# Run all snapshot tests (skip slow parser_stress tests)
cargo test --package baml_tests -- --skip parser_stress

# Run LSP tests and auto-update expectations
UPDATE_EXPECT=1 cargo test --package lsp_actions_tests

# Accept all pending snapshots
cargo insta accept --all

# Review snapshots interactively
cargo insta review

Key Files

  • Lexer: crates/baml_compiler_lexer/src/tokens.rs
  • Parser: crates/baml_compiler_parser/src/parser.rs
  • Syntax kinds: crates/baml_compiler_syntax/src/syntax_kind.rs
  • AST helpers: crates/baml_compiler_syntax/src/ast.rs
  • HIR lowering: crates/baml_compiler_hir/src/body.rs
  • Type checking: crates/baml_thir/src/lower.rs

DO NOT EDIT the diagnostics manually in lsp_actions_tests. Use update_expect=1

Find the base-case that makes syntax fail and add that to baml_test with a good name and good folder organization.

A good place to start when given a diagnostic failure or some parser issue is to look at the snapshot test (create one if missing) and checking the .snap files for CST/HIR etc.

BEFORE you run these lsp tests with update_expect, make sure to just run without it and figure out if the new results are what you expect.

Just because the existing file may say 'no diagnostics expected' doesn't mean it is correct by the way. We haven't finished implementing all diagnostics. You have to see if we added some other comments elsewhere in the file to see what we should sort of expect, or just inspect the behavior manually.