JavaScript testing promise - javascript

I'm trying to test a function which recives a promise like this
{
state: 'fulfilled'
value:[Array]
}
function register (foo) {
return new Promise((resolve,reject)=>{
query = "INSERT IGNORE INTO....";
connection.query(query, [foo.value.from, foo.value.id], (err, res, fields) => {
if(err){
return undefined //this will change
}
return foo
})
})
}
The thing is I am returning, non rejecting neither resolving. So when Im testing..
it('insertion error', function () {
var res = 'error'
connection = {
query: (query, input, cb) =>{
return cb(res, null, null)
}
}
let database = proxyquire('path/to',{
'./connection': connection
})
var input =
{
value: {
id: 'bar',
data: [],
from: 'foo'
}}
return database.register(input)
.then( (result) => {
expect(result).to.be.undefined
})
.catch( (err) => {
console.log(err)
err = new Error (`Test fail: ${err}`)
throw err;
})
})
The function works well, Im pretty sure of that.
The other thing I'm sure of is that the THEN/CATCH condition never shows up. I know if I replace the return for resolve it will show up, but I need to be returned.
What should I modify, or how should I test it.

This is a basic example how can you make a promisse, in this example you can send true or false in the register() function.
function register ( foo ) {
return new Promise( (resolve, reject) =>{
if(foo){
resolve('Done');
}else{
reject('Error')
}
} )
}
register(false).then( result=>{
document.write('promisse success... ', result);
} ).catch( error => {
document.write('promisse error... ', error);
} )

Related

How to properly throw an error from the promise

I have one function, which returns Promise:
updatePassword(currentPassword: string, newPassword: string): Promise<void> {
return this.fireAuth.currentUser.then((user: firebase.User) => {
if (user) {
const credentials = auth.EmailAuthProvider.credential(user.email, currentPassword)
user.reauthenticateWithCredential(credentials).then(res => {
if (res) {
user.updatePassword(newPassword)
}
}).catch(err => {
throw new Error(err)
})
}
})
}
I call it inside another component:
this.userService.updatePassword(currentPassword, newPassword).then(() => {
console.log('successfully')
}).catch(err => {
console.log('error')
})
But even when updatePassword() return Error, the function call in component still console log 'successfully' from 'then`. How to properly throw an error in my case?
You also need to return your inner promise. Like this:
updatePassword(currentPassword: string, newPassword: string): Promise<void> {
return this.fireAuth.currentUser.then((user: firebase.User) => {
if (user) {
const credentials = auth.EmailAuthProvider.credential(user.email, currentPassword)
return user.reauthenticateWithCredential(credentials).then(res => {
if (res) {
user.updatePassword(newPassword)
}
}).catch(err => {
throw new Error(err)
})
}
throw new Error('USER_NOT_FOUND')
})
}
Edit:
I also added throw if you do not get user as safety net.
Refactor your function to use async/await, and you don't need to manually throw anything.
You may wish to make those return falses some sort of throw too.
async updatePassword(currentPassword: string, newPassword: string): Promise<void> {
const user = await this.fireAuth.currentUser;
if(!user) return false;
const credentials = auth.EmailAuthProvider.credential(user.email, currentPassword);
const res = await user.reauthenticateWithCredential(credentials);
if(!res) return false;
user.updatePassword(newPassword);
return true;
}
If your fireAuth call fails, then throw a Error from the updatePassword catch block.
Check the working snippet attached.
let fireAuth = (pass = false) => {
if (pass) return Promise.resolve("passed");
return Promise.reject("failed");
};
function updatePassword(shouldPass = false) {
if (!shouldPass) {
return fireAuth(false)
.then(console.log)
.catch((err) => {
console.log('err in fireauth', err)
throw new Error('OOPS')
});
}
return Promise.resolve("success");
}
let failedResult = updatePassword()
.then()
.catch((err) => console.log("failedresult error", err.toString()));
let successResult = updatePassword(true)
.then((res) => console.log("res is", res))
.catch();

Make the return statement wait until everything else in the function is finished

I'm trying to make a function that returns the results of a SOAP call (using npm-soap in combination with node.js). The problem is that the function returns undefined because the SOAP call isn't finished yet when the return statement is reached.
I tried putting the return statement in the SOAP call callback itself, but then it returns undefined. I think this is because the return statement should be in the outer function instead of the inner function, just like I did in the example below. A console.log() in the SOAP call callback outputs the right data, so I know it's there.
How do I make the return statement wait on the inner SOAP call? Thanks!
var config = require('./config.js');
var soap = require('soap');
function getInvoices() {
let invoices;
// Connect to M1
soap.createClient(config.endpoint, function(err, client) {
// Log in
client.login(
{
username: config.username,
apiKey: config.password
},
function(err, loginResult) {
// Get invoices
client.salesOrderInvoiceList(
{
sessionId: loginResult.loginReturn.$value
},
function(err, invoiceResult) {
// Save invoices
invoices = invoiceResult;
console.log(invoices); // <- Returns the right data
// Log out
client.endSession(
{
sessionId: loginResult.loginReturn.$value
},
function(err, logoutResult) {
}
);
}
);
});
});
// Return invoices
return invoices; // <- Returns undefined
}
console.log(getInvoices(); // <- So this returns undefined as well
Have getInvoices return a Promise which you can then resolve once all the callbacks finish i.e.
function getInvoices() {
return new Promise((resolve, reject) => {
// Connect to M1
soap.createClient(config.endpoint, (err, client) => {
if (err) return reject(err);
// Log in
client.login({
username: config.username,
apiKey: config.password
}, (err, loginResult) => {
if (err) return reject(err);
// Get invoices
client.salesOrderInvoiceList({
sessionId: loginResult.loginReturn.$value
}, (err, invoiceResult) => {
if (err) return reject(err);
// Log out & resolve the Promise
client.endSession({
sessionId: loginResult.loginReturn.$value
}, (err, logoutResult) =>
err ? reject(err) : resolve(invoiceResult)
);
});
});
});
}
...
(async () => {
try {
const invoices = await getInvoices();
console.log(invoices);
} catch (e) {
console.error(e);
}
})();

javascript promise all array of values passed as argument

Given the following array of values:
var sportList = ['football', 'volleyball'];
i want to run a query on mongo database using each of these values:
function myFunc(sport, callback) {
mongoDB.sports.find({'name': sport}, function (error, result) {
if (error) {
callback(error)
} else {
callback(null, result)
}
})
}
so i build my promises like:
var promises = sportList.map(function(val){
return myFunc(val);
});
and then trying to run all in a promise all chain:
Promise.all(promises)
.then(function (result) {
console.log('log results: ', result);
})
.catch(function (error) {
console.log(error);
});
but this is not working, because it is complaining that the callback is undefined, how can i fix this up correctly?
The reason for the error is that you are calling the myFunc method without supplying the callback parameter.
A solution would be to replace the myFunc function with the below. This function will return a new Promise. I haven't tested the below code but it should work.
function myFunc(sport) {
return new Promise((resolve, reject) => {
mongoDB.sports.find({'name': sport}, function (error, result) {
if (error) {
reject(error);
} else {
resolve(result);
}
})
}));
}

Error: TypeError: Cannot read property 'catch' of undefined when trying to register user for website node.js

I seem to have messed up one of my promises (I think) in a javascript function that is supposed to register a user. I have included the post request and the actual function itself.
app.post("/register", (req, res) => {
dataServiceAuth.registerUser(req.body).then(() => {
res.render("register", {successMessage: "User created"});
}).catch((err) => {
res.render("register", {errorMessage: err, user: req.body.user});
});
});
module.exports.registerUser = function (userData) {
return new Promise(function (resolve, reject) {
if (userData.password != userData.password2) {
reject("Passwords do not match");
}
else {
let newUser = new User(userData);
newUser.save((err) => {
resolve();
}).catch((err) => {
if (err) {
if (err.code == 11000) {
reject('User Name already taken');
}
else {
reject('There was an error creating the user: ${err}');
}
}
});
}
});
};
If newUser.save can return a promise, you definitely shouldn’t be passing a callback to it, or even using the Promise constructor at all. If you really want to reject with strings, the way to implement that would be by transforming rejections from newUser.save() with .catch into new rejections by returning them, and returning the resulting promise from registerUser:
module.exports.registerUser = function (userData) {
if (userData.password != userData.password2) {
return Promise.reject("Passwords do not match");
}
let newUser = new User(userData);
return newUser.save().catch((err) => {
if (err.code == 11000) {
return Promise.reject('User Name already taken');
}
else {
return Promise.reject('There was an error creating the user: ${err}');
}
});
};

Handle error in promise ES6 nodejs

Hi I am new to ES6 and I am using promise chain
I am not getting error catch in my promise chain.
let cost, stars;
getStarInfo(req.body.star_id).then( (star) => {
let stripe_object = new Stripe(req.body.stripe_token, cost);
return stripe_object.makepayment();
}).then( (payment) => {
console.log(1 ,payment);
return savePurchase(req.decoded._id, cost, stars, payment.id);
}).catch( (err) => {
res.json({'success' : false , 'err' : err , msg : 'Something went wrong please try again'});
});
My savePurchase function is like this
function savePurchase( cost , stars, payment_id){
console.log("hello")
return new Promise( (resolve, reject) => {
var purchasedstars = new Stars({
user_id : user_id,
stars : stars,
money_earned : cost,
transaction_id : payment_id
});
console.log(purchasedstars)
purchasedstars.save(function(err , saved_doc){
console.log('save' , err , saved_doc)
if(err){
reject(err)
}else{
resolve(saved_doc);
}
});
});
}
In savePurchase function if my user_id is undefined, the promise does not give me error. It just goes in the catch and give empty error object. How can I find out the error in my function.?
After returning a new promise from savePurchase you chaining your catch with it, but not with getStarInfo promise anymore, so you have no error handler for getStarInfo promise.
.then() takes an optional second function to handle errors
var p1 = new Promise( (resolve, reject) => {
resolve('Success!');
// or
// reject ("Error!");
} );
p1.then( value => {
console.log(value); // Success!
}, reason => {
console.log(reason); // Error!
} );
Define custom error and reject.
purchasedstars.save(function (err, saved_doc) {
console.log('save', err, saved_doc)
if (err) {
reject({
err,
message: 'Some error message'
})
} else {
resolve(saved_doc);
}
});

Categories