Skip to Content
Bear documentation

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

PropertyModifierType
notreadonlyExpect

Methods

toBe()
toBe(expected): void;

Defined in: bear/test.ts:202

Parameters
ParameterType
expectedunknown
Returns

void

toBeCloseTo()
toBeCloseTo(expected, numDigits?): void;

Defined in: bear/test.ts:205

Parameters
ParameterType
expectednumber
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
ParameterType
expectednumber
Returns

void

toBeGreaterThanOrEqual()
toBeGreaterThanOrEqual(expected): void;

Defined in: bear/test.ts:207

Parameters
ParameterType
expectednumber
Returns

void

toBeLessThan()
toBeLessThan(expected): void;

Defined in: bear/test.ts:208

Parameters
ParameterType
expectednumber
Returns

void

toBeLessThanOrEqual()
toBeLessThanOrEqual(expected): void;

Defined in: bear/test.ts:209

Parameters
ParameterType
expectednumber
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
ParameterType
expectedunknown
Returns

void

toContainEqual()
toContainEqual(expected): void;

Defined in: bear/test.ts:216

Parameters
ParameterType
expectedunknown
Returns

void

toEqual()
toEqual(expected): void;

Defined in: bear/test.ts:203

Parameters
ParameterType
expectedunknown
Returns

void

toHaveLength()
toHaveLength(expected): void;

Defined in: bear/test.ts:217

Parameters
ParameterType
expectednumber
Returns

void

toHaveProperty()
toHaveProperty(path, value?): void;

Defined in: bear/test.ts:218

Parameters
ParameterType
pathstring
value?unknown
Returns

void

toMatchObject()
toMatchObject(expected): void;

Defined in: bear/test.ts:219

Parameters
ParameterType
expectedobject
Returns

void

toStrictEqual()
toStrictEqual(expected): void;

Defined in: bear/test.ts:204

Parameters
ParameterType
expectedunknown
Returns

void

toThrow()
toThrow(expected?): void;

Defined in: bear/test.ts:220

Parameters
ParameterType
expected?string | RegExp | Error
Returns

void


TestContext

Defined in: bear/test.ts:139

What every test callback receives.

Properties

PropertyTypeDescription
qQueryFnbear/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.
resultModelViewThe whole model: diagnostics, edges and dims live here, not on nodes.
treeTreeNodeThe 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

PropertyType
messagestring
namestring

TestOptions

Defined in: bear/test.ts:163

What a test states beyond its name, for a reader of its result.

Extended by

Properties

PropertyTypeDescription
layer?CheckLayerWhere the check comes from. Unset for an ordinary test.
requirement?stringThe 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

PropertyTypeDescription
durationMsnumber-
failednumber-
failuresTestFailure[]-
passednumber-
resultsTestResult[]Every registered test, in registration order.
skippednumber-

TestResult

Defined in: bear/test.ts:177

One registered test's outcome.

Extends

Properties

PropertyTypeDescriptionInherited from
layer?CheckLayerWhere the check comes from. Unset for an ordinary test.TestOptions.layer
message?stringWhy it failed; only a failed test has one.-
namestring--
requirement?stringThe 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

ParameterType
ctxTestContext

Returns

void | Promise<void>

Variables

it

const it: Register & object;

Defined in: bear/test.ts:262

State one fact about the model.

Type Declaration

NameTypeDescription
onlyRegisterRun only the only tests in this file.
skipRegisterRegister but don't run.

test

const test: Register & object = it;

Defined in: bear/test.ts:270

Alias of it.

Type Declaration

NameTypeDescription
onlyRegisterRun only the only tests in this file.
skipRegisterRegister but don't run.

Functions

describe()

function describe(name, fn): void;

Defined in: bear/test.ts:239

Group related checks. Nests.

Parameters

ParameterType
namestring
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

ParameterType
valueunknown

Returns

Expect


mergeReports()

function mergeReports(reports): TestReport;

Defined in: bear/test.ts:441

Several runs as one report, results in the order given.

Parameters

ParameterType
reportsreadonly TestReport[]

Returns

TestReport


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

ParameterType
rootModelView | Source

Returns

Promise<TestReport>

On this page