DEV Community

Cover image for Testing NodeJs/Express API with Jest and Supertest

Testing NodeJs/Express API with Jest and Supertest

Chinedu Orie on August 26, 2019

This is part three of building API using Express, Sequelize, and Postgres. In part two, we built simple API endpoints to demonstrate CRUD operation...
Collapse
 
avinash8686 profile image
avinash8686

There are no cases for api failure, It would be great if u add the failure cases as well, overall the post was pretty good and easy to understand.

Collapse
 
hotdangca profile image
James Robert Perih

You could:

expect(res.statusCode).toEqual(500);
Enter fullscreen mode Exit fullscreen mode

when you do something intentionally wrong, as well as:

expect(res.body).toEqual('Post not found');
Enter fullscreen mode Exit fullscreen mode

Various other uses of expect here

Collapse
 
nedsoft profile image
Chinedu Orie

Yeah, I've included some failure test cases in the sample project, I'd update the article to reflect it. Thanks for the hints.

Collapse
 
nedsoft profile image
Chinedu Orie

Yeah, that's true, I seemed to focus on the success side. I'll update it to take into account your feedback. Thank you.

Collapse
 
devorji profile image
Chigbogu Orji

I tried using jest after I first read your article, but as I changed from Windows7 to Windows10 after Microsoft stopped its update(windows7); my jest test ain't working anymore.

I got the error app.address is not a function
What could be the cause, am using yarn as my package manager.

I have being using mocha and chai for my tests and it works, none works these days
Thanks in anticipation.

Collapse
 
nedsoft profile image
Chinedu Orie

Could you check if the discussion in this thread helps

Collapse
 
prashant1k99 profile image
Prashant Singh

Thanks for the article, I followed the instructions and wrote the test code. For anyone who is looking for the sample Express Route Test using Jest (As explained in the above article) can visit this sample GitHub Repo I created.
Repo Link: github.com/prashant1k99/JWT-example

Collapse
 
mememe profile image
mememe

This is good content. I hope you get to complete the whole CRUD testing.

Collapse
 
elishaking profile image
King Elisha

Great article

Collapse
 
martinnuc profile image
Martin Nuc

It looks like the database is not refreshed between endpoint tests, right? Means endpoint tests could affect each other, right?

Collapse
 
nedsoft profile image
Chinedu Orie

Yes, that is definitely a short-cut solution, you could make each test suite independent of each other

Collapse
 
yassern profile image
Yasser Nascimento

Nice! Thanks a lot :)

Collapse
 
brunolnetto profile image
Bruno Peixoto

I receive a timeout with log ": Timeout - Async callback was not invoked within the 10000ms timeout specified by jest.setTimeout.Timeout - Async callback was not invoked within the 10000ms timeout specified by jest.setTimeout.Error:
"

Collapse
 
akopchinskiy profile image
akopchinskiy

What about more real-world complex example with database connectors and sharing them around several test suites with Jest? )))

Collapse
 
isaackomeza profile image
Isaac Komezusenge

Good article with rich content!! Thank you Orie

Collapse
 
adilsoncarvalho profile image
Adilson Carvalho

Hello Chinedu,

It's 2021 and your post still helping people. I am one of them.

Cheers mate!

Collapse
 
gabrielbb profile image
Gabriel Basilio Brito • Edited

Step 2 is not needed anymore. Is the default behavior. Except the test script of course

Collapse
 
gennady77 profile image
Gennady77

Links in the Step4 doesn't work

Collapse
 
hidayahweb profile image
Hidayah Ramadlana (ID)

your website oriechinedu.com/ is not accessible

Collapse
 
warrenwongcodes profile image
Warren Wong

The two links to how to setup database are erroring out.

Collapse
 
kiorq profile image
Kirk Douglas Jr

Thanks

Collapse
 
kedar9444 profile image
Kedar Paritkar

set content-type text/plain not working in supertest. every time it converts the text to object. any idea why ?

Collapse
 
shubhu2812 profile image
shubhu2812

In this case how can I get codecoverage?

Collapse
 
kartick17 profile image
Kartick Sadhu

After configureing jest when i run sample test code i got this error
Image description

Collapse
 
emmadegreat profile image
Emma F. Mkpurunchi

Nice and enriching article.

Collapse
 
anjneeksharma profile image
anjneeksharma

i did the same setup but i am getting bellow error.
"npm ERR! missing script: migrate:reset"

Collapse
 
nedsoft profile image
Chinedu Orie

The error said that a script is missing? do you have the migrate:reset script in package.json?

Collapse
 
zainabsaad profile image
Zainab-Saad

Thanks for this great tutorial, the links in this post are not working, could you please update the post?
It would be great help :)

Collapse
 
deshaware profile image
Swapnil Deshaware

How do we make it exit after completing the test case executions, apologies if this isn't the right place to ask such query, but it'd be helpful

Collapse
 
deshaware profile image
Swapnil Deshaware

Nevermind I got it, we must use --forceExit. Thank you for the post. It helped me today :) Cheers to you!!!