Skip to content

How to change default database connection while testing ? #4493

Closed Answered by Julien-R44
lepresk asked this question in Help
Discussion options

You must be logged in to vote
  • Define a DB_CONNECTION=sqlite variable in your environment variables ( .env + env.ts )
  • Define a DB_CONNECTION=sqliteTest variable in your .env.test file
  • Use this DB_CONNECTION in your config/database.ts file:
const dbConfig = defineConfig({
  connection: env.get('DB_CONNECTION'), // 👈👈👈👈
  connections: {
    sqlite: {
     // ...
    },
    sqliteTest: {
      // ...
     }
  },
})

export default dbConfig

And when you run your tests, the .env.test will be used, so your sqliteTest connection defined in your config will be used

see https://docs.adonisjs.com/guides/testing#environment-variables

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by Julien-R44
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
2 participants