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, 2019 in Others by shubham
• 6,910 points
49 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, 2019 by sunshine
• 1,260 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

closed Feb 1, 2019 by Omkar 248 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
60 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
31 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
62 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
31 views
0 votes
2 answers

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

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

answered Aug 22, 2019 in Selenium by Abha
• 27,720 points
330 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 22, 2019 in Selenium by Abha
• 27,720 points
213 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,510 points
10,185 views
0 votes
1 answer

NodeJS: Serving dynamic files

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

answered Jul 16, 2019 in Others by sunshine
• 1,260 points
136 views