test
Import from bear/test.
Testing a Bear model
A test states a fact about a building and checks it. It materializes the source once and asserts against the model's tree (extents, transforms, node counts, props), so a claim costs one materialize pass rather than a pile of boolean geometry.
Tests are ordinary project files, so they version and sync alongside the model.
Writing one
// deck.test.ts
import { describe, expect, it, runTests } from "bear/test";
import NominalLumber from "bear/blueprints/materials/NominalLumber";
import { d } from "bear/measurements";
import Source from "@/model";
describe("deck framing", () => {
it('has a 1/4" ledger gap', ({ q }) => {
const ledger = q("Ledger").one();
const joist = q(/^Joist-/).first();
expect(gapBetween(ledger, joist)).toBeCloseTo(d`1/4"`, 3);
});
it("hangs every joist off the ledger", ({ q }) => {
expect(q(NominalLumber).length).toBe(14);
});
});
export default () => runTests(Source);Running it
executeScript({ filePath: "deck.test.ts" }). That's the whole wiring.
The default export is what makes the file runnable. The execution session
imports a fresh entry module and calls its handler with (args, env), so
the value you return becomes the tool result: the agent reads the
TestReport straight out of the tool call.
Every call re-fetches and re-evaluates the file, so describe/it register
again on each run and the counts stay stable. Registration is inert on its
own: nothing runs until runTests does, so importing a test file is free.
Checking one thing, right now
In the REPL the model is already on screen, and the REPL's built-in
runTests() (no import) checks that tree. No import of the model, and no
second materialize:
import { expect, it } from "bear/test";
import NominalLumber from "bear/blueprints/materials/NominalLumber";
it("has 14 joists", ({ q }) => expect(q(NominalLumber).length).toBe(14));
await runTests()The built-in passes the model on screen to this module's runTests, so
the q a check receives is bound to the same tree as the q you explored
with by hand. A selector naming a project component works too: the sandbox
keeps one instance of each project file, so import Deck from "@/parts/Deck"
in the REPL is the very Deck the model on screen was built from, and
q(Deck) matches it. The body ports into a file unchanged: keep the it(…) as
written, import runTests from bear/test, and give it a source to materialize.
The test context
Each test receives a TestContext, destructured. q is
bear/engine/query's bound entry point (queryOf(result)), nothing
narrowed: every selector form works, and the tree you're asserting against
is the one this run materialized. Nothing global is touched, so verifying a
model never disturbs the one on screen.
q's strict terminals are worth leaning on here. .one() and .first()
throw when a selector matches nothing, and the message carries the chain and
a diagnosis, so a check that was silently asserting over an empty set becomes
a named failure telling you which selector went stale.
Checks in the workspace
The workbench's Checks panel runs Bear's built-ins (bear/checks/builtin)
against the model on screen, then imports each spec/tests/ test file and
runs what it registered against that same model, after each build and
whenever a test file changes. The file's default export isn't called there,
so there is no second materialize, and its project imports are the modules
the model on screen was built from. Tell a
reader where a check comes from and what it requires with an options
object, as in Vitest:
it(
"Ledger is flashed",
{ layer: "code", requirement: "IRC R703.4: flashing over every ledger." },
({ q }) => expect(q("Ledger-Flashing").length).toBe(1),
);layer is one of bear, location, code, firm or model; a check
without one is the model's own. bear/checks/mechanical holds ready-made
checks for an imported Revit mechanical model.
Matchers
expect is the Jest/Vitest vocabulary: toBe, toEqual, toStrictEqual,
toBeCloseTo, toBeGreaterThan, toBeGreaterThanOrEqual, toBeLessThan,
toBeLessThanOrEqual, toBeDefined, toBeUndefined, toBeNull,
toBeTruthy, toBeFalsy, toContain, toContainEqual, toHaveLength,
toHaveProperty, toMatchObject, toThrow, and .not in front of any of
them. Measurements are floats in project units, so compare them with
toBeCloseTo, not toBe.
it and test are the same function; describe nests.
Interfaces
Expect
Defined in: bear/test.ts:201
The matcher surface. Every matcher also reads through .not.
Hand-declared rather than imported so it resolves through the bear mount;
the implementations come from @vitest/expect at runtime.
Properties
| Property | Modifier | Type |
|---|---|---|
not | readonly | Expect |
Methods
toBe()
toBe(expected): void;Defined in: bear/test.ts:202
Parameters
| Parameter | Type |
|---|---|
expected | unknown |
Returns
void
toBeCloseTo()
toBeCloseTo(expected, numDigits?): void;Defined in: bear/test.ts:205
Parameters
| Parameter | Type |
|---|---|
expected | number |
numDigits? | number |
Returns
void
toBeDefined()
toBeDefined(): void;Defined in: bear/test.ts:210
Returns
void
toBeFalsy()
toBeFalsy(): void;Defined in: bear/test.ts:214
Returns
void
toBeGreaterThan()
toBeGreaterThan(expected): void;Defined in: bear/test.ts:206
Parameters
| Parameter | Type |
|---|---|
expected | number |
Returns
void
toBeGreaterThanOrEqual()
toBeGreaterThanOrEqual(expected): void;Defined in: bear/test.ts:207
Parameters
| Parameter | Type |
|---|---|
expected | number |
Returns
void
toBeLessThan()
toBeLessThan(expected): void;Defined in: bear/test.ts:208
Parameters
| Parameter | Type |
|---|---|
expected | number |
Returns
void
toBeLessThanOrEqual()
toBeLessThanOrEqual(expected): void;Defined in: bear/test.ts:209
Parameters
| Parameter | Type |
|---|---|
expected | number |
Returns
void
toBeNull()
toBeNull(): void;Defined in: bear/test.ts:212
Returns
void
toBeTruthy()
toBeTruthy(): void;Defined in: bear/test.ts:213
Returns
void
toBeUndefined()
toBeUndefined(): void;Defined in: bear/test.ts:211
Returns
void
toContain()
toContain(expected): void;Defined in: bear/test.ts:215
Parameters
| Parameter | Type |
|---|---|
expected | unknown |
Returns
void
toContainEqual()
toContainEqual(expected): void;Defined in: bear/test.ts:216
Parameters
| Parameter | Type |
|---|---|
expected | unknown |
Returns
void
toEqual()
toEqual(expected): void;Defined in: bear/test.ts:203
Parameters
| Parameter | Type |
|---|---|
expected | unknown |
Returns
void
toHaveLength()
toHaveLength(expected): void;Defined in: bear/test.ts:217
Parameters
| Parameter | Type |
|---|---|
expected | number |
Returns
void
toHaveProperty()
toHaveProperty(path, value?): void;Defined in: bear/test.ts:218
Parameters
| Parameter | Type |
|---|---|
path | string |
value? | unknown |
Returns
void
toMatchObject()
toMatchObject(expected): void;Defined in: bear/test.ts:219
Parameters
| Parameter | Type |
|---|---|
expected | object |
Returns
void
toStrictEqual()
toStrictEqual(expected): void;Defined in: bear/test.ts:204
Parameters
| Parameter | Type |
|---|---|
expected | unknown |
Returns
void
toThrow()
toThrow(expected?): void;Defined in: bear/test.ts:220
Parameters
| Parameter | Type |
|---|---|
expected? | string | RegExp | Error |
Returns
void
TestContext
Defined in: bear/test.ts:139
What every test callback receives.
Properties
| Property | Type | Description |
|---|---|---|
q | QueryFn | bear/engine/query's entry point, pointed at this run's tree. The whole selector vocabulary works, not a narrowed subset: injecting q by its own contract type is what keeps it that way as the query API grows. |
result | ModelView | The whole model: diagnostics, edges and dims live here, not on nodes. |
tree | TreeNode | The model's tree itself, for assertions that don't need a query. |
TestFailure
Defined in: bear/test.ts:171
One failed assertion, named by its enclosing describes.
Properties
TestOptions
Defined in: bear/test.ts:163
What a test states beyond its name, for a reader of its result.
Extended by
Properties
| Property | Type | Description |
|---|---|---|
layer? | CheckLayer | Where the check comes from. Unset for an ordinary test. |
requirement? | string | The requirement in one line: what is checked and what passing means. |
TestReport
Defined in: bear/test.ts:185
The result of a runTests call.
Properties
| Property | Type | Description |
|---|---|---|
durationMs | number | - |
failed | number | - |
failures | TestFailure[] | - |
passed | number | - |
results | TestResult[] | Every registered test, in registration order. |
skipped | number | - |
TestResult
Defined in: bear/test.ts:177
One registered test's outcome.
Extends
Properties
| Property | Type | Description | Inherited from |
|---|---|---|---|
layer? | CheckLayer | Where the check comes from. Unset for an ordinary test. | TestOptions.layer |
message? | string | Why it failed; only a failed test has one. | - |
name | string | - | - |
requirement? | string | The requirement in one line: what is checked and what passing means. | TestOptions.requirement |
status | "passed" | "failed" | "skipped" | - | - |
Type Aliases
CheckLayer
type CheckLayer = "bear" | "location" | "code" | "firm" | "model";Defined in: bear/test.ts:160
Where a check comes from, from most general to most specific: Bear's own built-ins, the building's location, the codes and AHJ that govern it, the firm's standards, and checks written for this one model.
Source
type Source =
| BearElement
| (() => BearElement);Defined in: bear/test.ts:339
The two shapes a source file default-exports, matching the sandbox runtime.
TestFn
type TestFn = (ctx) => void | Promise<void>;Defined in: bear/test.ts:153
A test body. May be async.
Parameters
| Parameter | Type |
|---|---|
ctx | TestContext |
Returns
void | Promise<void>
Variables
it
const it: Register & object;Defined in: bear/test.ts:262
State one fact about the model.
Type Declaration
| Name | Type | Description |
|---|---|---|
only | Register | Run only the only tests in this file. |
skip | Register | Register but don't run. |
test
const test: Register & object = it;Defined in: bear/test.ts:270
Alias of it.
Type Declaration
| Name | Type | Description |
|---|---|---|
only | Register | Run only the only tests in this file. |
skip | Register | Register but don't run. |
Functions
describe()
function describe(name, fn): void;Defined in: bear/test.ts:239
Group related checks. Nests.
Parameters
| Parameter | Type |
|---|---|
name | string |
fn | () => void |
Returns
void
expect()
function expect(value): Expect;Defined in: bear/test.ts:326
Assert on a value. Only callable from inside a test: runTests loads the matchers before it runs anything, so a test body always finds them ready.
Parameters
| Parameter | Type |
|---|---|
value | unknown |
Returns
mergeReports()
function mergeReports(reports): TestReport;Defined in: bear/test.ts:441
Several runs as one report, results in the order given.
Parameters
| Parameter | Type |
|---|---|
reports | readonly TestReport[] |
Returns
runTests()
function runTests(root): Promise<TestReport>;Defined in: bear/test.ts:381
Run every test registered so far, against root: a source materialized fresh,
or a model that already exists.
Pass the model in a saved test file, so the file checks its own model rather
than whatever happens to be on screen. In the REPL, the built-in runTests()
passes the model on screen, so nothing re-materializes and the checks see the
same tree as the q you were just exploring with.
Draining the registry is what makes a second call safe: the sandbox caches modules by URL, so a re-run would otherwise replay every registration on top of the last set. It happens first, before anything that can throw, so a run that fails to even get a tree still consumes what it was going to check rather than leaking it into the next run.
A failing assertion is data, not an exception: the report comes back whole, so one bad check doesn't hide the rest.
Parameters
Returns
Promise<TestReport>