AdonisJS + Japa: How to create a test database for E2E tests

84 views Asked by At

I'm currently trying to create a test database when running my E2E tests. For my Project I am running a docker Postgres instance (as my "prod" db). So to prevent tests from accessing or changing real data I want to create a new test database when starting my tests and drop it when they're done.

I thought I could just do it like this (in tests/bootstrap.ts):

export const runnerHooks: Pick<Required<Config>, 'setup' | 'teardown'> = {
  setup: [
    async () => {
      await Database.rawQuery(`DROP DATABASE IF EXISTS ${Env.get('DB_NAME')}`).catch(console.log) // <-- JUST FOR TESTING PURPOSES
      await Database.rawQuery(`CREATE DATABASE ${Env.get('DB_NAME')}`).catch(console.log)
    },
    () => TestUtils.ace().loadCommands(),
    () => TestUtils.db().migrate(),
    () => TestUtils.db().seed(),
  ],
  teardown: [async () => await Database.rawQuery(`DROP DATABASE ${Env.get('DB_NAME')}`)],
}

But I get the error database "test_db" does not exist from those commands.

Is there a way I can do this efficiently?

Cheers

0

There are 0 answers