I have a controller/route file like this..
const express = require('express');
const pService = require('../../services/parameter/service');
let router = express.Router()
router.get('/valuebyobject/:main/:sub?', pService.getValue);
and the getValue function is like this below..
const getValue = async (req, res, next) => {
try {
let ts = Date.now();
const obj= {};
obj.utime = ts;
obj.data =[];
const valueMain = req.params.main;
const sub = req.params.sub;
var jsonPath = path.join(__dirname, '..','..', 'data','Format_'+valueMain+'.json');
fs.readFile(jsonPath, (err, data) => {
if (err) return res.status(404).json({
'code': 'NOT_FOUND',
'description': `Required Main Parameter: ${valueMain} result is empty`
});
let file = JSON.parse(data);
if(sub){ //If 'sub' param is not empty, take both 'main' and 'sub'
const filterData = file['Data'].filter(element=>{
if (element.Id == sub) //Filtering according to Main & Sub params
obj.data.push({'My': parseInt(valueMain), 'Other': parseInt(element.Id), 'Value': element.Value})
return element;
})
if(typeof filterData != "undefined" && filterData != null && filterData.length != null && filterData.length > 0) //Checking if array is not empty
{
console.log(obj)
res.send(obj);
}
else { // If array is empty send 404 back
return res.status(404).json({
'code': 'NOT_FOUND',
'description': `Required Sub Parameter ${sub} result is empty`
});
}
}
else { // if 'sub' parameter is empty then query with only 'main' param
const filterData = file['Data'].filter(element=>{
obj.data.push({'My': parseInt(valueMain), 'Other': parseInt(element.Id), 'Value': element.Value})
return element;
})
if(typeof filterData != "undefined" && filterData != null && filterData.length != null && filterData.length > 0) //Checking if array is not empty
{
console.log(obj)
res.send(obj);
}
else { // If array is empty send 404 back
return res.status(404).json({
'code': 'NOT_FOUND',
'description': `Required Sub Parameter ${sub} result is empty`
});
}
}
});
} catch (err) {
return res.status(500).json({
'code': 'SERVER_ERROR',
'description': 'something went wrong, Please try again'
});
}
}
I am testing the following route with chai and I have a test file like this below..
const chai = require("chai");
const chaiHttp = require("chai-http");
const expect = chai.expect;
const server = require("../controllers/apis/parameter");
chai.should();
chai.use(chaiHttp);
let main= 101
describe('Parameter APIs', () => {
describe('Test GET route (/valuebyobject', () => {
it('should return all ibjects', async () => {
let res = await chai
.request(server)
.get(`/valuebyobject/${main}`)
expect(res.status).to.equal(200)
});
});
});
By running the test file it gives me this error:
- Parameter APIs Test GET route (/valuebyobject should return all objects: Uncaught TypeError: res.send is not a function
But I am not able to figure out the solution how I can test my API with chai and to avoid this TypeError.
But changinging the express.Router() to normal express() in controller file from above make it work.
const express = require('express');
const pService = require('../../services/parameter/service');
let router = express()
router.use(express.json())
router.get('/valuebyobject/:main/:sub?', pService.getValue);
But i would like to use expres.Router() in my application. Can somebody please help how to test endpoints of express.Router()?