Testing a REST API in Node JS with Express using Mocha and Chai
ฝัง
- เผยแพร่เมื่อ 5 พ.ย. 2019
- In this video we are going to test our REST API in Node JS with Express using Mocha and Chai.
➥ Buy Me a Coffee: www.buymeacoffee.com/pragmatic
Mocha is a testing framework for JavaScript that runs on Node.js and the browser. You can use any assertion library, in this case we are going to use Chai to validate the http requests we make against our ExpressJS Restful API.
Assertion with Chai provides natural language assertions, expressive and readable style. Chai provides three assertion styles: Should, Expect and Assert.
Stay tuned for more!
➥ If I have helped you in any way please consider becoming a patron for just $1/month: / pragmatic
➥➥ Learn even more! ➥➥
NestJS Crash Course ➥ bit.ly/2QevL9X
TypeScript Crash Course ➥ bit.ly/37lJEsH
Nuxt.js Crash Course ➥ bit.ly/37kQiPG
Vuetify.js Crash Course ➥ bit.ly/2sArggU
Vue.js Crash Course ➥ bit.ly/37mCaFM
Automate.io Crash Course ➥ bit.ly/2SRGlph
Zapier Crash Course ➥ bit.ly/37q1BGt
SEO Crash Course ➥ bit.ly/2QbIhqy
Wordpress Crash Course ➥ bit.ly/2ZBZRr5
#Express #Mocha #Chai
➥ Here is the source code for this video: gitlab.com/pragmaticreviews/node-mocha-chai
➥ Get a Certification as a Python Data Scientist or Data Engineer: pragmaticreviews.com/datacamp
➥ Get a Free Golang Cheat Sheet here: pragmaticreviews.com
Thanks!
I got a job following your lessons in doing the assessment task that was given to me. Thank you so much
Exactly what I needed. The crux of mocha and chai. Thank you so much sir.
thank you! nice tutorial! easy to understand, keep up the great work!
This is very clear and to the point mocha tutorial i ever see, Thanks a lot @pragrmatic review
Thanks! nice and easy to follow
Great tutorial mate !
Thanks
Great tutorial, Very Easy to understand, thanks man :)
Thanks for the video, very well explained.
Glad it was helpful!
Thank you so much for the help!
Cool!
Does anyone know how can I like a video 5 times?
It REALLY IS THAT GOOD!
The way you explain, oh my god, you're a legend bro.
Thank you so much for this. Srsly, I wish I could Like it more than once.
Needless to say, I just subscribed.
Awesome man!
Me vino genial. Muy buen canal, muchas gracias!
Genial! Gracias!
Thanks!! Super useful :)
thank you ..that's really helpful
Well Done !!!
Thank you, i t was very helpful
Hi Sir, Thanks for the video, I was looking for the tutorial of node.js based API unit testing using mocha & chai, however I got but not that much easy way you have done it. Thanks to save my life
Awesome. Thank you!
By the way, here is the source code for this video: gitlab.com/pragmaticreviews/node-mocha-chai
Awesome tutorial
Glad you think so!
Hi Can you please tell how to generate code coverage report?
Hi. when fetching the data from real mongodb database, we use Promises, async, await, so it function is not accepting async await or its rejecting done cb called inside then block. what would be the solution for this?
thanks for sharing
Thank you!
THANKS BRO
good job! but how can i stop running mocha test ???
I have an honest question. If it is so easy to test API calls using JS, why do we need tools like Postman? What are the advantages of such a a tool?
Thanks for this excellent video.
Hi.., can u explain that how to use assert using API
if we have used MongoDB as a Database, then will these steps will work, or for the Database, we have to add more conditions to get work.
Everything worked fine, thank you. A question, how would you run tests for PUT or DELETE methods for random generated IDs?
Before running the test you need to create one element, then you get the first element (you need a "findAll" endpoint) and finally you run the test.
If you want to delete the Id then you need to use the get api to fetch that id and use delete API and for each the ids and and store it in variable and delete
WHERE I CAN GET THE RESTFULL API
CODE THAT YOU ARE USING?
You said in introducing that will use a beforeEach and another method but on video, I don't get it. Thanks
We can use before o beforeEach to setup the test (e.g. initializing the database) and after or afterEach (e.g. to close the database connection). Thanks for watching!
This will create some test data in the database right. We should do a mock instead of calling actual API
Hi, this is great thing by you. I have a question that, how we can do authentication of APIs?, using jwt for the running api collection.
Hey, I've just found this great tutorial buddy.works/tutorials/unit-testing-jwt-secured-node-and-express-restful-api-with-chai-and-mocha Thanks!
Thanks a lot
Hi, stuck once again that how to use this for token authentication??
can't access your site
Hi Sir, Thanks for the video. I wanna ask something, in your tutorial when the id is not in the response body and must be retrieved from the database. how do we do it?. Thank you
You have to map the id field with Mongoose for example so you can retrieve it. Thanks!
@@PragmaticReviews I am already get the id, so how I do to put the id to const of mocha?
You can do something like: response.body.should.have.property('id').eq(1);
@@PragmaticReviews No No, I mean that how I do change the value when i get the is from database to const id = null
@@januarypanjaitan3307 That sounds like a problem with either the code creating the objects and assigning their ID, the code that's presenting that as JSON, or the code on the front-end that's interpreting that JSON
➥➥ Learn even more! ➥➥
Golang Crash Course ➥ bit.ly/39ZH8dy
Vue.js Crash Course ➥ bit.ly/37mCaFM
Vuetify.js Crash Course ➥ bit.ly/2sArggU
Nuxt.js Crash Course ➥ bit.ly/37kQiPG
TypeScript Crash Course ➥ bit.ly/37lJEsH
NestJS Crash Course ➥ bit.ly/2QevL9X
Automate.io Crash Course ➥ bit.ly/2SRGlph
Zapier Crash Course ➥ bit.ly/37q1BGt
SEO Crash Course ➥ bit.ly/2QbIhqy
Wordpress Crash Course ➥ bit.ly/2ZBZRr5
Thank You for the video. All websites currently use token and APIs cannot be used without that. How to request Api with bearer token?
Hi , you can set token using this way by assign it into a variable,
it('it should fetch All Historical Questions', (done) => {
chai.request(server)
.get('/incard/v1/user/questions')
.set('token', token)
.end((_err, res) => {
res.should.have.status(200);
res.body.should.be.a('array');
done();
});
});
@@SAHILBHARTIIPU Thank You very much for the response :) One more question: How to use the token taken from the endpoint response in other requests?
Example: there is an Authentication endpoint which response with token. That token always needs to be sent in the Headers for the other endpoints. How to keep the token given from Authentication endpoint in the variable before running other tests and make it globally available in other suites/test cases in order to use that variabe for the rest requests?
TypeError: Joi.validate is not a function
its showing this in all of my methods except get , how i can resolve it
Hi, POST, PATCH, GET is ok but for the DELETE test, we cannot know exactly the id that we have to delete. I want to get the id from the object that I posted before with the POST test. How could I assign it to the variable outside the test and call it? Because if I dockerize the program, I will not know exactly the ID of the object that I want to delete. Hope to know your answer
Hi, if you create a new record by doing a post call then there will be a Id for that record will get created, so you call a get call where you look for an id and store it in variable var newId = response.body.newid;
Then pass this newid in the delete call ex: api.delete(“endpontUrl/‘+newid+’”)
Hello sir iam also running the integration test on crud api which is also built with express js framework
So my question is whenever I ran the test for any task it is throwing me the timeout error
Can u please help me😭😭😭
great,please send me the link to this rest API development totorial
Here you go gitlab.com/pragmaticreviews/node-mocha-chai
hello sir , do you have the full course on testing rest api with mocha and chai.
Here is the source code: gitlab.com/pragmaticreviews/node-mocha-chai Thanks!
@@PragmaticReviews do you have further videos or full course on this ?
@@mdfaisalhossain9317 Hi sorry, I thought you were asking about the code, my mistake. I don't have any other course on Mocha and Chai, this is the only video. I do have a video on Vue.js testing using Vue Test Utils and Jest here: th-cam.com/video/TZj60NV70QA/w-d-xo.html and another one for testing automation with Cypress here: th-cam.com/video/_f61LfPHyAo/w-d-xo.html
good
why should we use mocha and chai if my postman show correct output?
If you want to automate your regression tests by doing continuous integration, this is a good option. Also, you can run your tests in the background and get a report afterwards. Postman is great but you have to be there.
Hey, I do not want to watch building API in node js which I already know, can you provide me with the link from where I can clone it to use as a base for this tutorial.
Node API here, thank you for watching man: gitlab.com/pragmaticreviews/node-tasks-api
@@PragmaticReviews Thank you so much for responding quickly. You are one of the active tech youtube.
Very nice Tutorial! Thanks for that. Keep in mind hearing your typing all the Time is a nightmare to follow your Tutorial.
Thank you!
Good Video.. but you never test exactly length of the response from the server, otherwise if there will be a small change (like if some post a new user) the test will failed.. in fact if you do that it's force you all the time to go to the DB and count how much users the server will get.. So don't test to specific or to general.
Thank you for your feedback!
Is your name is Alex??
How we can mock the DB with REST API call, actually I wanted to cover my whole code in code coverage report and don't want to save anything in DB how can we achieve this?
API testing is integration testing because you test the endpoints of an api. Your tests are outside the API. You are talking about unit tests where you mock the database but then you shouldn't call the API endpoints. You should call methods inside the API project and you can do that just inside the project itself because those methods are not publicly accessible by outside tests.
Why trying to break that keyboard? :D
I need a new keyboard now! :-)
@@PragmaticReviews Ahahahah :) Nice tutorial though. Thank you for your time and effort!