On the Google groups post on deprecating loopback-testing there is a question that asks about providing a proper example of how testing can be achieved without loopback-testing. That thread talks about using supertest instead.
Below is an attempt that I made to combine Mocha, supertest along with models (from app.js). The result works really well when I run the file by itself. But if I had another test file (say test-teacher.js) then the first test file (call it test-student.js) starts to fail in weird ways I can't describe.
Am I missing something or can models not be used like I am using them below?
describe('/Student', function () {
var server = require('../server/server')
var loopback = require('loopback')
var supertest = require('supertest')
var request = require('supertest')(server)
var dataSource = server.dataSource('db', {adapter: 'memory'})
var Student = dataSource.define('Student', {
'id': Number,
'points': Number
});
beforeEach(function () {
Student.updateOrCreate({id: 1, points: 5000});
})
it('Post a new student', function (done) {
request.post('/api/Students').send({points: 5000}).expect(200, done)
})
})
dataSource
variable from? In any case, multiple files in the same test run all share memory, which means you also share in-memory model definitions and data. My guess is that you start up your LB app somewhere in there, yes? You need to make sure to close that down, but the data may still persist. That would be my guess. – ConstantinopledataSource
declaration. WRT to starting up LB app, I don't do it explicitly. The above code can be run withmocha test/test-student.js
. And then I can run all the tests withmocha test
. By defining request with the server, it calls LB. – RedfordStudent
. When I comment outStudent
in test-teacher.js the test above works. Then obviously the test in test-teacher.js fails because it doesn't now aboutStudent
. – Redford