I wanted to test the models of my Trails.js project with mocha. I use the trailpack-waterline to load my models into the Waterline ORM.
Following the Trails Docs I created a User.test.js:
'use strict'
const assert = require('assert')
describe('User Model', () => {
let User
before(() => {
assert(global.app.models.User)
User = global.app.models.User
})
it('should exist', () => {
assert(User)
})
})
This runs without any error.
But I cannot instantiate the model in any way. Following the example of the Docs new User({...}) should create a new user object, but this code throws an error saying User is not a constructor. And neither the example of the Waterline Docs using User.create({...}) seems to work.
Printing out the User model shows it consists of only two methods: [ 'getModelName', 'getTableName' ].
How do I instantiate a waterline Model for unit testing?
It's because global.app.models.User is your model's definition and not the waterline model. This one is under global.app.orm.User, after that you can use User.create without any problem
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With