@spica-devkit/testing

Start a disposable Spica instance from your test suite, install your project's resources, reset data between tests and tear it down.

@spica-devkit/testing starts a minimal Spica instance in Docker for end-to-end tests: a single-node MongoDB replica set and the API container, without the panel or an ingress. It installs your project's resource files and works with any test framework.

Terminal
npm install --save-dev @spica-devkit/testing

Docker must be running. The sync engine is bundled, so you don't need the CLI.

Example with Jest or Vitest

TypeScript
import {start} from "@spica-devkit/testing";
import * as Bucket from "@spica-devkit/bucket";

let spica;

beforeAll(async () => {
  spica = await start({version: "0.22.1", resourcePath: "./"});

  const response = await fetch(`${spica.publicUrl}/passport/identify`, {
    method: "POST",
    headers: {"Content-Type": "application/json"},
    body: JSON.stringify({identifier: "spica", password: "spica"})
  });
  const {token} = await response.json();

  Bucket.initialize({identity: token, publicUrl: spica.publicUrl});
}, 180_000);

afterEach(() => spica.reset(["bucket-data"]));

afterAll(() => spica.teardown());

test("published posts are listed", async () => {
  await Bucket.data.insert(POSTS_BUCKET_ID, {title: "Hello", slug: "hello", status: "published"});
  const posts = await Bucket.data.getAll(POSTS_BUCKET_ID);
  expect(posts).toHaveLength(1);
});

start(options?)

OptionDefaultDescription
version"latest"spicaengine/api image tag.
mongoVersion"8.0"mongo image tag.
resourcePath"./"Folder with CLI-format resources (bucket/, function/, ...).
installResourcestruetrue, false, or a selection such as {bucket: ["Posts"], function: ["seed"]}.
portFree portHost port of the API.
nameGeneratedName used to label containers.
identifier, password"spica"Default identity credentials.
masterKeyRandom--master-key of the API.
apiOptions{}Extra API options, such as {"activity-stream": false}.
imagePullPolicy"if-not-present""always" pulls images every time.
readyTimeoutMs120000How long to wait for the API to accept logins.

Installing only what a test file needs keeps boot times short, because every installed function is built.

The returned handle

MemberDescription
publicUrlAPI base URL, such as http://localhost:54231. There is no /api suffix.
waitForReady()Wait until the API accepts logins.
reset(modules?)Wipe state between tests.
teardown()Remove every container, network and volume of the instance.

reset(modules)

ModuleEffect
bucket-dataDrops every bucket's documents. Schemas are kept.
bucketDrops documents and bucket schemas.
identityDeletes identities except the default one, and refresh tokens.
userDeletes users.
apikeyDeletes API keys.
functionDeletes function records. Loaded functions keep running until a restart.
storageDeletes storage metadata. Files stay on the volume.
all (default)All of the above.

Resets drop MongoDB collections directly, which is fast but bypasses the API. Prefer bucket-data, identity and storage between tests.