Skip to content

Testing observable queries

ObservableQueryScenario opens a live query through the real observable pipeline and collects what it emits, within a deadline you set.

The Tasks sample spec registers a task and collects the current value:

import { given } from '@cratis/arc.testing';
import { TaskId } from '../../../TaskId.js';
import { TaskTitle } from '../../../TaskTitle.js';
import { a_task_listing } from '../given/a_task_listing.js';
describe('when collecting the current tasks emission', given(a_task_listing, context => {
let result: Awaited<ReturnType<typeof context.observable.collect>>;
beforeEach(async () => {
context.tasks.register(TaskId.create(), new TaskTitle('Review changes'));
result = await context.observable.collect(1, 1_000);
});
afterAll(async () => { await context.query.dispose(); await context.observable.dispose(); });
it('should receive the wire-shaped current value', () => {
result.emissions.should.have.lengthOf(1);
String(result.emissions[0]?.data?.[0]?.title).should.equal('Review changes');
});
}));

The a_task_listing context is shown on Testing queries.

collect(count, timeoutMs = 5000, arguments = {}, options?) waits for up to count emissions, or fails at the deadline, which also covers opening the subscription. The scenario closes its subscription in every case, including a timeout.

Result fieldMeaning
emissionsThe query results received, wire-shaped
completedtrue only when a finite stream completed before reaching count
rejectionThe query result when opening was rejected, such as a failed authorization or validation

A rejection is reported in rejection with completed: false, not as an emission. Always assert the expected emission count.