Change production env to test env in adonis 5

750 views Asked by At

I am implementing the tests in adonis 5 and would like to change the production .env to the test .env.test. In the production env I use the postgres database and in the test env I use the sqlite database. During the execution of the tests I add and remove fictional data, squeegee migrations and rollbackMigrations.

To run the tests I start the server with node ace serves --watch and then and then run the command node build / japaFile.js

So I would like to know where I get the test env?

My japaFile file is the same as the adonis 5 documentation, below is the japaFile.ts file

import 'reflect-metadata'
import execa from 'execa'
import { join } from 'path'
import getPort from 'get-port'
import { configure } from 'japa'
import sourceMapSupport from 'source-map-support'

process.env.NODE_ENV = 'testing'
process.env.ADONIS_ACE_CWD = join(__dirname, '..')
sourceMapSupport.install({ handleUncaughtExceptions: false })

async function runMigrations() {
  await execa.node('ace', ['migration:run'], {
    stdio: 'inherit',
  })
}

async function rollbackMigrations() {
  await execa.node('ace', ['migration:rollback'], {
    stdio: 'inherit',
  })
}

async function startHttpServer() {
  const { Ignitor } = await import('@adonisjs/core/build/src/Ignitor')
  process.env.PORT = String(await getPort())
  await new Ignitor(__dirname).httpServer().start()
}

/**
 * Configure test runner
 */
configure({
  files: [
    'build/test/**/*.spec.js',
  ],
  before: [
    runMigrations,
    startHttpServer,
  ],
  after: [
    rollbackMigrations,
  ]
})```
1

There are 1 answers

0
Amir Hosein Salimi On

You can simply create a .env.testing file and put your desired values in there. Japa will automatically load those values while testing.