japa / runner

Standalone test runner built on top of japa core

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unable to run migrations in github workflow when automating test runner

estermer opened this issue · comments

Description

I am trying to automate the test runner in my github workflow and it will exit with an error on ace commands. Running the test runner locally works as expected.

Not sure if I should have posted this here or in the adonis repo itself.

Even if there is a decent example of setting up running tests with the db for a github workflow, that would be greatly appreciated.

Package version

^4.0.0

Error Message & Stack Trace

Error: Command failed with exit code 1: node ace migration:run
    at makeError (/home/runner/work/portal-api/portal-api/node_modules/execa/lib/error.js:60:11)
    at handlePromise (/home/runner/work/portal-api/portal-api/node_modules/execa/index.js:118:26)
    at processTicksAndRejections (internal/process/task_queues.js:[95](https://github.com/DirectLink-AI/portal-api/runs/5773496600?check_suite_focus=true#step:7:95):5)
    at runMigrations (/home/runner/work/portal-api/portal-api/japaFile.ts:32:3)
    at run (/home/runner/work/portal-api/portal-api/node_modules/japa/build/src/SlimRunner/index.js:107:9) {
  shortMessage: 'Command failed with exit code 1: node ace migration:run',
  command: 'node ace migration:run',
  escapedCommand: 'node ace "migration:run"',
  exitCode: 1,
  signal: undefined,
  signalDescription: undefined,
  stdout: undefined,
  stderr: undefined,
  failed: true,
  timedOut: false,
  isCanceled: false,
  killed: false
}

Relevant Information

Just to note, I have a .env.testing file for any necessary environment variables for my application

I have in my workflow already set up postgres, node, and installed the dependecies:

name: validate-pull-requests
on:
  pull_request:
    branches:
      - dev
      - staging
      - main
jobs:
  lint-test-build:
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres
        env:
          POSTGRES_USER: root
          POSTGRES_PASSWORD: root
          POSTGRES_DB: test
        ports:
          - 5432:5432
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Setup Node
        uses: actions/setup-node@v2
        with:
          node-version: '14.19.0'

      - name: Install Dependecies
        run: npm install

      - name: Run Linter
        run: npm run lint

      - name: Run Tests
        run: npm run test

      - name: Run the build
        run: npm run build

and here is my japaFile in order to set up the test dbs and run the migrations:

import 'reflect-metadata'
import pgtools from 'pgtools'
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 })

const dbConfig = {
  user: 'root',
  password: 'root',
  port: 5432,
  host: 'localhost',
}

const handleDBCallback = (error, result) => {
  if (error) console.log(error.message)
  else console.log(result)
}

async function createTestDbs() {
  pgtools.createdb(dbConfig, 'test', handleDBCallback)
  pgtools.createdb(dbConfig, 'dl-test-1', handleDBCallback)
  pgtools.createdb(dbConfig, 'dl-test-2', handleDBCallback)
}

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

  await execa('node', ['ace', 'migration:run', '-c', 'dl-test-1'], { stdio: 'inherit' })

  await execa('node', ['ace', 'migration:run', '-c', 'dl-test-2'], { stdio: 'inherit' })
}

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

  await execa('node', ['ace', 'migration:rollback', '-c', 'dl-test-1', '--batch=0'], {
    stdio: 'inherit',
  })

  await execa('node', ['ace', 'migration:rollback', '-c', 'dl-test-2', '--batch=0'], {
    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()
}

function getTestFiles() {
  let userDefined = process.argv.slice(2)[0]
  if (!userDefined) {
    return 'test/**/*.spec.ts'
  }

  return `${userDefined.replace(/\.ts$|\.js$/, '')}.ts`
}

/**
 * Configure test runner
 */
configure({
  files: getTestFiles(),
  before: [createTestDbs, runMigrations, startHttpServer],
  after: [rollbackMigrations],
})

I am really sorry, but I realized I was missing a configuration variable in my .env.testing file when I finally added it back in, I got it working.

Thanks!