Skip to main content
Every GatoJS project ships with four kinds of tests already wired up. Each one replaces a different part of the system, so each catches different bugs. A unit test with a mocked Prisma checks the order of steps in a command, but it will never notice a missing organizationId filter, because the mock returns whatever the test prepared regardless of where. Only an integration test on a real database with two organizations catches that.

Test types

npm run verify is the gate you run before handing in work: lint, typecheck, npm test, the production build, workspace package checks and hook tests. It does not run integration, e2e or mutation tests, because they need a database or a browser, and a full mutation run takes about an hour. Integration and e2e tests run in separate CI jobs; mutation tests run locally or when you trigger the workflow manually. So before you hand in a change to a query, a command or the sign-in flow, run them yourself.

Unit and component tests

npm test runs Vitest with vitest.config.ts:
  • jsdom environment so component tests can render React. A file that needs plain Node starts with // @vitest-environment node.
  • vitest.setup.ts adds the Testing Library DOM matchers (such as toBeInTheDocument) and loads .env.local and .env. Tests see DATABASE_URL but never connect, because Prisma is replaced.
  • Files: src/**/*.test.ts(x), scripts/**/*.test.ts and e2e/**/*.test.ts, plus project packages in packages/*/src. *.integration.test.ts files are excluded.
  • globals: true gives you describe, it and expect without imports; clearMocks: true resets mock call history before each test.
  • server-only is aliased to an empty module, so server files can be imported in tests.
Replace only inputs and outputs: Prisma, mail, the network and better-auth’s auth.api. Keep the file containing the security rule under test real. If you mock the file that checks the admin role, the test can no longer see that rule. There are two ways to replace Prisma:
  • vi.mock("@/shared/lib/prisma", …) with vi.fn() functions, when the test checks the order of steps and the data passed (see src/features/users/services/commands/__tests__/invite-user-command.test.ts).
  • createFakePrisma from src/test/fake-prisma.ts, when the test must prove that where really filters. It is an in-memory Prisma for the users, organizations, members and sessions tables that evaluates where (AND, OR, NOT, equals, in, notIn, not, relation filters), orderBy, select, include, take and skip. An unsupported operator throws, so a test never passes silently.
Run one file with npx vitest run <path>, or watch mode with npm run test:watch.

Integration tests

Integration tests exercise queries, commands and guards on real Postgres. They live in *.integration.test.ts files and are not part of npm test. The core has one, the organization isolation test; kit modules add their own. vitest.integration.config.ts:
  • node environment, files src/**/*.integration.test.ts;
  • a globalSetup that creates the test database if missing and syncs its schema with prisma db push;
  • a setup file that points DATABASE_URL at the test database and sets the variables the app needs (auth secret, APP_URL, EMAIL_FROM, API_KEY_PEPPER, S3 settings for MinIO, MULTI_ORG=false, AUTH_PUBLIC_SIGNUP=false);
  • fileParallelism: false, because every file shares one database and every test starts by truncating tables;
  • testTimeout: 20_000 and hookTimeout: 60_000.
Helpers in src/test/integration/database.ts:

Run them

1

Start the database

With the uploads module, also start MinIO and create the bucket (these services use the s3 profile):
2

Run the tests

The default target is postgresql://postgres:postgres@localhost:5432/wa_app_test, and globalSetup creates it if needed. If your Postgres runs on another port, set the URL in the shell:
Vitest does not load .env into process.env here, so a value in .env is ignored and the tests fall back to port 5432.
3

Check where they ran

Prisma prints Datasource "db": PostgreSQL database "wa_app_test", schema "public" at "localhost:5432" first. Check the database name and port.
Run a single file with npm run test:integration -- <path>. Plain npx vitest run <path> uses the unit config, skips integration files and exits with No test files found, exiting with code 1.

End-to-end tests

E2E tests check flows that involve every part at once in a browser: invitation by email, setting a password, sign-in, sign-up and creating an organization. npm run test:e2e:
  1. runs npm run build (standalone output) and copies .next/static into the standalone folder;
  2. starts two node .next/standalone/server.js servers with different settings and waits for /sign-in to respond (up to 120 seconds);
  3. runs e2e/global-setup.ts: checks the database name, drops and rebuilds the e2e database, creates the admin account and empties the mailpit inbox;
  4. runs two Playwright projects, each against its own server.
Tests run serially with one worker and no retries; traces are kept only for failures. Tests find elements by Polish UI labels, most of them in the LABELS constant in e2e/helpers.ts.

Before the first run

1

Start the database and mailpit

docker compose up -d. The wa_app_e2e database is created by Prisma.
2

Install Chromium once

3

Free ports 3000 and 3001

Stop npm run dev. Playwright doesn’t reuse a running server and fails before touching the database if a port is busy. Use E2E_CORE_PORT and E2E_MULTI_ORG_PORT to change them.
4

Pass custom ports in the shell

The e2e config reads ports only from environment variables, not from .env:
Prisma refuses db push --force-reset and migrate reset when it detects it was started by an AI agent (for example inside Claude Code), unless PRISMA_USER_CONSENT_FOR_DANGEROUS_AI_ACTION is set. So npm run test:e2e launched by an agent stops at the database reset. Run e2e tests yourself, or let an agent run them only with your explicit consent. Integration tests usually work under an agent, because a plain prisma db push only asks when a schema change would drop data.

Commands that reset a database

Integration and e2e tests delete data in their database on every run. The only safeguard is the database name: integration tests run only on a database whose name ends in _test, e2e tests only on one ending in _e2e. The host and port are not checked. Your development database wa_app is safe, but any other database with one of those suffixes, including one in another project’s Postgres, will be wiped.
npm test, npm run verify, npm run test:hooks, npm run test:mutation, npm run test:mutation:task and npm run test:mutation:gate never touch a database. What the name check does not cover:
  • The server. Every GatoJS project uses the same default names wa_app_test and wa_app_e2e, and both commands default to port 5432. If you moved your Postgres to another port because 5432 belongs to another project, and didn’t pass the port in the shell, the tests will wipe that project’s test database. Check echo "$DB_PORT $DATABASE_URL $INTEGRATION_DATABASE_URL" before running.
  • What the database is for. Any name with the suffix passes, such as orders_test. Don’t give those names to databases you want to keep.
  • Manual Prisma commands, which work on DATABASE_URL with no name check.
The e2e setup also deletes every email from mailpit at MAILPIT_URL (default http://localhost:8025), which is the same mailpit npm run dev sends to.

Organization isolation test

Prisma does not filter by organization on its own. Every query and command adds the organizationId filter from getOrgContext(), and the kit’s runtime tenant-scope guard throws when a scoped model is queried without one. What neither the guard nor a mocked unit test can tell you is whether the filter uses the right organization. That takes a test on real Postgres with two organizations, and the project rules require one for every new query and command on domain data. See Organizations for the data model. Add an isolation test when you add:
  • a query that reads organization data;
  • a command that creates, updates or deletes organization data (the most common leak is updating a record by an id from the input without organizationId in where);
  • a change to where in an existing query or command;
  • a fix for a leak. The test must fail without the fix.
Components and pure functions without Prisma don’t need one.

How to build one

Create a separate file such as src/features/<x>/services/__tests__/<x>-isolation.integration.test.ts.
1

Copy the stubs and signInAs

The session stub (currentSession, readMockedSession in a vi.hoisted block), the vi.mock calls and signInAs are defined in src/features/organizations/services/__tests__/organization-isolation.integration.test.ts itself, so your file needs its own copy. They replace what doesn’t exist outside an HTTP request: headers, locale, notFound, redirect and auth.api.getSession. signInAs creates a real session in the database and returns it from the stub:
If your command goes through handleCommandError, give the redirect error a digest (NEXT_REDIRECT;replace;<href>;307;) and mock next/cache. The feature tutorial explains why.
2

Seed two organizations in beforeEach

Call resetDatabase(), create users and two organizations with the helpers, then create feature records in both organizations with literal values. Call prisma.$disconnect() in afterAll.
3

Test the query

Sign in as a user of organization A, call the query and compare the result with a literal that contains only A’s records. Repeat for B.
4

Test the command

Sign in as a user of A and call the command with the id of a record in B. Assert that it refused (a literal OperationResult), then read B’s record straight through prisma and compare it with the seeded values.
5

Test a missing membership

Sign in as user A with B as the active organization and assert the query throws NoOrganizationError.
6

Make sure it can fail

Temporarily point the where filter at the other organization and run the test. It must go red. npm run test:mutation:integration does this automatically for every file under src/features/*/services/.
Write expected values as literals (emails, names, counts), never as the output of production code. resetDatabase() truncates the core tables with CASCADE, so a feature table with a foreign key to organizations or users is cleared too. A table without such a key must be cleared in your own beforeEach with prisma.<model>.deleteMany().

Mutation testing

Stryker creates mutants (copies of your code with one deliberate change, such as a flipped condition) and runs the tests covering each one. A mutant no test catches points at behavior without a test. Mutation testing is central to how GatoJS works with AI agents, so it is covered in detail in Built for AI agents. The short version: Never run two Stryker processes at once: they share report files and the test database.

Run times and CI

Indicative local timings on an 8-core machine: Incremental runs are much shorter, because Stryker only retests mutants in changed code. Mutation tests don’t run on pull requests because a full run is too slow. Run them per task locally, and the full suite before a release or by triggering Mutation testing in the Actions tab. More in CI.

Command reference

Test environment variables

All are read from the shell only; neither the integration config nor Playwright loads .env.