Monday 12 March 2018

Nodejs test structure noob question

Hello!I am new to the TDD world. I need to test API endpoints responses and I have some doubts on how to properly structure my tests. In particular, I need to test the HTTP Response code, the Content-Type header and check that the body contains some stuff.I have doubts about where I should do everything in one single it by chaining these tests, or it is better to provide different its for each of these.For instance, I could do this (done here):describe('GET /v1/accounts', () => { it('should return all the accounts', () => { return api.get('/v1/accounts') .expect(200) .expect('Content-Type', 'application/vnd.api+json') .expect(res => { expect(res.body.data).to.be.an('array'); expect(res.body.data[0]).to.be.an('object'); expect(res.body.data[0].type).to.be.equal('account'); expect(res.body.data[0].attributes).to.exist; }); }); }); Or I could do this (which is what I was going for before reading the option above):describe('GET /v1/accounts', () => { it('should return 200 OK', (done) => { api.get('/v1/accounts') .expect(200, done) }); it('should return Content-Type application/vnd.api+json', (done) => { api.get('/v1/accounts') .expect('Content-Type', 'application/vnd.api+json', done) }) // ... and so on }); Both approach work, so which one would you pick and why? Are there alternatives to these approaches?Thanks!

Submitted March 12, 2018 at 04:54PM by honestserpent

No comments:

Post a Comment