Skip to content

Commit

Permalink
feat(wip): update database provider to register testUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-R44 committed Jan 25, 2024
1 parent cb0dda0 commit 942baa0
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions providers/database_provider.ts
Expand Up @@ -14,6 +14,7 @@ import { Database } from '../src/database/main.js'
import { Adapter } from '../src/orm/adapter/index.js'
import { QueryClient } from '../src/query_client/index.js'
import { BaseModel } from '../src/orm/base_model/index.js'
import { DatabaseTestUtils } from '../src/test_utils/database.js'
import type { DatabaseConfig, DbQueryEventNode } from '../src/types/database.js'

/**
Expand All @@ -28,6 +29,12 @@ declare module '@adonisjs/core/types' {
}
}

declare module '@adonisjs/core' {
export interface TestUtils {
db(connectionName?: string): DatabaseTestUtils
}
}

/**
* Extending VineJS schema types
*/
Expand Down Expand Up @@ -80,6 +87,20 @@ export default class DatabaseServiceProvider {
}
}

/**
* Register TestUtils database macro
*/
protected async registerTestUtils(db: Database) {
if (this.app.getEnvironment() === 'test') {
const { TestUtils } = await import('@adonisjs/core')

Check failure on line 95 in providers/database_provider.ts

View workflow job for this annotation

GitHub Actions / typecheck

Property 'TestUtils' does not exist on type 'typeof import("/home/runner/work/lucid/lucid/node_modules/@adonisjs/core/build/index", { with: { "resolution-mode": "import" } })'.

Check failure on line 95 in providers/database_provider.ts

View workflow job for this annotation

GitHub Actions / typecheck

Property 'TestUtils' does not exist on type 'typeof import("/home/runner/work/lucid/lucid/node_modules/@adonisjs/core/build/index", { with: { "resolution-mode": "import" } })'.
const ace = await this.app.container.make('ace')

TestUtils.macro('db', function (this: typeof ace, connectionName?: string) {
return new DatabaseTestUtils(ace, db, connectionName)
})
}
}

/**
* Invoked by AdonisJS to register container bindings
*/
Expand Down Expand Up @@ -107,6 +128,7 @@ export default class DatabaseServiceProvider {
const db = await this.app.container.make('lucid.db')
BaseModel.$adapter = new Adapter(db)

await this.registerTestUtils(db)
await this.registerReplBindings()
await this.registerVineJSRules(db)
}
Expand Down

0 comments on commit 942baa0

Please sign in to comment.