possible to access models in tests?
jamesdixon opened this issue · comments
Hello!
Is it possible to access objection models in tests using Schwifty?
I'm trying the following:
it('can retrieve an auth token with valid credentials', async () => {
const Member = server.models().Member;
const newMember = await Member.query().insert({});
request.payload = {
data: {
type: 'token',
attributes: {
email: 'heidifarfenugel@gmail.com',
password: 'Q1w2e3r4'
}
}
};
const res = await server.inject(request);
expect(res.statusCode).to.equal(401);
});
When I log Member
, it's undefined
. If I log server.models()
, it returns an empty object.
THanks!
Yes! Schwifty is designed for multi-plugin deployments, where each plugin owns its own models. The server
there is your root server, which also owns its own models. By default each plugin doesn't see other plugins' models; so when you call server.models()
you only get the models owned by the current plugin/realm. If you want to ignore this "ownership" just use server.models(true)
instead :) Let me know if that gives you any more trouble.
Ah! Thanks @devinivy 👍
That said, is there a way to specify which plugin to load models for or do you just recommend using server.models(true)
. As you know, I'm using your boilerplate, so at the moment all of my models are in the lib
plugin.
server.models(true)
has been sufficient for my purposes, at least! If you really want to access a single plugin's models in the tests I would suggest using server.expose()
in your plugin to expose its models()
function.