Skip to main content
fakeAttract() puts a fake site in the container for one test: it sends nothing, every call answers a valid model, and you assert what was called. Every controller, service and job that injects SiteClient (or SiteClients) gets the fake, and the real client is back when the test ends.
Terminal

Through a route

tests/functional/blog_posts.spec.ts
Nothing is sent and no key is read: the placeholder in .env.test (Configuration) only satisfies start/env.ts. fakeAttract() answers the fake itself, with its assertions.

A service, directly

tests/unit/blog_service.spec.ts
app.container.make() builds the service with the fake injected. BlogService caches the site’s answer (Jobs, cache and health), so each test starts from an empty cache.

Answers and refusals

Give a method its answer by key, <accessor>.<method>. fakeAttract() takes the SDK’s answers, all on TypeScript → Testing, and the package hands you every helper:
The assertions (assertCalled, assertCalledTimes, assertNotCalled, assertNothingCalled) throw node:assert’s AssertionError, which Japa reports like its own.

Your own validation

A request your validator refuses never reaches the site:
tests/functional/testimonials.spec.ts

Many sites

fakeAttractSites() fakes every site and answers the directory: .site('globex') is that site’s fake, to assert on. The SitesReport service is on Configuration:
tests/unit/sites_report.spec.ts

An Ace command of your own

Ace runs a command inside the test, and the fake reaches it like any other class. A command that prints the site’s newest posts:
tests/unit/latest_posts.spec.ts
The command is on Ace commands. In raw mode Ace keeps the command’s output to assert on instead of printing it, its colors written as names. Commands live outside app/, so import them by path. A command that prompts takes command.prompt.trap('…').replyWith('…') for each question before exec().

A queue job

With QUEUE_DRIVER=sync in .env.test, dispatch() runs the job at once, through the container, so the fake reaches it:
tests/unit/sync_blog_posts.spec.ts
The job is on Jobs, cache and health.

Next: Ace commands

Store a key, check a site, and a REPL with the client loaded.