Test Database Configuration#
Cedar's test infrastructure handles three interconnected concerns: routing the cedar test command to the right test runner (Jest or Vitest), setting up the DATABASE_URL for API tests, and synchronizing the Prisma schema against a test database before tests run. Both Jest and Vitest paths share the same environment variable conventions and Prisma sync logic.
CLI Routing: Jest vs. Vitest#
The top-level cedar test command is registered in packages/cli/src/index.ts using a runtime check:
projectIsEsm() ? testCommandEsm : testCommand
- Non-ESM projects load
commands/test.ts, which delegates totestHandler.tsβ the Jest path. - ESM projects load
commands/testEsm.ts, which delegates totestHandlerEsm.tsβ the Vitest path.
Both command files expose --db-push (default true) and --force . The Jest command additionally exposes --watch and --collect-coverage .
Database URL Resolution#
Both handlers resolve DATABASE_URL with the same fallback chain :
- Use
TEST_DATABASE_URLif set. - Otherwise, default to a SQLite file at
{generated.base}/test.dbβ e.g..cedar/test.db.
The resolved value is injected into the child process environment when Jest/Vitest is spawned .
If --db-push false is passed and the api side is included, the handler sets SKIP_DB_PUSH=1 to skip schema synchronization entirely .
Prisma Schema Synchronization#
Schema sync runs once before tests begin. The implementation is duplicated across two entry points that share the same core reset functionality:
- Jest:
packages/testing/src/config/jest/api/globalSetup.tsβ runs as Jest'sglobalSetup. - Vitest:
packages/testing/src/api/vitest/CedarApiVitestEnv.tsβ runs inside the customcedar-apiVitest environment'ssetup()hook.
The Vitest implementation captures stdout and stderr to provide detailed error diagnostics when the database reset fails, whereas the Jest implementation uses inherited stdio. This improves the developer experience in Vitest by showing Prisma's actual error messages when reset operations fail.
Both entry points:
- Check
SKIP_DB_PUSH=1and bail early if set . - Set
DATABASE_URLtoTEST_DATABASE_URLor the SQLite fallback . - Choose a Prisma command based on
TEST_DATABASE_STRATEGY:TEST_DATABASE_STRATEGY=resetβprisma migrate reset --force- (default) β
prisma db push --force-reset --accept-data-loss
- Run the command synchronously via
execa.sync, adjusting the executor for pnpm vs. npx .
Vitest API Environment (cedar-api)#
The Vitest path uses a custom environment named cedar-api , wired up by the cedarVitestApiConfigPlugin. The plugin sets:
test.environmentβ path toCedarApiVitestEnv.jstest.fileParallelism: falseβ because all API tests share a single database and cannot run concurrentlytest.setupFilesβvitest-api.setup.js
Note: The
poolOptions.forks.singleForkworkaround used in Vitest 3 was replaced byfileParallelism: falsewhen Vitest 4 droppedpoolOptions.
For Jest, the equivalent serial execution is enforced by the jest-serial-runner in the API Jest preset .
Safety: Non-Standard Datasource URLs#
When the api side is included, both handlers call warnIfNonStandardDatasourceUrl before running tests. If Prisma is configured with an environment variable other than DATABASE_URL, Cedar warns that it will not override it β which could direct destructive reset commands at a production database. The --force flag bypasses the interactive prompt.
Environment Variables Reference#
| Variable | Effect |
|---|---|
TEST_DATABASE_URL | Override the test database URL (any Prisma-compatible URL) |
SKIP_DB_PUSH | Set to 1 to skip Prisma schema sync entirely |
TEST_DATABASE_STRATEGY | Set to reset to use prisma migrate reset instead of prisma db push |
Key Source Files#
| File | Purpose |
|---|---|
packages/cli/src/index.ts | Routes cedar test to Jest or Vitest based on projectIsEsm() |
packages/cli/src/commands/test.ts | Jest command definition |
packages/cli/src/commands/testEsm.ts | Vitest command definition |
packages/cli/src/commands/test/testHandler.ts | Jest handler: DB URL resolution, SKIP_DB_PUSH, runs Jest |
packages/cli/src/commands/test/testHandlerEsm.ts | Vitest handler: DB URL resolution, SKIP_DB_PUSH, runs Vitest |
packages/testing/src/config/jest/api/globalSetup.ts | Jest global setup: Prisma schema sync |
packages/testing/src/api/vitest/CedarApiVitestEnv.ts | Vitest cedar-api environment: Prisma schema sync |
packages/testing/src/api/vitest/vite-plugin-cedar-vitest-api-config.ts | Vite plugin wiring the cedar-api environment + serial execution |