Server

Run the server tests: the full application stack, including the database.

Syntax

$ vela test:server
$ vela test:server pets

This is vitest run --project server with vela’s defaults: the dot reporter, CI mode, and a pass on an empty suite. npm run test:unit runs the other project; npm test runs both. A bare positional argument filters which tests run, and any other arguments are passed straight through to Vitest. The command exits non-zero when a test fails, so it can gate CI.

With no filter of your own, a project that has no server tests yet passes — the state a project is in right after vela enable backend. A filter that matches no test file still fails.

The servers

The servers the tests need are started by Vitest itself, not by this command. vitest.config.ts names vela/testing as a global setup:

{
	name: 'server',
	include: ['src/**/*server.{test,spec}.{js,ts}'],
	globalSetup: ['vela/testing'],
	setupFiles: ['test/setup.ts']
}

Before the first test file, it:

  • Creates a throwaway PocketBase database under test-data/, with all migrations applied and the app’s hooks loaded
  • Loads seeds, then fixtures, into it
  • Starts a Vite server with the page components stubbed out

After the last, it stops both and removes the database. Both run in a process of their own, started from the vela the project installed, so the app’s Vite server never shares a process with Vitest’s.

Because the setup is part of the Vitest config, the same servers come up for npm test, npx vitest, npx vitest --watch and an editor’s test runner. In watch mode they stay up for the whole session: the database is created once, and the tests clean up after themselves. Each run gets a directory of its own under test-data/, so a watch session in the editor and a run in the terminal do not share a database. Directories left behind by a run that died are removed by the next one.

Test files

Server tests are the files named server.test.ts or <name>.server.test.ts under src, and run in the Vitest project named server; every other test file is a unit test. Component tests — *.svelte.test.ts — are excluded from both. The generators write a server.test.ts next to the routes they create, and a <name>.server.test.ts next to each workflow; a workflow test starts a worker in the test process, so the run completes there. vela generate form and vela enable ai only write theirs when the project has supertest installed.

Server tests reach the app over HTTP and import nothing from it, so Vitest’s watcher cannot tell which tests an edit to a route affects. The config lists src/routes, src/lib/server, src/hooks.server.ts and data/hooks under forceRerunTriggers, and an edit there reruns the whole suite.

Test context

test/setup.ts gives every test a fresh user and a set of clients, so tests read as HTTP requests against the running app. vela enable backend creates it, and the vitest.config.ts that loads it, in a project that has neither:

  • request - A supertest client, unauthenticated
  • agent - A supertest agent that keeps cookies, with authenticateUser()
  • admin - The PocketBase admin client
  • pb - The PocketBase client scoped to the test user
  • user - The user record created for this test

The setup file finds the servers with inject('vela'), which gives the PocketBase URL, the app URL, the superuser’s credentials and the run’s data directory. The same values are also in the environment, for code that reads it: POCKETBASE_URL, POCKETBASE_SUPERUSER_EMAIL, POCKETBASE_SUPERUSER_PASSWORD, VITE_TEST_URL, VELA_DATA_DIR, and TEST=true. The project’s .env is loaded first, as every vela command loads it, without replacing anything already set. Variables set under test.env in the config reach the app as well as the tests.

Frontend

Page, layout and error components are replaced with stubs for the duration of the run. Server tests are only concerned with the backend and the database, and skipping the frontend bundle is a large speed win.

Seeds and fixtures

The test database is not empty. After the migrations, every file in data/seeds is loaded, then every file in data/fixtures, through the same server the tests talk to — so the app’s hooks apply to these records as they would to any write. A record the current schema rejects, or a seed record without an id, stops the run and is named in the error; vela fixtures regen rewrites the fixtures from the schema.

Test files run in parallel against one database, so treat seeds and fixtures as a read-only baseline: read them and look records up by id, but create your own records for anything a test changes or deletes. The generated tests already work this way.