DB setup for E2E test

I’m trying to get setup with some basic end to end tests in Playwright to sanity check that the key user flows on my app are working.
I’ve got a simple Playwright setup working, but now want to seed my database with some specific data for the pages that I want to test.

I’ve not looked into whether I can use the existing test scenarios feature for this (although am using it in my jest tests), but would be open to using that if there is a way.

I thought that what I would do is create a script in my package.json that changes my DB string env var to use a different DB, and then run seeding and teardown scripts. I’m falling at the first hurdle though of getting it to use an alternative postgres database.

I can get it to work if I bake the connection string for my test database into the package.json:

"scripts": {
    "dev:e2e": "DATABASE_URL=postgresql://myapp@localhost:5432/mytestdatabase yarn rw dev"
  },

but don’t want that in a file that will get committed.

Any suggestions or existing approaches? The less hacky the better although a bit hacky is fine.
Ideally I would use either another db variable in my .env file or an e2e specific .env file for this.

Thanks

We also use Playwright in our codebase and utilize seed data in scripts/ for E2E testing. We just have something as simple as TEST_DATABASE_URL defined in our .env so that the regular dev db isn’t affected by tests.

Then we create a new db instead of importing from import { db } from 'api/src/lib/db'

export const db = new PrismaClient({
  datasources: { db: { url: process.env.TEST_DATABASE_URL } },
})
1 Like

Thanks, I hadn’t considered creating a different instance of PrismaClient.
I’ve ended up with a quite longwinded script that changes env vars, runs db setup, test, then db clear down.

That works OK, except that some sequence of running e2e tests whilst also running yarn rw dev led to my clear down script running against my dev db. I’ve added some extra checks in for that now.

As it stands I am only using Playwright for a handful of sanity checks that things are up and the main user flows work, plus a few Timezone and DST related tests.

Oh, interesting! Was there any specific reason behind the decision to use the dev server for e2e tests? We also do that for local development, but in our CI we build and serve the backend and frontend with the following snippet in our playwright.config.ts:

  webServer: {
    command: 'yarn rw serve',
    url: 'http://localhost:8910',
    reuseExistingServer: !process.env.CI,
  },

Ha, no it was just an error on my part and was on my local. To clarify, I am only running this all locally at the moment.
Getting the E2E tests setup in a CI pipeline is still in the future, I’ll do that with a separate environment.

1 Like