Skip to content

Test kit

@scwrap/domain/testing runs a Scraper once with typed items and a typed failure, and fakes the site behind an HttpClient.

@scwrap/domain/testing is the test kit of the domain SDK. A test imports it next to @effect/vitest. It never enters a Release.

ExportJob
ScraperTest.runRun a Scraper once and return its items and its failure.
FakeSite.makeAn HttpClient Layer that answers from memory and records what was requested.
FakeSite.layerThe same Layer without the record.

Example (A Partial Run against a fake site)

ts
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 ​

ts
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.

FieldMeaning
itemsEvery item the Run delivered, in order, typed by the Definition's output schema.
failurenull 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:

ts
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 ​

ts
FakeSite.make(site)   // Effect of { layer, requests }
FakeSite.layer(site)  // the Layer alone

Both build a Layer that provides HttpClient. site says how the fake answers, in one of two forms.

Example (Routes by URL)

ts
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)

ts
FakeSite.layer((url, request) =>
  request.headers.cookie === undefined ? 403 : `<p>${url.pathname}</p>`
);
ReplyThe fake answers
A stringHTTP 200 with that body.
A numberThat status with an empty body.
A ResponseThat response. It can be served any number of times.
FakeSite.unreachableA 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:

Source ​