Javascript unit tesing, test hitting a callback inside function - javascript

I am having trouble getting complete coverage in my testing where I am trying to hit a callback function inside the function I am testing. Here is the function :
CrowdControl.prototype.get = function() {
var options = this.optionsFor('GET');
return q.Promise(function(resolve, reject) {
function callback(error, response, body) {
if (error) {
reject(error);
} else {
resolve(body);
}
}
request(options, callback);
});
};
So I have the function covered off except the function callback :
function callback(error, response, body) {
if (error) {
reject(error);
} else {
resolve(body);
}
}
I cant seem to figure out how to hit this with tests.
Up top I have the request stubbed out like this
var request = sinon.stub();
beforeEach(function() {
CrowdControl = rewire('crowdcontrol');
CrowdControl.__set__({
request: request
});
});
So I'm not sure how I can make it hit the callback and test that. Could use some insight as this is still new to me. Thanks!
So I'm trying a simple test at first something like this -
it("should call callback function.", function() {
crowdControl.get();
//callback should fire?
expect(callback).to.have.been.called;
});

Simple way to achieve what you want is this:
CrowdControl.prototype.get = function(callback) {
callback = callback || function callback(error, response, body) {
if (error) {
reject(error);
} else {
resolve(body);
}
};
var options = this.optionsFor('GET');
return q.Promise(function(resolve, reject) {
callback();
request(options, callback);
});
};
You can now inject the function in when testing (it will use the real implementation if the argument is not supplied).
CrowdControl.get(someFakeFunction);

When you are working with functions that return promise, you have to make your asserts (expect) are inside then(), so it will look something like this:
it("should call callback function.", function(done) {
var callback = sinon.stub();
crowdControl.get(callback).then(function(){
expect(callback).to.have.been.called;
done();
});

Related

Convert a Promise Based Approach to Callbacks

I'm new to js. I've read many of the prior stack overflow posts on asynchronicity but still don't understand the question below.
I have the following code to upload an image file to an S3 bucket. The key thing the code needs to achieve is to have the image1 variable in the parent store the information in the data variable from the s3.upload call in the child function.
My code using promises below works fine. My understanding is that the same thing can be done using callbacks only, and I've been trying to rewrite the code below with callbacks only as a learning exercise, but it has not worked. How would I change this code to do the same thing with callbacks and no promises?
Parent function:
try {
image1 = await uploadToAWSBucket(file);
}
catch (error) {
return next(error);
}
Child:
const uploadToAWSBucket = async (fileObject) => {
let randomFileName = shortid.generate();
const AWSUploadObject = {
Bucket: BUCKET_NAME,
Key: randomFileName,
Body: fileObject.buffer,
ContentType: fileObject.mimetype,
};
return new Promise((resolve, reject) => {
s3.upload(AWSUploadObject, (err, data) => {
if (err) {
return reject(err);
}
return resolve(data);
});
});
};
At first, you need to add a callback arg to you async function, removing async keyword
const uploadToAWSBucket = (fileObject, callback) => {
Next, you need to handle s3 response in a callback manner replacing Promise with callback usage.
s3.upload(AWSUploadObject, (err, data) => {
if (err) {
callback(err);
return;
}
callback(null, data);
});
Or maybe you can even simplify it to
s3.upload(AWSUploadObject, callback)
You also need to update your usage to a callback manner
uploadToAWSBucket(file, (error, image1) => {
if (error) {
next(error);
return;
}
// your success code here
});
The final result is
const uploadToAWSBucket = (fileObject, callback) => {
let randomFileName = shortid.generate();
const AWSUploadObject = {
Bucket: BUCKET_NAME,
Key: randomFileName,
Body: fileObject.buffer,
ContentType: fileObject.mimetype,
};
s3.upload(AWSUploadObject, callback);
};
That's it. I hope this explanation will help you to understand how to use callbacks.
If my understanding is correct, you want to use image1 after the catch block.
In that case, I suppose, you will be calling some function with image1. It can be done as follows, with some snippets taken from this answer:
const uploadToAWSBucket = (fileObject, callback) => { ... }; // described in the linked answer
uploadToAWSBucket(file, function callback(error, image1) {
if(error) { return next(error); }
someOtherFunction(image1, next); // "next" is passed as callback, with the assumption that nothing else needed to be called after that.
});
If you want to call 2 more functions with the result of someOtherFunction, it can be done as follows:
uploadToAWSBucket(file, function callback(error, image1) {
if(error) { return next(error); }
someOtherFunction(image1, function someOtherFunctionCb(error, someOtherFunctionResult) {
if(error) { return next(error); }
someOtherFunction2(someOtherFunctionResult, function someOtherFunction2Cb(error, someOtherFunction2Result) {
if(error) { return next(error); }
someOtherFunction3(someOtherFunction2Result, function someOtherFunction3Cb(error, someOtherFunction3Result) {
if(error) { return next(error); }
next(null, someOtherFunction3Result);
});
});
});
});
Basically, you cannot have local global variables if you use callbacks. I will try to explain a problem situation.
let image1 = null;
uploadToAWSBucket(file, function uploadToAWSBucketCallback(error, _image1) {
if(error) { return next(error); }
image1 = _image1;
});
someOtherFunction(image1, function someOtherFunctionCb(error, someOtherFunctionResult) {
if(error) { return next(error); }
...
});
In the above snippet, someOtherFunction will be called before uploadToAWSBucketCallback is executed. That means, image1 is not assigned with _image1. Now, you know what will be the value of image1 when someOtherFunction is called.
The second snippet shows how to pass result of one async function to another, by nesting the subsequent calls inside the callbacks. This makes code less readable for many. There are libraries like async, which helps to make things easier & readable.
The second snippet can be rewritten with async library's waterfall function like this:
async.waterfall([
function uploadToAWSBucketStep(callback) {
uploadToAWSBucket(file, callback);
},
function someOtherFunctionStep(image1, callback) {
someOtherFunction(image1, callback);
},
function someOtherFunction2Step(someOtherFunctionResult, callback) {
someOtherFunction2(someOtherFunctionResult, callback);
},
function someOtherFunction3Step(someOtherFunction2Result, callback) {
someOtherFunction3(someOtherFunction2Result, callback);
}
], function lastStep(error, someOtherFunction3Result) {
if(error) { return next(error); };
next(null, someOtherFunction3Result);
});
Promisifcation of a callback-based function is well understood and well documented.
I have never seen a discussion of "de-promisification", but it is pretty simple.
Starting with your uploadToAWSBucket() and assuming you want your callback to be "nodeback" style (signature (err, data)), then you can write:
const uploadToAWSBucketNodeback = (fileObject, nodeback) => {
uploadToAWSBucket(fileObject) // call the promise-returning "async" version.
.then(data => { // success path
nodeback(null, data);
})
.catch(nodeback); // error path
};
Or you could write a generic de-promisifier ...
const depromisify = (asyncFunction) => {
return function(...params) {
let nodeback = params.pop(); // strip nodeback off the end of params
asyncFunction(...params)
.then(data => { // success path
nodeback(null, data);
})
.catch(nodeback); // error path
}
};
... then
const uploadToAWSBucketNodeback = depromisify(uploadToAWSBucket);
Either approach will allow you to write:
uploadToAWSBucketNodeback(fileObject, function(err, data)) {
if(err) {
// handle error
} else {
// handle data
}
}
Notes
we just need to know that the original asyncFunction is thenable/catchable.
the original asyncFunction is completely opaque to the depromisified function.
we don't need to know anything about the internal workings of the original asyncFunction. Thus, the composition of AWSUploadObject doesn't need to be replicated ..... it is still performed by uploadToAWSBucket().
After reading everyone's responses, I came up with the following which I think works and is basically what #Pavlo Zhukov suggested. (Note the function names have changed slightly from my earlier post.)
Code from parent function:
let image1;
uploadToAWSBucketCallbackStyle(file, (err, data) => {
if (err) {
return next(err);
}
image1 = data;
// Do stuff with image1 here or make additional function
// calls using image1.
});
Child function:
const uploadToAWSBucketCallbackStyle = (fileObject, callback) => {
let randomFileName = shortid.generate();
const AWSUploadObject = {
Bucket: BUCKET_NAME,
Key: randomFileName,
Body: fileObject.buffer,
ContentType: fileObject.mimetype,
};
s3.upload(AWSUploadObject, callback);
}

How to get data from a different node.js code

I have a code snippet in the db.js as below,
exports.asyncGetAllData = function () {
connection.connect(function(err) {
connection.query(sqlGetAllData, function (err, result) {
if (err) reject(err);
else
{
//console.log(result);
}
});
});
};
And I want to get the result data when I called the function in app.js as below.
app.get('/test/getPriceTrend', function(req, res) {
console.log('SERVER::getPriceTrend');
console.log(req.url);
var data = db_connection.asyncGetAllData(); //data is undefined
console.log(data);
res.setHeader('Accept', 'application/json');
res.writeHead(res.statusCode);
//The following piece of code will send information from the database
res.write(JSON.stringify({"hello":"world"}));
res.end();
});
As you can see, when I tried to fetch data from db.js, it shows in the console window "the data is undefined". How can I solve this issue? Any suggestion?
Thanks in advance,
Looks like you are calling for data using async method and not waiting for the response.
var data = db_connection.asyncGetAllData(); //data is undefined
console.log(data);
Either use a function that would get you SyncData or use a callback as in:
exports.asyncGetAllData = function (cb) {
connection.connect(function(err) {
connection.query(sqlGetAllData, function (err, result) {
if (err) reject(err);
else
{
//console.log(result);
cb(data);
}
});
});
};
var data = db_connection.asyncGetAllData(function(data) {
console.log(data);
res.write(JSON.stringify(data));
res.end();
});
The easiest way to do this is to create a callback function that you pass to asyncGetAllData()
Your function would look more like this:
exports.asyncGetAllData = function (callback) {
connection.connect(function(err) {
connection.query(sqlGetAllData, callback)
})
}
Then in you app.js you pass the callback in:
db_connection.asyncGetAllData(function(err, result{
if (err) reject(err);
else
{
//console.log(result);
}
})
You could also adjust asyncGetAllData to return a promise,which might make things a little prettier.

Conditional async function

I have my function getting an email from Gmail. I want to run this function n times or until an email is found.
What is a proper way to do it? I tried: http://caolan.github.io/async/docs.html#retry but without success.
I was following this article how to read emails: https://developers.google.com/gmail/api/quickstart/nodejs
Assuming you have a routine called gmail, which returns a promise which succeeds (fulfills) if an email is found, and otherwise fails (rejects), then:
function get(n) {
return gmail().catch(() => {
if (!n) throw "Too many tries!";
return get(--n);
};
}
Usage:
get(5).then(
mail => console.log(mail.body),
() => console.log("No mail!"));
If for some reason you do not like the recursive style:
function get(n) {
let promise = Promise.reject();
do { promise = promise.catch(gmail); } while (n--);
return promise;
}
If gmail is callback style, then
function get(n, cb) {
gmail(function(err, data) {
if (err)
if (!n) get(--n, cb);
else cb("Too many tries!");
else cb(null, data);
});
}
Or better yet, promisify gmail, either using a library or
function promisify(fn) {
return new Promise((resolve, reject) {
fn(function(data, err) {
if (err) reject(err);
else resolve(data);
});
});
}
and then replace gmail in the first solution with promisify(gmail).

Timing issue while writing mocha test cases

I am trying to write a test case for one of my REST Apis using mocha.
My Rest api looks like this:
server.route({
method : "DELETE",
path : "/local/{id}",
handler: function (request, reply) {
var id = request.params.id;
return getId(id)
.then(function (result) {
return testFunction(result, id, reply);
})
.catch (function (err) {
reply(400).err;
})
}
function testFunction(result, id, reply) {
return update(id, result)
.then(function (resp) {
reply(resp);
stopSomething(id)
.then(function () {
//do some work
return Bluebird.resolve(data);
})
.catch(function (error) {
//handle error & just log..user does not need to know
//stopSomething is a promise chain which runs in background
});
})
.catch(function (err) {
//handle error and reply to user
});
}
});
To test this I wrote the following test case:
describe("with an valid id", function () {
var stopSomethingStub;
var result
before(function () {
stopSomethingStub = Sinon.stub(stopSomethinbObject, "stopSomething", function () {
return Bluebird.resolve();
});
return new Request("DELETE", "/local/id123").inject(server)
.then(function (data) {
result = data;
});
});
after(function () {
//do clean up of stubs
});
it("deletes id", function () {
expect(result.statusCode).to.equal(200);
expect(stopSomethingStub.called).to.be.true;
//Few more checks
});
}
Right now, the "it" block executes immediately after receiving the 200 Ok response for the DELETE Request. However I would like it to finish the entire promise chain before checking the assertion. stopSOmethingStub.called is shown as false if I keep it as the first expect block. However if I keep it as the last assertion it works. I think this is some timing issue.

Return Meteor.http results in method

I have a Meteor method that wraps around an http.get. I am trying to return the results from that http.get into the method's return so that I can use the results when I call the method.
I can't make it work though.
Here's my code:
(In shared folder)
Meteor.methods({
getWeather: function(zip) {
console.log('getting weather');
var credentials = {
client_id: "string",
client_secret: "otherstring"
}
var zipcode = zip;
var weatherUrl = "http://api.aerisapi.com/places/postalcodes/" + zipcode + "?client_id=" + credentials.client_id + "&client_secret=" + credentials.client_secret;
weather = Meteor.http.get(weatherUrl, function (error, result) {
if(error) {
console.log('http get FAILED!');
}
else {
console.log('http get SUCCES');
if (result.statusCode === 200) {
console.log('Status code = 200!');
console.log(result.content);
return result.content;
}
}
});
return weather;
}
});
For some reason, this does not return the results even though they exist and the http call works: console.log(result.content); does indeed log the results.
(Client folder)
Meteor.call('getWeather', somezipcode, function(error, results) {
if (error)
return alert(error.reason);
Session.set('weatherResults', results);
});
Of course here, the session variable ends up being empty.
(Note that this part of the code seems to be fine as it returned appropriately if I hard coded the return with some dummy string in the method.)
Help?
In your example Meteor.http.get is executed asynchronously.
See docs:
HTTP.call(method, url [, options] [, asyncCallback])
On the server, this function can be run either synchronously or
asynchronously. If the callback is omitted, it runs synchronously and
the results are returned once the request completes successfully. If
the request was not successful, an error is thrown
Switch to synchronous mode by removing asyncCallback:
try {
var result = HTTP.get( weatherUrl );
var weather = result.content;
} catch(e) {
console.log( "Cannot get weather data...", e );
}
Kuba Wyrobek is correct, but you can also still call HTTP.get asynchronously and use a future to stop the method returning until the get has responded:
var Future = Npm.require('fibers/future');
Meteor.methods({
getWeather: function(zip) {
console.log('getting weather');
var weather = new Future();
var credentials = {
client_id: "string",
client_secret: "otherstring"
}
var zipcode = zip;
var weatherUrl = "http://api.aerisapi.com/places/postalcodes/" + zipcode + "?client_id=" + credentials.client_id + "&client_secret=" + credentials.client_secret;
HTTP.get(weatherUrl, function (error, result) {
if(error) {
console.log('http get FAILED!');
weather.throw(error);
}
else {
console.log('http get SUCCES');
if (result.statusCode === 200) {
console.log('Status code = 200!');
console.log(result.content);
weather.return(result);
}
}
});
weather.wait();
}
});
There's not really much advantage to this method over a synchronous get in this case, but if you're ever doing something on the server which can benefit from something like an HTTP call running asynchronously (and thus not blocking the rest of the code in your method), but you still needs to wait for that call to return before the method can, then this is the right solution. One example would be where you need to execute multiple non-contingent gets, which would all have to wait for each other to return one by one if executed synchronously.
More here.
Sometimes asynchronous calls are preferable. You can use async/await syntax for that, and you need to promisify HTTP.get.
import { Meteor } from 'meteor/meteor';
import { HTTP } from 'meteor/http';
const httpGetAsync = (url, options) =>
new Promise((resolve, reject) => {
HTTP.get(url, options, (err, result) => {
if (err) {
reject(err);
} else {
resolve(result);
}
});
});
Meteor.methods({
async 'test'({ url, options }) {
try {
const response = await httpGetAsync(url, options);
return response;
} catch (ex) {
throw new Meteor.Error('some-error', 'An error has happened');
}
},
});
Notice that meteor test method is marked as async. This allows using await operator inside it with method calls which return Promise. Code lines following await operators won't be executed until returned promise is resolved. In case the promise is rejected catch block will be executed.

Categories

Resources