Simple Unit Testing NodeJS Express

+1 vote

I have been very new testing, so can anyone of you help me with how to write a test case for the below code.

import fs from 'fs';
import express from 'express';
const app = express();
const port = 6500;

app.get('/',(req,res)=>{
    res.send('<h1>Welcome to api For Fs</h1>')
});
app.listen(port,(err)=>{
    console.log(`Server is running on port ${port}`)
})

Jul 16, 2019 in Others by shubham
• 7,340 points
1,201 views

1 answer to this question.

0 votes

You can test it with mocha which is a test runner and chai which is an assert module (Which helps to verify your result)

let chai = require('chai');
let chaiHttp = require('chai-http');
let expect = chai.expect;
chai.use(chaiHttp);

describe('Testing my get Api', () => {
    it('should be return status 200 for /', function(done){
        chai
            .request('http://localhost:6500')
            .get('/')
            .then(function(res){
                expect(res).to.have.status(200);
                done();
            })
            .catch(function(err){
                throw(err);
            });
    });
}}
answered Jul 16, 2019 by sunshine
• 1,300 points

Related Questions In Others

0 votes
0 answers

Mention the difference between Data Driven Testing and Retesting?

Retesting:  It is a process of checking ...READ MORE

Feb 1, 2019 in Others by riya

reopened Nov 12 by Neelam 3,254 views
0 votes
1 answer

What is functional testing?

Functional testing a type of testing where ...READ MORE

answered Feb 18, 2019 in Others by Jobin
655 views
0 votes
1 answer

How is regression testing different from functional testing?

Functional testing is testing a section of code ...READ MORE

answered Feb 18, 2019 in Others by Mythlli
689 views
0 votes
1 answer

What sort of testing is involved in functional testing?

The following types of testing are involved ...READ MORE

answered Feb 18, 2019 in Others by Jobin
621 views
0 votes
1 answer

How do I carry out functional testing?

Well, functional testing is pretty simple. it ...READ MORE

answered Feb 18, 2019 in Others by Jobin
765 views
0 votes
3 answers

Can we run selenium tests (Firefox) on WebDriver without a GUI?

Hi ,  Yes, you can use headless browsers ...READ MORE

answered Sep 2, 2020 in Selenium by Sri
• 3,190 points
4,018 views
0 votes
2 answers

How can we take screenshots of tests in Selenium 2 using C#

Hey, try using following code command to ...READ MORE

answered Aug 23, 2019 in Selenium by Abha
• 28,140 points
1,434 views
0 votes
1 answer
0 votes
2 answers

TestNG error:- Cannot find class in classpath

The above link didn't work it means ...READ MORE

answered Feb 12, 2020 in Selenium by Rashmi Reddy PR
40,980 views
0 votes
1 answer

NodeJS: Serving dynamic files

You must use the EJS (Embedded JavaScript template) ...READ MORE

answered Jul 17, 2019 in Others by sunshine
• 1,300 points
4,289 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP