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.