Skip to content

helix-plugin-ream ​

@c9up/helix-plugin-ream: the bridge between a ream app and the helix test runner: a plugin that puts an HTTP test client on each test's context, and the runner behind ream test. Guide: HTTP tests.

Installation ​

bash
pnpm add -D @c9up/helix-plugin-ream

@c9up/helix (^0.2.13) and @c9up/ream (^0.2.7) are peer dependencies. The client itself, TestClient, lives in @c9up/ream/testing.

Entry points ​

ImportContents
@c9up/helix-plugin-reamapiClient(), testClient(), their types, and the ctx.client augmentation of helix's TestContext.
@c9up/helix-plugin-ream/runnerrunTests(), runTestsFromRcFile() and their options: run the suites declared in the rc file.

Exports ​

@c9up/helix-plugin-ream ​

ExportKindDescription
apiClientfunctionA helix plugin that injects a fresh TestClient on every test as ctx.client.
testClientfunctionThe client the run is using, reachable without a test context.
errorsnamespaceThe package's errors, one class per code, see Errors. Also exported from ./runner.
ApiClientConfiginterfaceThe options of apiClient().
ClientHosttypePick<PluginApi, "context" | "cleanup">: the part of helix's plugin API the plugin uses.

Importing the package augments helix's TestContext with client: TestClient.

ts
function apiClient(config: ApiClientConfig): (api: ClientHost) => Promise<void>
function testClient(): TestClient

apiClient() gives each test its own client (headers set with withHeader() end with the test) over the server the file booted. With testUtils, the server is the one a suite hook starts, and a suite without the hook has no server. With boot, the plugin owns the server and starts it on the first request; its cleanup closes it.

testClient() returns the current test's ctx.client while a test runs, and the client of the file's server outside one (a fixture, a helper module, a runner hook).

ApiClientConfig:

FieldTypeDescription
testUtilsTestUtilsThe utilities whose httpServer() a suite hook starts; the plugin reads that server's client.
bootBootServerBoots the app directly, on the client's first request. For a project whose suites are not split.
authAuthStrategyThe auth strategy for client.withAuth() / asUser().
routesRouteManifestThe named-route manifest (router.namedManifest()) for client.visit().

One of testUtils or boot is required.

@c9up/helix-plugin-ream/runner ​

ExportKindDescription
runTestsFromRcFilefunctionLoads an rc file and runs its suites: the one-liner of a bin/test.ts.
runTestsfunctionRuns the given tests config. Returns the exit code.
workerNodeArgsfunctionThe flags the worker processes are spawned with: options.nodeArgs, or this process's execArgv.
RunTestsOptionsinterfaceWhat a caller may override on top of the rc file.
TestFiltersinterface{ suites?, files?, tags?, groups?, tests?: string[] }: the filters ream test forwards.
errorsnamespaceThe same namespace as the main entry's.
ts
function runTestsFromRcFile(rcPath: string, options?: RunTestsOptions): Promise<number>
function runTests(tests: TestsConfig | undefined, options?: RunTestsOptions): Promise<number>
function workerNodeArgs(options: RunTestsOptions): string[]

Both run functions set NODE_ENV=test and load the .env files of root (.env.test over .env, .env.local skipped) before anything else; runTestsFromRcFile does it before importing the rc file. rcPath is resolved against root. Prefer runTestsFromRcFile: it passes the rc path as configModule, which a suite's configure callback needs.

ts
// bin/test.ts
import { runTestsFromRcFile } from '@c9up/helix-plugin-ream/runner'

process.exitCode = await runTestsFromRcFile('./reamrc.ts', {
  suites: process.argv.slice(2),
})

RunTestsOptions:

OptionTypeDefaultDescription
rootstringprocess.cwd()Project root the suites resolve against.
suitesstring[]every declared suiteSuite names to run, in that order.
threadsnumberConcurrent worker processes.
reportersstring[]Reporter names (["spec", "json"]).
bailbooleanStops at the first failure.
coveragehelix's RunConfig["coverage"]V8 coverage, passed to helix untouched.
configModulestringset by runTestsFromRcFileThe module that declared the suites, re-imported in each worker for suites[].configure.
nodeArgsstring[]process.execArgvFlags the workers are spawned with.
passWithNoTestsbooleanfalseExits 0 when the selection finds no test file, or a file declares no test.
filtersTestFiltersNarrows the run: files, tags (~@tag excludes), group titles, test titles; filters.suites adds to suites.
timeoutnumberTimeout of every test in ms; beats a suite's own and the rc file's.
retriesnumberExtra attempts for every test; beats a suite's own.
failedbooleanRuns only the tests that failed during the last run.
watchbooleanWatches the files and re-runs on a change.
pollbooleanWith watch: detects changes by polling.
clearbooleantrueWith watch: clears the terminal before each re-run.
drainGuardbooleanfalseWhen the run finishes but the process stays alive, reports what is still open after a grace period and exits with the run's code. ream test turns it on.

When the rc file sets tests.forceExit, the process exits with the run's code as soon as the run ends.

Errors ​

CodeRaised when
E_HELIX_PLUGIN_REAM_NO_SERVERapiClient() was given neither testUtils nor boot.
E_HELIX_PLUGIN_REAM_SERVER_NOT_STARTEDA test reads ctx.client but no suite hook started the server.
E_HELIX_PLUGIN_REAM_NO_TEST_CLIENTtestClient() is called while no apiClient() plugin is in play.
E_HELIX_PLUGIN_REAM_UNKNOWN_SUITEA suite name was asked for but the rc file does not declare it.
E_HELIX_PLUGIN_REAM_SUITE_CONFIGURE_UNREACHABLEA suite declares configure and the runner has no module path to re-import it in each worker.

Each one is a class of errors, a flare Exception: read error.code, or test error instanceof errors.E_HELIX_PLUGIN_REAM_UNKNOWN_SUITE.

Migration ​

  • HelixPluginReamError, UnknownSuiteError and SuiteConfigureUnreachableError are gone: test error.code, or instanceof the matching class of errors (errors.E_HELIX_PLUGIN_REAM_UNKNOWN_SUITE, errors.E_HELIX_PLUGIN_REAM_SUITE_CONFIGURE_UNREACHABLE).
  • Three codes carry the package prefix now: E_NO_SERVER is E_HELIX_PLUGIN_REAM_NO_SERVER, E_SERVER_NOT_STARTED is E_HELIX_PLUGIN_REAM_SERVER_NOT_STARTED, E_NO_TEST_CLIENT is E_HELIX_PLUGIN_REAM_NO_TEST_CLIENT.

See also ​