@scwrap/domain/testing is the test kit of the domain SDK. A test imports it next to @effect/vitest. It never enters a Release.
| Export | Job |
|---|---|
ScraperTest.run | Run a Scraper once and return its items and its failure. |
FakeSite.make | An HttpClient Layer that answers from memory and records what was requested. |
FakeSite.layer | The same Layer without the record. |
Example (A Partial Run against a fake site)
import { expect, it } from "@effect/vitest";
import { FakeSite, ScraperTest } from "@scwrap/domain/testing";
import { Effect, Layer } from "effect";
import { Quotes, QuotesApiLive, quotesOrigin } from "../src/index.js";
it.effect("keeps delivered quotes when a later page is refused", () =>
Effect.gen(function* partialRun() {
const site = yield* FakeSite.make({
[url(1)]: page(1, true),
[url(2)]: 403,
});
const run = yield* ScraperTest.run(Quotes, { maxPages: 3 }).pipe(
Effect.provide(QuotesApiLive.pipe(Layer.provide(site.layer)))
);
expect(run.failure?._tag).toBe("Blocked");
expect(run.items).toHaveLength(1);
expect(yield* site.requests).toStrictEqual([url(1), url(2)]);
})
);ScraperTest.run
ScraperTest.run(definition, input)Runs the Scraper through Scraper.run, the code path an instance uses, and returns an Effect of { items, failure }. It needs the Implementation, which the test provides with its fake Capabilities.
| Field | Meaning |
|---|---|
items | Every item the Run delivered, in order, typed by the Definition's output schema. |
failure | null when the Run completed. Otherwise the error it ended with: one from the Definition's error union, InvalidInput or SchemaDrift. |
The Effect itself does not fail. A failed Run is a value, so one test can assert on the failure and on the items delivered before it. Items are collected as they arrive, which is what makes a Partial Run visible.
input is typed by the Definition's input schema, in its encoded form. A misspelt field does not compile. To prove that a Run rejects input the schema does not accept, pass it under // @ts-expect-error.
failure is the error value itself, so a test can compare it whole, with its own fields:
expect(outcome.failure).toStrictEqual(
new CaptchaUnsolved({ attempts: maxCaptchaAttempts })
);A defect, such as a bug that throws, is not a failure. It fails the test.
FakeSite
FakeSite.make(site) // Effect of { layer, requests }
FakeSite.layer(site) // the Layer aloneBoth build a Layer that provides HttpClient. site says how the fake answers, in one of two forms.
Example (Routes by URL)
FakeSite.layer({
"https://source.example/feed": '{"rows":[]}',
"https://source.example/limited": 429,
"https://source.example/down": FakeSite.unreachable,
"https://source.example/page": new Response("<p>ok</p>", {
headers: { "content-type": "text/html; charset=utf-8" },
}),
});A URL without a route answers 404.
Example (A function of the request)
FakeSite.layer((url, request) =>
request.headers.cookie === undefined ? 403 : `<p>${url.pathname}</p>`
);| Reply | The fake answers |
|---|---|
| A string | HTTP 200 with that body. |
| A number | That status with an empty body. |
A Response | That response. It can be served any number of times. |
FakeSite.unreachable | A transport error, as when the site cannot be reached. |
FakeSite.make also records. site.requests is an Effect of the requested URLs, in order. Assert on it to prove what the Scraper asked for and, for invalid input, that it asked for nothing.
Retries and the test clock
Download retries a transient failure after a delay. it.effect runs on a test clock, so a test of a retried failure forks the Run, advances the clock with TestClock.adjust from effect/testing and then joins it. The Earthquakes acceptance test does this for every fault it generates.
Other Capabilities
The kit fakes HttpClient only. A fake for another Capability is a Layer the test writes:
ProxiedHttpClient: a provider that records the routes it was asked for.Browser: a real Chromium with routed fixture pages.TextCaptchaSolver: a solver scripted with the readings a test needs, as in thefirstConfidenttest.
Source
- testing.ts
- scraper-layer.test.ts and download.test.ts, which use the kit