[V6] TypeError: Cannot read properties of undefined (reading 'query') #4451
-
I want to write test for my package but I'm getting error when fetching record from db import { test } from '@japa/runner'
import { BaseModel, column } from '@adonisjs/lucid/orm'
test.group('Some title | check type', (group) => {
group.setup(async () => {
await setup() // this works ok - db connection, table create
})
group.teardown(async () => {
await cleanupTables() // this is ok too
})
test('ensure dummy assert works', async ({ assert }) => {
class User extends BaseModel {
@column({ isPrimary: true })
declare id: number
@column()
declare username: string
@column()
declare email: string
@column()
declare password: string
}
const a = await User.find(1) // user with id=1 exsists in db
assert.instanceOf(a, User)
})
})
|
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 1 reply
-
I have not tried writing tests that create models on the fly, so I'm just spitballing here. Models need booted, so maybe try adding a boot call just before your query? test.group('Some title | check type', (group) => {
group.setup(async () => {
await setup() // this works ok - db connection, table create
})
group.teardown(async () => {
await cleanupTables() // this is ok too
})
test('ensure dummy assert works', async ({ assert }) => {
class User extends BaseModel {
@column({ isPrimary: true })
declare id: number
@column()
declare username: string
@column()
declare email: string
@column()
declare password: string
}
User.boot() // <--
const a = await User.find(1) // user with id=1 exsists in db
assert.instanceOf(a, User)
})
})``` |
Beta Was this translation helpful? Give feedback.
-
I would suggest looking at the Auth package tests that uses Lucid models. Especially this line https://github.com/adonisjs/auth/blob/develop/tests/helpers.ts#L102 |
Beta Was this translation helpful? Give feedback.
-
I'm getting similar issue when using
@thetutlage I've followed your suggestion and it works for the models created on the fly(thanks) but I'm getting error when using models from I'm about to finish this package and I'm blocked becouse of this issue.
also tried to call this methods but still getting same error Role.boot()
Role.useAdapter(db.modelAdapter())
// again error
await Role.create({
slug: 'edit',
title: 'Edit',
}) // TypeError: Cannot read properties of undefined (reading 'booted') So basicly I want to have some kind of environment where I can call package methods and then check if DB operations are correct or not, something like this import {Acl} from 'package'
import ModelPermission from 'package/models'
// Give a user the permission to edit
await Acl.model(user).allow('edit');
// then make direct call to db and check
const row = await ModelPermission.where(/*my consition */).first()
assert.isTrue(row.some_column)
Any solution? |
Beta Was this translation helpful? Give feedback.
-
If anyone reaching this was getting this error after running a command, you may need to enable the framework to be loaded with the static options: CommandOptions = {
startApp: true,
}; |
Beta Was this translation helpful? Give feedback.
I would suggest looking at the Auth package tests that uses Lucid models. Especially this line https://github.com/adonisjs/auth/blob/develop/tests/helpers.ts#L102