Add initial project files
This commit is contained in:
Executable
+76
@@ -0,0 +1,76 @@
|
||||
// apps/backend/tests/bootstrap.ts
|
||||
import { assert } from '@japa/assert'
|
||||
import { apiClient } from '@japa/api-client'
|
||||
import { pluginAdonisJS } from '@japa/plugin-adonisjs'
|
||||
import type { Config } from '@japa/runner/types'
|
||||
|
||||
import app from '@adonisjs/core/services/app'
|
||||
import testUtils from '@adonisjs/core/services/test_utils'
|
||||
|
||||
/**
|
||||
* Japa plugins
|
||||
*/
|
||||
export const plugins: Config['plugins'] = [assert(), apiClient(), pluginAdonisJS(app)]
|
||||
|
||||
/**
|
||||
* Reporters
|
||||
*/
|
||||
export const reporters: Config['reporters'] = {
|
||||
activated: ['spec'],
|
||||
}
|
||||
|
||||
/**
|
||||
* Global setup/teardown (for ALL suites)
|
||||
*
|
||||
* Runs migrations against the test database (`.env.test` → adonis_test) before
|
||||
* the suites and rolls them back afterwards. DB-backed specs additionally wrap
|
||||
* each test in a global transaction via `testUtils.db().wrapInGlobalTransaction()`
|
||||
* in their own group hooks.
|
||||
*/
|
||||
export const runnerHooks: Required<Pick<Config, 'setup' | 'teardown'>> = {
|
||||
setup: [() => ensureTestDatabase(), () => testUtils.db().migrate()],
|
||||
teardown: [],
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the test database on first run so a fresh checkout only needs the
|
||||
* postgres container up. Guarded to `*_test` names so a misconfigured
|
||||
* `.env.test` can never provision anything else.
|
||||
*/
|
||||
async function ensureTestDatabase() {
|
||||
const { default: env } = await import('#start/env')
|
||||
const database = env.get('DB_DATABASE')
|
||||
if (!database.endsWith('_test')) {
|
||||
throw new Error(
|
||||
`Refusing to run tests against "${database}": DB_DATABASE in .env.test must end with "_test"`
|
||||
)
|
||||
}
|
||||
|
||||
const { Client } = await import('pg')
|
||||
const client = new Client({
|
||||
host: env.get('DB_HOST'),
|
||||
port: env.get('DB_PORT'),
|
||||
user: env.get('DB_USER'),
|
||||
password: env.get('DB_PASSWORD'),
|
||||
database: 'postgres',
|
||||
})
|
||||
await client.connect()
|
||||
try {
|
||||
const exists = await client.query('SELECT 1 FROM pg_database WHERE datname = $1', [database])
|
||||
if (exists.rowCount === 0) {
|
||||
await client.query(`CREATE DATABASE "${database}"`)
|
||||
}
|
||||
} finally {
|
||||
await client.end()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Per-suite configuration (start HTTP server for HTTP tests)
|
||||
*/
|
||||
export const configureSuite: Config['configureSuite'] = (suite) => {
|
||||
// adjust names to your suites if needed
|
||||
if (['functional', 'e2e', 'browser'].includes(suite.name)) {
|
||||
suite.setup(() => testUtils.httpServer().start())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user