Skip to main content
Keep the values your tests type and visit out of the test code. There are three common sources:
  • Environment variables for settings that change between environments, such as the app URL.
  • Fixtures for a known set of test data you reuse across tests.
  • Generated data for values that must be unique on every run.
To keep passwords and tokens out of logs as well, see Use secrets.

Read environment variables

Read settings from process.env so the same test runs against dev, staging, or production.
tests/home.test.mjs
Set the variable when you run the tests:
TestDriver loads a .env file in your project root automatically, so you can also keep local values there:
.env

Reuse test data with fixtures

Put shared test data in its own module and import it wherever you need it.
tests/fixtures/usernames.js
Use Vitest’s it.for() to run the same test once per fixture. Unlike it.each(), it.for() passes the test context as the second argument, which TestDriver(context) needs.
tests/login.test.mjs
Each fixture becomes its own test with its own sandbox.

Generate unique data

When a test creates records, such as new accounts, generate fresh values on every run so they don’t collide with earlier runs. Faker is a common choice:
tests/signup.test.mjs

Keep values out of descriptions

Pass changing values to type(), not to find(). The cache matches find() descriptions exactly. A description that contains a generated value misses the cache on every run:
Describe the element itself, and type the value after you click it:

Next steps

Use secrets

Keep passwords and tokens out of code and logs.

Reuse code

Share helpers like a login step across tests.

Run in CI/CD

Pass environment variables to tests in your pipeline.

Caching

Write descriptions that reuse cached locations.