Testing

Running Tests

How to run tests in Nuda Kit.

This guide covers how to run tests in Nuda Kit using the Japa test runner.

Prerequisites

Before running tests, ensure:

  1. Docker is running — Tests use PostgreSQL
  2. Database exists — Tests will migrate automatically
  3. Dependencies installed — Run npm install in backend

Running All Tests

cd backend

# Run all tests
node ace test

# Or using npm script
npm test

Test Suites

Nuda Kit has two test suites configured:

SuiteDirectoryTimeoutPurpose
unittests/unit/2 secondsIsolated unit tests
functionaltests/functional/30 secondsAPI/integration tests

Run Specific Suite

# Run only functional tests
node ace test --suite=functional

# Run only unit tests
node ace test --suite=unit

Filtering Tests

Run Single File

# Run specific test file
node ace test tests/functional/users/create_user.spec.ts

Run by Filename Pattern

# Run all user-related tests
node ace test --files="**/users/*.spec.ts"

# Run all team-related tests
node ace test --files="**/teams/*.spec.ts"

# Run all billing tests
node ace test --files="**/billing/*.spec.ts"

Run by Test Title

# Run tests matching a pattern
node ace test --match="creates a new user"

# Run tests containing "validation"
node ace test --match="validation"

Watch Mode

Re-run tests automatically when files change:

node ace test --watch

This is useful during development to get instant feedback.


Test Output

Default Output

node ace test

Shows a summary with pass/fail counts.

Verbose Output

# Show all test names
node ace test --reporter=spec

Minimal Output

# Dot reporter (one dot per test)
node ace test --reporter=dot

Failing Fast

Stop on first failure:

node ace test --bail

Useful for CI/CD pipelines or when debugging.


Running Tests in CI

GitHub Actions Example

name: Tests

on: [push, pull_request]

jobs:
  test:
    runs-on: ubuntu-latest

    services:
      postgres:
        image: postgres:15
        env:
          POSTGRES_USER: root
          POSTGRES_PASSWORD: root
          POSTGRES_DB: app_test
        ports:
          - 5432:5432
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

      redis:
        image: redis:7
        ports:
          - 6379:6379

    steps:
      - uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: Install dependencies
        run: npm ci
        working-directory: backend

      - name: Run tests
        run: npm test
        working-directory: backend
        env:
          DB_HOST: localhost
          DB_PORT: 5432
          DB_USER: root
          DB_PASSWORD: root
          DB_DATABASE: app_test
          QUEUE_REDIS_HOST: localhost
          QUEUE_REDIS_PORT: 6379

Test Configuration

Tests are configured in adonisrc.ts:

tests: {
  suites: [
    {
      files: ['tests/unit/**/*.spec(.ts|.js)'],
      name: 'unit',
      timeout: 2000,  // 2 seconds
    },
    {
      files: ['tests/functional/**/*.spec(.ts|.js)'],
      name: 'functional',
      timeout: 30000, // 30 seconds
    },
  ],
  forceExit: false,
}

Adjusting Timeouts

For slow tests, increase the timeout:

// In test file
test('slow operation', async () => {
  // ...
}).timeout(60000) // 60 seconds

Common Commands

CommandDescription
node ace testRun all tests
node ace test --suite=functionalRun functional tests only
node ace test --files="**/users/*"Run tests matching pattern
node ace test --match="login"Run tests with "login" in title
node ace test --watchWatch mode
node ace test --bailStop on first failure
node ace test --reporter=specVerbose output
node ace test --helpShow all options

Troubleshooting

Database Connection Errors

Ensure Docker is running:

docker compose up -d

Tests Hanging

Check for unclosed connections or pending promises. Use --forceExit as a workaround:

node ace test --forceExit

Slow Tests

  • Use --bail to stop on first failure
  • Run specific test files instead of all tests
  • Check for unnecessary database operations

Port Already in Use

If port 5432 is in use, stop other PostgreSQL instances:

# Check what's using the port
lsof -i :5432

# Or use different port in .env.test
DB_PORT=5433

Resources