# Add a Test environment (/en/docs/deployment/after-launch/test-environment)

A Test environment is a second copy of your app with test billing. Vercel can follow a `test` branch; Cloudflare requires a manual Test Worker deploy.

**Give your agent this prompt:**

```text interactive=true
Set up a separate Test environment for <framework> on <host> with test billing after approval.
Follow https://coremvp.com/en/docs/deployment/after-launch/test-environment.md and its prerequisites.
Ask me before creating or linking provider resources, writing secrets, replacing existing settings, pushing a deploy-triggering branch, deploying or changing Production. Finish when Supabase passes for the pushed commit, the matching Vercel deployment passes or the Worker deploy succeeds for that commit, and the changed behavior and relevant test checkout work on the Test URL.
Report what changed, the check results and anything you could not run.
```

**You'll need**

A second host project (Vercel) or Worker (Cloudflare), and a second Supabase
project. Stripe test mode or Polar sandbox.

### 1. Build the Test copy

Test always uses test mode or sandbox. For Stripe, it keeps
`EMAIL_DELIVERY_MODE=test_capture` with its own `TEST_EMAIL_CAPTURE_TOKEN`.

**Vercel:**

Repeat [Deploy to Vercel](/en/docs/deployment/vercel) with a new project
name, such as `<app>-test`. A second Vercel project keeps its own settings.
Then follow [Add your database](/en/docs/deployment/supabase) with the
second Supabase project, and add [email](/en/docs/deployment/email) and
test [billing](/en/docs/deployment/billing) to the Test project.

**Cloudflare:**

Test builds from the same config as Production under another Worker name,
with its own settings file, `.env.cloudflare.test`. Follow
[Deploy to Cloudflare](/en/docs/deployment/cloudflare) with three changes:

1. Skip **Name the Worker**. Your config keeps the Production name.

2. In **Save your app URL**, create `.env.cloudflare.test` with these two
   lines instead:

   Replace `<app>` with your Worker name and `<your-subdomain>` with your
   Workers subdomain.

   ```bash interactive=true
   (set -C; echo 'CLOUDFLARE_WORKER_NAME=<app>-test' > .env.cloudflare.test) &&
   echo 'NEXT_PUBLIC_APP_URL=https://<app>-test.<your-subdomain>.workers.dev' >> .env.cloudflare.test
   ```

   If the file already exists, inspect it and ask before replacing its settings.

3. In every build and deploy command, use `.env.cloudflare.test` in place
   of `.env.cloudflare`.

Then follow [Add your database](/en/docs/deployment/supabase) with the
second Supabase project, and add [email](/en/docs/deployment/email) and
test [billing](/en/docs/deployment/billing). Add each setting to
`.env.cloudflare.test` and never recreate the file: its first line sends
every build to the Test Worker, so a deploy never replaces Production.

### 2. Create the `test` branch

```bash
git push origin main:test
```

### 3. Connect Test to the `test` branch

In the Test Supabase project, connect the GitHub integration as in
[Ship on merge](/en/docs/deployment/ship-on-merge#connect-supabase), with
**Production branch name** set to `test`.

**Vercel:**

Connect the Test project to your repository as in
[Ship on merge](/en/docs/deployment/ship-on-merge#connect-your-host). Then,
in the Test project, open **Settings → Environments → Production** and set
**Branch Tracking** to `test`.

**Cloudflare:**

Keep deploying the Test Worker from your computer, with the build and
deploy commands and `.env.cloudflare.test` from the step above. Workers
Builds checks that the Worker name matches your config, which keeps the
Production name.

### 4. Check a change on Test first

From your change's branch:

Replace `<change-name>` with the branch to test.

```bash interactive=true
git switch test && git pull &&
git merge <change-name> &&
git push
```

On Cloudflare, build and deploy Test from this exact pushed `test` commit before switching branches. Then run `git switch <change-name>`. When Test
passes the checks below, open the pull request to `main` as in
[Ship a change](/en/docs/deployment/after-launch/ship-a-change).

## Check it works

The commit you pushed to `test` shows passing checks from Supabase and, on
Vercel, from the matching Test deployment; on Cloudflare, the manual Worker deploy succeeds. Your Test URL shows the change and the relevant test checkout grants access. Production is
unchanged until you merge to `main`.

## Watch the billing checks

Run the Stripe billing checks against Test and watch them in a browser.
Playwright, a browser automation tool, pays with Stripe's test card the way a
customer would, so you can rerun the same checks after every change. Your
local `.env` needs the Test project's `NEXT_PUBLIC_SUPABASE_URL`,
`SUPABASE_SECRET_KEY` and `TEST_EMAIL_CAPTURE_TOKEN`, plus the Test app's
`NEXT_PUBLIC_STRIPE_` price IDs, as described in
[Hosted checks](/en/docs/deployment/deploy-app#prepare-the-local-runner). The
checks compare each purchase with those price IDs and read them from your
shell, so load `.env` first. Before the subscription check,
[activate the Stripe customer portal](https://dashboard.stripe.com/test/settings/billing/portal)
in test mode.

```bash interactive=true
set -a; source .env; set +a
export TEST_BASE_URL=https://<app>-test.<host-domain>
unset PROD_E2E_AUTH_MODE
./coremvp prod e2e billing:lifetime --headed

export PROD_E2E_AUTH_MODE=signup
./coremvp prod e2e billing:subscription --headed
```

A browser buys with Stripe's test card, sets up the account and opens the paid
content. Each command ends with `result production E2E passed` and the Test
URL. Pointed at a live app, both commands stop before any checkout.

_Video: The lifetime billing check in a browser, sped up: it buys with Stripe's test card, sets a password, opens the dashboard and then the paid content (/docs/deployment/test-billing.mp4)._

One passing `./coremvp prod e2e billing:lifetime --headed` run against a
Next.js Test environment on Vercel, shown at 2.5× speed. The test account's
email and name are hidden.
