Create an Isolated Test Database

Automated tests should not modify your development or production database.

Create a separate test database.

For example:

MONGODB_TEST_URI=mongodb://127.0.0.1:27017/rest_api_test

A separate database allows tests to create and delete test records without affecting normal application data.

For larger test suites, consider an isolated MongoDB instance or a dedicated test database per test worker.

Database Isolation

Never execute test cleanup commands against a production database. Verify the active database name before dropping collections or deleting test data.

.env.test
NODE_ENV=test
MONGODB_TEST_URI=mongodb://127.0.0.1:27017/rest_api_test
JWT_SECRET=test-only-secret

Defines environment variables for local automated testing. Never reuse production credentials or databases in tests.

Key Takeaways
  • Use a separate test database.

  • Do not run destructive tests against production.

  • Keep test configuration isolated.