I want to test rest service which is connecting to mongodb by mongoose.
But still puzzled how to do testing in this framework?
Any example project with testing?
You can check out the feathers-service-tests repo.
And then look at how it is used in any of the adapters.
The generated app has Mocha tests that should be pretty straightforward (the template is here: https://github.com/feathersjs/generator-feathers/blob/master/generators/app/templates/static/test/app.test.js).
You basically require your app and then run tests against services聽you get with app.service('myservice'):
describe('myservice', function() {
it('runs create', function(done) {
app.service('myservice').create({ name: 'David' }).then(person => {
assert.ok(person._id);
assert.equal(person.name, 'David');
done();
});
}).catch(done);
});
Everything else is the same as testing an Express app.
I'm curious what you guys do to test services that require authentication.
I tend to rely on using feathers-client to do some client mock ups.
What I'm doing right now is create a test user first in mocha before hook. Then do a clean up in a after hook. But it gets tedious when you implement a soft delete on a service.
Authentication hooks will normally be skipped if params.provider isn't set (e.g. if a service tested like my example above).
@daffl Thanks for the examples.
In the end, I started server in mocha before hook and created the user in beforeEach hook, and then test each service against supertest.
Let me know if this is not a good approach to do test in feathers.
https://github.com/youpin-city/youpin-api/blob/master/test/services/user/index.test.js
describe('user service', () => {
let server;
before((done) => {
server = app.listen(9100);
server.once('listening', () => done());
});
beforeEach((done) => {
UserModel.remove({}, done);
});
// Clears collection after finishing all tests.
after((done) => {
server.close((err) => {
if (err) { throw err; }
UserModel.remove({}, done);
});
});
// Registers user service.
it('registers the users service', () => {
expect(app.service('users')).to.be.ok();
});
describe('GET /users', () => {
beforeEach((done) => {
// Create admin user
fixtures.load({ User: [adminUser] }, mongoose, done);
});
it('return user array conatining only admin user', () =>
request(app)
.post('/auth/local')
.send({
email: 'email',
password: 'password',
})
.then((tokenResp) => {
const token = tokenResp.body.token;
if (!token) {
throw new Error('No token returns');
}
return request(app)
.get('/users')
.set('Authorization', `Bearer ${token}`)
.expect(200)
.then((userResp) => {
const body = userResp.body;
expect(body).to.have.all.keys(['total', 'limit', 'skip', 'data']);
expect(body.total).to.equal(1);
const userDataList = userResp.body.data;
expect(userDataList).to.be.a('array');
expect(userDataList).to.have.lengthOf(1);
expect(userDataList[0]).to.contain.all.keys(
['_id', 'name', 'phone', 'email', 'role', 'owner_app_id',
'customer_app_id', 'updated_time', 'created_time']);
expect(userDataList[0].email).to.equal('[email protected]');
// also check response does not contain password
expect(userDataList).to.not.have.keys('password');
});
})
);
});
});
@parnurzeal That looks fine to me, thanks for sharing. We will add a chapter about testing eventually but I'm going to close this issue since it looks like it has been all figured out.
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue with a link to this issue for related bugs.
Most helpful comment
@daffl Thanks for the examples.
In the end, I started server in mocha before hook and created the user in beforeEach hook, and then test each service against supertest.
Let me know if this is not a good approach to do test in feathers.
https://github.com/youpin-city/youpin-api/blob/master/test/services/user/index.test.js