Make mocha test fail in try / catch - node.js

I have the following function to validate a JSON file:
export const validateJSON = (sourceDir, fileName) => {
return new Promise((resolve, reject) => {
try {
pricingService.processJSON(params, data)
.then(data => {
config.accounts.currencies.forEach(function (currency) {
if (data[currency] === '' || data[currency] === undefined) {
reject({err: 'Missing required values'});
}
});
resolve('JSON Validation Success');
})
.catch(error => {
logger.error({
message: `Error while processing JSON, request Failed with Error: ${error}`,
});
reject({err: error});
});
}
} catch (error) {
logger.error({
message: `Error while validating JSON file, request Failed with Error: ${error}`,
});
return reject({err: {status: 422, message: 'Error while validating JSON - Unprocessable Entity'}});
}
});
};
Now, I have a test in Mocha
it.only('Validate Pricing JSON file', function () {
s3Service.validateJSON('',filename,Date.now()).then(data =>{
setTimeout(() =>{
assert.equal(data, 'JSON Validation Success')
done()
}, 1000)
}).catch(function (error) {
console.error("JSON validation failed "+JSON.stringify(error))
throw error
})
});
What I am trying to do is to validate the JSON file in the test and if there is some fields are missing in the file, the test should fail. Now when I execute the test with a file with missing entries, I am getting the following error printed to console.
JSON validation failed {"err":{"status":422,"message":"Error while validating JSON - Unprocessable Entity"}}
(node:26091) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): #<Object>
(node:26091) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
But the test is shown as passed. How can I make it fail if there is an error ? Also how to get rid of the UnhandledPromiseRejectionWarning ? I am very new to Unit Testing and so any help will be appreciated.

Don't throw an error.
Use assert.fail
Check this

Related

Why am I having trouble displaying and deleting in Mongo using Prisma (React app)?

I use Node, Express, React, Mongo and Prisma to import a csv file in the database, display it on the frontend and delete all records in the db. It worked with one record and so I assumed it would work with the rest of the csv file (1000 records). But I get an error:
Invalid `prisma.movie.findMany()` invocation:
Error occurred during query execution:
ConnectorError(ConnectorError { user_facing_error: None, kind: RawError { code: "unknown", message: "Command failed (CursorNotFound): cursor id 124425195753416376 not found)" } })
(node:2171) UnhandledPromiseRejectionWarning: Error:
Invalid `prisma.movie.deleteMany()` invocation:
Error occurred during query execution:
ConnectorError(ConnectorError { user_facing_error: None, kind: RawError { code: "unknown", message: "Command failed (CursorNotFound): cursor id 4391617472265441923 not found)" } })
at cb (/Users/nwsursock/Sites/test-algot/backend/node_modules/#prisma/client/runtime/index.js:36378:17)
at runMicrotasks ()
at processTicksAndRejections (internal/process/task_queues.js:93:5)
at async exports.deleteRequest (/Users/nwsursock/Sites/test-algot/backend/src/controllers/movie.controller.js:49:3)
(node:2171) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
My code is rather simple. I'm using the Prisma API in REST endpoints.
const { PrismaClient } = require("#prisma/client");
const prisma = new PrismaClient();
exports.createRequest = async (req, res) => {
const movie = req.body.movie;
console.log("============> importing", movie);
const data = {
name: movie.Name,
genre: movie.Genre,
descr: movie.Description,
director: movie.Director,
actors: movie.Actors,
year: movie.Year,
runtime: Number(movie["Runtime (Minutes)"]),
rating: Number(movie.Rating),
votes: Number(movie.Votes),
revenue: Number(movie["Revenue (Millions)"]),
score: Number(movie.Metascore),
};
const result = await prisma.movie.create({ data });
console.log("============> imported", result);
res.status(201).json({ message: "Movie correctly added!" });
};
exports.readRequest = async (req, res) => {
try {
const movies = await prisma.movie.findMany();
res.status(200).json(movies);
} catch (e) {
console.log("======> Error:", e.message);
}
};
exports.deleteRequest = async (req, res) => {
await prisma.movie.deleteMany({});
res.status(202).json({ message: "Table deleted!" });
};
It's a version problem. You have to downgrade to Prisma 2.26. Above, the bug appears. https://github.com/prisma/prisma/issues/8389

Getting UnhandledPromiseRejectionWarning error while performing with axios on Nodejs

I used nested request by using axios. Initially requesting POST request for the token with key/secrets and then trying to get results in following GET request by using previous request's token.
var config = {
method: 'post',
url: gsecConfig.tokenUrl,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data: data
};
axios(config)
.then(function (response) {
if (response.data.access_token) {
const config = {
headers: { Accept: 'application/json', Authorization: `Bearer ${response.data.access_token}` }
};
axios.get(gsecConfig.gsecUrl + gsecid, config)
.then(function (response) {
let supplierData = response.data;
res.status(200).json({
"data": supplierData
});
}).catch(function (error) {
res.status(error.response.status || error.response).json({
"errors": [error.response.data]
});
});
}
})
.catch(function (error) {
res.status(error.response.status || error.response).json({
"errors": [error.response.data]
});
});
while running the application getting below errors.
(node:32) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'status' of undefined
at /whitelist-v1/nodejs/routes/gsec.js:47:39
at process._tickCallback (internal/process/next_tick.js:68:7)
(node:32) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:32) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
It's highly likely your error doesn't have a .response member, making error.response.status failing. Errors may be decorated with .response only if they originate from the network - the standard Error object doesn't declare it.
So both catches should probably be more like:
res.status(error.response && error.response.status || 500).json({
"errors": [error.response && error.response.data || error.message]
});
If it's not error.response that yields an undefined, then your res is undefined, which should be more obvious to catch.

chai and mocha showing test successful after error

I am using chai and mocha to test my REST API developed in NodeJS with typescript. I have written my test cases with mockgoose library to mock mongo db. When I run the first test case it successfully adds some data in database, but when I run the second and third test case it first shows Cannot set headers after they are sent to the client and later shows that the test is passed. I am not getting this workflow of execution how is it happening. Could anyone please explain.
My test case file looks like this :
process.env.NODE_ENV = 'TEST';
import { expect } from 'chai';
import request from 'supertest';
import app from '../app';
import * as mongodb from '../mongo/connection';
describe('Testing the API', () => {
before((done) => {
mongodb.connectMock()
.then(() => done())
.catch((err:any) => done(err))
})
it('OK, adding new employee', (done) => {
request(app).put('/add')
.send(<some JSON data>)
.then(res => {
...some matcher
done();
})
.catch(err => done(err));
})
it('OK, getting all employees', (done) => {
request(app).get('/all')
.then(res => {
...some matcher
done();
})
.catch(err => {
done(err)
});
})
it('OK, getting employee by ID', (done) => {
request(app)
.get(`/create/${id}`)
.then(res => {
...some matcher
done();
})
.catch(err => done(err));
})
})
and the controller file that produces the error is :
import { Request, Response } from 'express';
import Employee from '../models/Employee'
import { v4 as uuidv4 } from 'uuid';
export let allEmployee = (req: Request, res: Response) => {
Employee.find({})
.then(allEmployee => {
console.log('Getting all employee');
if(allEmployee.length > 0)
res.status(200).json(allEmployee);
console.log('Empty set of employees, please create');
res.status(404).json({ error: 'No employee found, please create', employee: allEmployee });
})
.catch((err:any) => {
console.log(err)
res.status(400).json({ error: err }); ****** I GET THE ERROR HERE ******
})
}
export let getEmployeeById = (req: Request, res: Response) => {
const employeeId: string = req.params.id;
Employee.find({ employeeId: employeeId })
.then(employee => {
console.log(`Getting employee with employee ID: ${employeeId}`);
if(employee.length > 0)
res.status(200).json(employee);
console.log(`No employee with employee ID: ${employeeId} found`);
res.status(404).json({ error: 'No employee found', employee: employee });
})
.catch(err => {
res.status(400).json({ error: err }); ****** I GET THE ERROR HERE ******
})
}
export let addEmployee = (req: Request, res: Response) => {
let employee = new Employee(<Some employee data in JSON>)
employee.save()
.then(employeeSaved => {
res.status(201).json({ message: 'New employee created!', employee: employeeSaved });
})
.catch(err => {
res.status(400).json({error:err});
})
}
The first test case that adds the employee to database works perfectly fine but when it does for second and third test case it shows error first and then successfully passes the test case.
It looks like :
Mock MongoDB Connected
✓ OK, adding new employee (69ms)
Getting all employees
Empty set of employees, please create
(node:16761) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:526:11)
at ServerResponse.header (/Users/abc/Downloads/some/git-test/some/Docker_Solution/node_modules/express/lib/response.js:771:10
)
at ServerResponse.send (/Users/abc/Downloads/some/git-test/some/Docker_Solution/node_modules/express/lib/response.js:170:12)
at ServerResponse.json (/Users/abc/Downloads/some/git-test/some/Docker_Solution/node_modules/express/lib/response.js:267:15)
at /Users/abc/Downloads/some/git-test/some/Docker_Solution/src/controllers/employeeController.ts:16:33
at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:16761) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch bl
ock, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rej
ections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 9)
(node:16761) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
✓ OK, getting all employees
Getting employee with employee ID: 2b1e419e-57a7-4785-a3d7-96a1c786676b
No employee with employee ID: 2b1e419e-57a7-4785-a3d7-96a1c786676b found
(node:16761) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:526:11)
at ServerResponse.header (/Users/abc/Downloads/some/git-test/some/Docker_Solution/node_modules/express/lib/response.js:771:10)
at ServerResponse.send (/Users/abc/Downloads/some/git-test/some/Docker_Solution/node_modules/express/lib/response.js:170:12)
at ServerResponse.json (/Users/abc/Downloads/some/git-test/some/Docker_Solution/node_modules/express/lib/response.js:267:15)
at /Users/abc/Downloads/some/git-test/some/Docker_Solution/src/controllers/employeeController.ts:31:33
at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:16761) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 10)
✓ OK, getting employee by ID
3 passing (1s)
Your problem seems to be your if conditions for res.status(200) or res.status(400) in both of your routes you have an issue with.
if(employee.length > 0)
res.status(200).json(employee);
console.log(`No employee with employee ID: ${employeeId} found`);
res.status(404).json({ error: 'No employee found', employee: employee });
should be and if(){} else{} because you are trying to send/alter the response again along with the 200 you already send
if(employee.length > 0) {
res.status(200).json(employee);
} else {
console.log(`No employee with employee ID: ${employeeId} found`);
res.status(404).json({ error: 'No employee found', employee: employee });
}

proxyReq.setHeader can not set headers after they are sent

i am building a node.js proxy and i want to add a conditional header
proxy.on('proxyReq', (proxyReq, req, res) => {
const realIP = parseHttpHeader(req.headers['x-real-ip'])[0];
const path = parseHttpHeader(req.headers['x-original-uri'])[0];
pAny([
check_ip(realIP) ,
check_path(path) ,
check_geo(realIP)
]).then(result => {
console.log (result , "result " )
if (result) {
proxyReq.setHeader('namespace' , 'foo');
} else {
proxyReq.setHeader('namespace' , 'bar'); }
console.log('sending req ')
});
});
.
async function check_ip(realIP) {
try {
const result = await ipModel.findOne({ip: realIP}).exec()
console.log(realIP , result , "ip")
if (result) {
return true
} else {
return false
}
} catch (e) {
throw e;
}
}
and it works just fine till i use the methos check_ip then i get the error
(node:3793) UnhandledPromiseRejectionWarning: Error: Can't set headers after they are sent.
at validateHeader (_http_outgoing.js:491:11)
at ClientRequest.setHeader (_http_outgoing.js:498:3)
at /home/master/IPS/server.js:109:14
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:189:7)
(node:3793) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:3793) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
as the error clearly states i am handeling a promise in the wrong way but i don't know how to fix it i tried using callbacks i tried using await
make the check_ip return a promise and try
function check_ip(realIP) {
return ipModel.findOne({ ip: realIP }).exec();
}

How to handle promise rejection in this case

In here i want to add new field in collection User (User.facebook.botLink)
but i get an error.
Code where i get error looks like this:
app.post('/savelink/', async (req, res, next) => {
try{
console.log("================USER FACEBOOK ===============" + user.local.email)
const {link} = req.body;
console.log(link);
User.update(
{email: user.facebook.email},
{botLink : link},
{multi:true},
function(err, numberAffected){
});
res.status(200).send(link);
}
catch(e){
next(error);
}
});
Error i'm getting:
UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): ReferenceError: error is not defined
(node:6032) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
try this
catch(e){
next(error);
}
to
catch(e){
next(e);
}
You can aslo do a try/catch...finally.
Note that the finally block will be executed regardles of the result of try/catch.
For more einformation, visit: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch
try{
// some code
}
catch(error){
// handle error
}
finally {
// do something else
}

Resources