End-to-end tests

Test your buckets, rules and functions against a disposable Spica instance with @spica-devkit/testing and Vitest.

@spica-devkit/testing starts a real Spica API with your project's resources installed. Your tests then call it exactly like your apps do, which makes it the best way to test rules, policies and functions together.

Setup

Terminal
npm install --save-dev vitest @spica-devkit/testing @spica-devkit/bucket @spica-devkit/auth

The project folder contains your resource files, for example spica/bucket/Posts/schema.yaml and spica/function/auth/.

vitest.config.ts
import {defineConfig} from "vitest/config";

export default defineConfig({
  test: {
    hookTimeout: 180_000,
    testTimeout: 30_000,
    fileParallelism: false
  }
});

A test file

test/posts.e2e.test.ts
import * as Auth from "@spica-devkit/auth";
import * as Bucket from "@spica-devkit/bucket";
import {start} from "@spica-devkit/testing";
import {afterAll, afterEach, beforeAll, expect, test} from "vitest";

let spica: Awaited<ReturnType<typeof start>>;
let postsBucketId: string;

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

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

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

  const buckets = await Bucket.getAll();
  postsBucketId = buckets.find(bucket => bucket.title == "Posts")!._id;
});

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

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

test("users only see published posts", async () => {
  await Bucket.data.insert(postsBucketId, {title: {en_US: "Draft"}, slug: "draft", status: "draft"});
  await Bucket.data.insert(postsBucketId, {title: {en_US: "Live"}, slug: "live", status: "published"});

  const reader = await Auth.signUp({username: "reader@example.com", password: "readerPass1"});
  await Auth.policy.attach(reader._id, ["BucketReadOnlyAccess"]);
  const session = await Auth.signIn("reader@example.com", "readerPass1");

  const response = await fetch(`${spica.publicUrl}/bucket/${postsBucketId}/data`, {
    headers: {Authorization: `USER ${session.token}`}
  });
  const posts = await response.json();

  expect(posts.map(post => post.slug)).toEqual(["live"]);
});

test("slugs are validated", async () => {
  await expect(
    Bucket.data.insert(postsBucketId, {title: {en_US: "Bad"}, slug: "Not A Slug"})
  ).rejects.toBeTruthy();
});

publicUrl has no /api suffix: the testing instance serves the API directly.

Testing functions

HTTP functions are served at ${spica.publicUrl}/fn-execute/<path>:

TypeScript
test("sign-up returns a token", async () => {
  const response = await fetch(`${spica.publicUrl}/fn-execute/auth/sign-up`, {
    method: "POST",
    headers: {"Content-Type": "application/json"},
    body: JSON.stringify({email: "new@example.com", password: "newUserPass1"})
  });

  expect(response.status).toBe(201);
  expect((await response.json()).token).toBeTypeOf("string");
});

Install only the resources a test file needs to make boots faster:

TypeScript
spica = await start({resourcePath: "./spica", installResources: {bucket: ["Posts"], function: ["auth"]}});

In CI

GitHub's ubuntu-latest runners include Docker:

YAML
- uses: actions/setup-node@v4
  with:
    node-version: 22
- run: npm ci
- run: npx vitest run test/*.e2e.test.ts

Tips

  • Use reset(["bucket-data"]) between tests and a fresh instance per test file.
  • Run test files one at a time (fileParallelism: false) unless each file starts its own instance on purpose.
  • Test as a user when you test rules. Identities and API keys bypass them.