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 15 in Others by shubham
• 6,890 points
19 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 15 by sunshine
• 1,240 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 in Others by riya

closed Feb 1 by Omkar 135 views
0 votes
1 answer

What is functional testing?

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

answered Feb 18 in Others by Jobin
43 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 in Others by Mythlli
18 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 in Others by Jobin
50 views
0 votes
1 answer

How do I carry out functional testing?

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

answered Feb 18 in Others by Jobin
16 views
0 votes
1 answer

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

You can explore options like HTMLUnit Driver ...READ MORE

answered Mar 23, 2018 in Selenium by nsv999
• 5,050 points
188 views
0 votes
1 answer

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

We can take screenshots using below function ...READ MORE

answered Mar 23, 2018 in Selenium by nsv999
• 5,050 points
142 views
0 votes
1 answer
0 votes
1 answer

TestNG error:- Cannot find class in classpath

The issue is because the project directory ...READ MORE

answered Mar 23, 2018 in Selenium by nsv999
• 5,050 points
6,169 views
0 votes
1 answer

NodeJS: Serving dynamic files

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

answered Jul 16 in Others by sunshine
• 1,240 points
25 views