Given
A block's function usually needs data from around it, like the id to fetch. A closure works, but a test can't see what it captured:
const comments = (postId: number) =>
extern.typed.by(Comments).will(() => fetchComments(postId));given passes the data through the chain instead. The function receives it as its only argument, and the spy records it:
import { } from "@ghostry/extern";
import { , } from "bun:test";
const = ();
declare const : (: number) => <string[]>;
const = .<string[]>();
const = (: number) =>
..().().();
("asks for the post's comments", async () => {
await .(async () => {
const = ().([]);
await (123);
(.[0]?.).(123);
});
});given takes one value. To pass several, pass an object:
extern.typed
.by(Comments)
.given({ postId, page })
.will(({ postId, page }) => fetchComments(postId, page));On effect blocks
An effect block takes given too, after named, since only a named effect can be spied on:
extern.effect.named("track view").given(postId).will(sendViewMetric);See Effects.
Where it goes in the chain
given comes after by and named, and right before will:
extern.typed.by(Comments).named("first page").given(postId).will(fetchComments);