@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/testingDocker 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?)
| Option | Default | Description |
|---|---|---|
version | "latest" | spicaengine/api image tag. |
mongoVersion | "8.0" | mongo image tag. |
resourcePath | "./" | Folder with CLI-format resources (bucket/, function/, ...). |
installResources | true | true, false, or a selection such as {bucket: ["Posts"], function: ["seed"]}. |
port | Free port | Host port of the API. |
name | Generated | Name used to label containers. |
identifier, password | "spica" | Default identity credentials. |
masterKey | Random | --master-key of the API. |
apiOptions | {} | Extra API options, such as {"activity-stream": false}. |
imagePullPolicy | "if-not-present" | "always" pulls images every time. |
readyTimeoutMs | 120000 | How 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
| Member | Description |
|---|---|
publicUrl | API 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)
| Module | Effect |
|---|---|
bucket-data | Drops every bucket's documents. Schemas are kept. |
bucket | Drops documents and bucket schemas. |
identity | Deletes identities except the default one, and refresh tokens. |
user | Deletes users. |
apikey | Deletes API keys. |
function | Deletes function records. Loaded functions keep running until a restart. |
storage | Deletes 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.