Skip to content

Instantly share code, notes, and snippets.

@musonant
Last active March 20, 2019 00:56
Show Gist options
  • Save musonant/16a658223e016e2379286cba295923a7 to your computer and use it in GitHub Desktop.
Save musonant/16a658223e016e2379286cba295923a7 to your computer and use it in GitHub Desktop.
import chai, { expect } from 'chai';
import { describe, it } from 'mocha';
import chaiHttp from 'chai-http';
chai.use(chaiHttp);
chai.should();
const newArticle = {
title: 'Design patterns',
body: 'There are several design patterns e.g: MVC pattern',
description: 'List of most popular design patterns used in Javascript'
};
describe('Test cases for Authors Haven app', () => {
it('POST /articles/', (done) => {
chai.request(app)
.post('/api/articles')
.send(newArticle)
.end((err, res) => {
res.should.have.status(201);
res.body.should.be.a('object');
res.body.article.should.be.a('object');
res.body.article.id.should.be.a('number');
res.body.article.title.should.equal('Design patterns');
res.body.article.body.should.equal('There are several design patterns e.g: MVC pattern');
res.body.article.body.should.equal('List of most popular design patterns used in Javascript');
done();
});
});
})
@nwamugo
Copy link

nwamugo commented Mar 19, 2019

@musonant nice work!

  • I wonder why you imported describe and it from mocha though. Once mocha package is in the app, describe and it work without the need to explicitly import them

  • Considering our airbnb style, I don't see the extra line needed at the end of your code

  • Usually, I will have an outermost describe function, where I put in the after(), to close the server after this test is completed.

  • Also the name of your file may prove difficult to find in a large application. What do you think about ah.test.js or ah.spec.js? This way, if you write a script, it is easier to target only a particular type of file. But let me know your reasons for your style.

It's a comprehensive test. Well done!

@musonant
Copy link
Author

Thanks for taking out time to review.

  • I agree that describe and it do not need need to be imported. However, it is flagged by ESLint when they used without being imported. Do you have a work-around for this?
  • I'm glad you noticed the missing line. I have added it.
  • Chai-http automatically shuts down the server, since I did not call keepOpen(). You can look at the documentation for chai-http here: https://www.chaijs.com/plugins/chai-http/
  • I totally agree with the naming convention for this type of file.

Thanks again for this amazing feedback!

@nwamugo
Copy link

nwamugo commented Mar 19, 2019

Thanks for taking out time to review.

  • I agree that describe and it do not need need to be imported. However, it is flagged by ESLint when they used without being imported. Do you have a work-around for this?
  • I'm glad you noticed the missing line. I have added it.
  • Chai-http automatically shuts down the server, since I did not call keepOpen(). You can look at the documentation for chai-http here: https://www.chaijs.com/plugins/chai-http/
  • I totally agree with the naming convention for this type of file.

Thanks again for this amazing feedback!

Interesting and novel point to me that you make about the flag issues with ESlint. I have not experienced it before. I will observe going forward, and let you know if I see an issue like that in my code.

The missing line is still not there. Please look at the code again. After your last line of code, airbnb style requires that you give an extra line to the code.

New knowledge I'm getting about chai-http. Thank you. I did not know that.

I see you updated your file name to reflect feedback. Awesome pulse man! Cheers

@musonant
Copy link
Author

For the new line, I had to add an extra line (making two) before it could display. Who knows why?

@nwamugo
Copy link

nwamugo commented Mar 19, 2019

For the new line, I had to add an extra line (making two) before it could display. Who knows why?

I guess it's part of the nuances of this platform. It appears a line with no character keyed in is trimmed off. Therefore even space, tab or enter, can preserve a line, and let it show when you save.

@igbominadeveloper
Copy link

Instead of hardcoding the expected values like you have done, to be on the safe side, you could use a reference to the original data you sent to the server i.e. res.body.article.title.should.equal('Design patterns') could be rewritten as res.body.article.title.should.equal(newArticle.title). This assertion as you have written it would fail if a single space or a character is mistakenly included in the hardcoded strings.
But the code is neatly written and clear

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment