Effects
Some external calls produce nothing your code uses: sending a metric, writing a log line, publishing an event. Wrap them with extern.effect:
extern.effect.will(() => sendMetric("login.success"));An effect block has no identity, so there's no by. Its function must return void or Promise<void>.
What a testing block does with them
Outside a testing block, an effect block runs its function. Inside one, it doesn't run, and it doesn't need a mock: an unmocked effect block does nothing and throws nothing.
So a test doesn't have to account for every metric its code sends, but it still sends none.
Named effects
To see an effect block in a test, name it:
export const trackView = (postId: number) =>
extern.effect.named("track view").given(postId).will(sendViewMetric);An unnamed effect block can't be spied on. given is available only after named. See Given.
An effect is matched by its name alone, so a spy on "track view" records every effect block with that name. Give effects you spy on names that say which call they are.
Spying on effects
mock.effect.named(name) targets named effect blocks, and has two terminals:
import { } from "@ghostry/extern";
import { , } from "bun:test";
const = ();
declare const : (: number) => void;
const = (: number) =>
..("track view").().();
("tracks the view", () => {
.(() => {
const = ..("track view").();
(123);
(.).(1);
(.[0]?.).(123);
});
});observe()records each call, and the block still doesn't run.passthrough()records each call, and runs the block's function.
As with value mocks, an effect spy that records no call throws UnusedMocksError when the testing block ends, unless it was given { unused: "allow" }. See Mocking.
