Sinon JS Stubbing/Mocking undefined properties - sinon

I am hoping someone will be able to help me out with a small issue. I am trying to stub out an undefined property that lives inside a function and return a known value, I am trying this with a simple test case:
var self = this;
self.test = function() {
return person.myId();
};
if (typeof module !== "undefined" && module.hasOwnProperty("exports")) {
module.exports = self;
}
return self;
what I have tried to do is:
it('Basic stub test', function() {
sinon.stub(code, 'person.myId').return("1234");
assert(code.test(), "1234");
});
I had hoped the above would stub any calls made to person.myId(), however, I just get any error saying: Cannot stub non-existent own property person.myId.
We use our own software that handles dependency injection (e.g. during runtime in our software person is made available) but we are attempting to write isolated unit tests outside of the software, hence the necessity for stubbing out the missing dependencies.
Has anyone attempted this kind of thing before? Am I trying to do something that isn't possible?
Thank you for any help/support anyone can provide.
Sam

I'm not sure what kind of magic the dependency injection software does, so here is a middle ground answer.
It's less than ideal because it tends to be a code smell if you're changing the code you want to test, although it fits your specific question.
I'm also assuming you're using something like mocha that provides the before & afterEach functions.
var sandbox = sinon.createSandbox();
before(function () {
var person = {
myId: () => console.log('myId no op'),
};
code.person = person;
}
it('Basic stub test', function() {
sandbox.stub(code.person, 'myId').returns('1234');
assert(code.test(), '1234');
});
afterEach(function () {
sandbox.restore();
});
Documentation Link: http://sinonjs.org/releases/v4.1.3/sandbox/
(Although the use of sandbox isn't strictly required here)

Related

Asynchronous execution of a function App Script

I've been digging around, and I'm not able to find references or documentation on how I can use Asynchronous Functions in Google App Script, I found that people mention It's possible, but not mention how...
Could someone point me in the right direction or provide me with an example?
Promises, Callbacks, or something, that can help me with this.
I have this function lets call it foo that takes a while to execute (long enough that It could time out an HTTP call).
What I'm trying to do Is to refactor it, in a way that it works like this:
function doPost(e) {
// parsing and getting values from e
var returnable = foo(par1, par2, par3);
return ContentService
.createTextOutput(JSON.stringify(returnable))
.setMimeType(ContentService.MimeType.JSON);
}
function foo(par1, par2, par3) {
var returnable = something(par1, par2, par3); // get the value I need to return;
// continue in an Async way, or schedule execution for something else
// and allow the function to continue its flow
/* async bar(); */
return returnable;
}
Now I want to realize that bit in foo because It takes to long and I don't want to risk for a time out, also the logic that occurs there it's totally client Independent, so It doesn't matter, I just need the return value, that I'll be getting before.
Also, I think It's worth mentioning that this is deployed in Google Drive as a web app.
It's been long since this, but adding some context, at that moment I wanted to scheduled several things to happen on Google Drive, and It was timing out the execution, so I was looking for a way to safely schedule a job.
You want to execute functions by the asynchronous processing using Google Apps Script.
You want to run the functions with the asynchronous processing using time trigger.
If my understanding is correct, unfortunately, there are no methods and the official document for directly achieving it. But as a workaround, that can be achieved by using both Google Apps Script API and the fetchAll method which can work by asynchronous processing.
The flow of this workaround is as follows.
Deploy API executable, enable Google Apps Script API.
Using fetchAll, request the endpoint of Google Apps Script API for running function.
When several functions are requested once, those work with the asynchronous processing by fetchAll.
Note:
I think that Web Apps can be also used instead of Google Apps Script API.
In order to simply use this workaround, I have created a GAS library. I think that you can also use it.
In this workaround, you can also run the functions with the asynchronous processing using time trigger.
References:
fetchAll
Deploy the script as an API executable
scripts.run of Google Apps Script API
Benchmark: fetchAll method in UrlFetch service for Google Apps Script
GAS library for running the asynchronous processing
If I misunderstand your question, I'm sorry.
There is another way to accomplish this.
You can use time-based one-off triggers to run functions asynchronously, they take a bit of time to queue up (30-60 seconds) but it is ideal for slow-running tasks that you want to remove from the main execution of your script.
// Creates a trigger that will run a second later
ScriptApp.newTrigger("myFunction")
.timeBased()
.after(1)
.create();
There is handy script that I put together called Async.gs that will help remove the boilerplate out of this technique. You can even use it to pass arguments via the CacheService.
Here is the link:
https://gist.github.com/sdesalas/2972f8647897d5481fd8e01f03122805
// Define async function
function runSlowTask(user_id, is_active) {
console.log('runSlowTask()', { user_id: user_id, is_active: is_active });
Utilities.sleep(5000);
console.log('runSlowTask() - FINISHED!')
}
// Run function asynchronously
Async.call('runSlowTask');
// Run function asynchronously with one argument
Async.call('runSlowTask', 51291);
// Run function asynchronously with multiple argument
Async.call('runSlowTask', 51291, true);
// Run function asynchronously with an array of arguments
Async.apply('runSlowTask', [51291, true]);
// Run function in library asynchronously with one argument
Async.call('MyLibrary.runSlowTask', 51291);
// Run function in library asynchronously with an array of arguments
Async.apply('MyLibrary.runSlowTask', [51291, true]);
With the new V8 runtime, it is now possible to write async functions and use promises in your app script.
Even triggers can be declared async! For example (typescript):
async function onOpen(e: GoogleAppsScript.Events.SheetsOnOpen) {
console.log("I am inside a promise");
// do your await stuff here or make more async calls
}
To start using the new runtime, just follow this guide. In short, it all boils down to adding the following line to your appsscript.json file:
{
...
"runtimeVersion": "V8"
}
Based on Tanaike's answer, I created another version of it. My goals were:
Easy to maintain
Easy to call (simple call convention)
tasks.gs
class TasksNamespace {
constructor() {
this.webAppDevUrl = 'https://script.google.com/macros/s/<your web app's dev id>/dev';
this.accessToken = ScriptApp.getOAuthToken();
}
// send all requests
all(requests) {
return requests
.map(r => ({
muteHttpExceptions: true,
url: this.webAppDevUrl,
method: 'POST',
contentType: 'application/json',
payload: {
functionName: r.first(),
arguments: r.removeFirst()
}.toJson(),
headers: {
Authorization: 'Bearer ' + this.accessToken
}
}), this)
.fetchAll()
.map(r => r.getContentText().toObject())
}
// send all responses
process(request) {
return ContentService
.createTextOutput(
request
.postData
.contents
.toObject()
.using(This => ({
...This,
result: (() => {
try {
return eval(This.functionName).apply(eval(This.functionName.splitOffLast()), This.arguments) // this could cause an error
}
catch(error) {
return error;
}
})()
}))
.toJson()
)
.setMimeType(ContentService.MimeType.JSON)
}
}
helpers.gs
// array prototype
Array.prototype.fetchAll = function() {
return UrlFetchApp.fetchAll(this);
}
Array.prototype.first = function() {
return this[0];
}
Array.prototype.removeFirst = function() {
this.shift();
return this;
}
Array.prototype.removeLast = function() {
this.pop();
return this;
}
// string prototype
String.prototype.blankToUndefined = function(search) {
return this.isBlank() ? undefined : this;
};
String.prototype.isBlank = function() {
return this.trim().length == 0;
}
String.prototype.splitOffLast = function(delimiter = '.') {
return this.split(delimiter).removeLast().join(delimiter).blankToUndefined();
}
// To Object - if string is Json
String.prototype.toObject = function() {
if(this.isBlank())
return {};
return JSON.parse(this, App.Strings.parseDate);
}
// object prototype
Object.prototype.toJson = function() {
return JSON.stringify(this);
}
Object.prototype.using = function(func) {
return func.call(this, this);
}
http.handler.gs
function doPost(request) {
return new TasksNamespace.process(request);
}
calling convention
Just make arrays with the full function name and the rest are the function's arguments. It will return when everything is done, so it's like Promise.all()
var a = new TasksNamespace.all([
["App.Data.Firebase.Properties.getById",'T006DB4'],
["App.Data.External.CISC.Properties.getById",'T00A21F', true, 12],
["App.Maps.geoCode",'T022D62', false]
])
return preview
[ { functionName: 'App.Data.Firebase.Properties.getById',
arguments: [ 'T006DB4' ],
result:
{ Id: '',
Listings: [Object],
Pages: [Object],
TempId: 'T006DB4',
Workflow: [Object] } },
...
]
Notes
it can handle any static method, any method off a root object's tree, or any root (global) function.
it can handle 0 or more (any number) of arguments of any kind
it handles errors by returning the error from any post
// First create a trigger which will run after some time
ScriptApp.newTrigger("createAsyncJob").timeBased().after(6000).create();
/* The trigger will execute and first delete trigger itself using deleteTrigger method and trigger unique id. (Reason: There are limits on trigger which you can create therefore it safe bet to delete it.)
Then it will call the function which you want to execute.
*/
function createAsyncJob(e) {
deleteTrigger(e.triggerUid);
createJobsTrigger();
}
/* This function will get all trigger from project and search the specific trigger UID and delete it.
*/
function deleteTrigger(triggerUid) {
let triggers = ScriptApp.getProjectTriggers();
triggers.forEach(trigger => {
if (trigger.getUniqueId() == triggerUid) {
ScriptApp.deleteTrigger(trigger);
}
});
}
While this isn't quite an answer to your question, this could lead to an answer if implemented.
I have submitted a feature request to Google to modify the implementation of doGet() and doPost() to instead accept a completion block in the functions' parameters that we would call with our response object, allowing additional slow-running logic to be executed after the response has been "returned".
If you'd like this functionality, please star the issue here: https://issuetracker.google.com/issues/231411987?pli=1

Why does my Meteor.method return undefined? [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 6 years ago.
There is a simple field to input a youtube ID. I am using renaldo's youtube api (https://atmospherejs.com/renaldo/youtube-api) to retrieve the title of the youtube clip.
The client side event passes the track ID (var tid) value to the method 'addTrack' successfully - it outputs the track's title to the console on the server. I am having a really bad time returning anything at all back to the client.
If I call the 'getVideoData' method from the 'addTrack' method, it still returns 'undefined' every time. I am no expert in meteor or javascript, this is just something I am learning for the hell of it.
I understand the concept of callbacks and the synchronous nature of javascript (I think!).
Thanks.
[EDIT The code below solves my problem, using Future]
if (Meteor.isServer) {
YoutubeApi.authenticate({
type: 'key',
key: API_KEY
});
Meteor.methods({
addTrack: function(tid) {
Meteor.call("getVideoData", tid, function(err,res) {
console.log(res);
});
},
getVideoData: function(tid) {
var future = new Future();
YoutubeApi.videos.list({
part: "snippet",
id: tid,
}, function (err,data) {
var _data = {"title":data.items[0].snippet.title,"desc":data.items[0].snippet.description};
future["return"](_data)
});
return future.wait();
}
Meteor.startup(function () {
Future = Npm.require('fibers/future');
});
}
Meteor methods are tricky, as they use Fibers to be synchronous (well, they appear to the developer as synchronous). So you need to use Meteor.wrapAsync() to wrap the all to YoutubeApi. I haven't tested the following code, but it should look something like:
Meteor.methods({
getVideoData: function(tid) {
var syncYT = Meteor.wrapAsync(YoutubeApi.videos.list);
var data = syncYT({part: "snippet",id: tid,});
var transformed = {"title":data.items[0].snippet.title,"desc":data.items[0].snippet.description};
console.log(transformed.title);
return transformed.title;
}
});
You'll want to read more about error handling in this case, but this should get you going. Just remember that client-side Meteor is always asynchronous and server-side is optionally asynchronous. Use wrapAsync or Futures to handle async needs in methods.
Lastly, the Meteor guide is great, use it!

How to get a published collection's total count, regardless of a specified limit, on the client?

I'm using the meteor-paginated-subscription package in my app. On the server, my publication looks like this:
Meteor.publish("posts", function(limit) {
return Posts.find({}, {
limit: limit
});
});
And on the client:
this.subscriptionHandle = Meteor.subscribeWithPagination("posts", 10);
Template.post_list.events = {
'click #load_more': function(event, template) {
template.subscriptionHandle.loadNextPage();
}
};
This works well, but I'd like to hide the #load_more button if all the data is loaded on the client, using a helper like this:
Template.post_list.allPostsLoaded = function () {
allPostsLoaded = Posts.find().count() <= this.subscriptionHandle.loaded();
Session.set('allPostsLoaded', allPostsLoaded);
return allPostsLoaded;
};
The problem is that Posts.find().count() is returning the number of documents loaded on the client, not the number available on the server.
I've looked through the Telescope project, which also uses the meteor-paginated-subscription package, and I see code that does what I want to do:
allPostsLoaded: function(){
allPostsLoaded = this.fetch().length < this.loaded();
Session.set('allPostsLoaded', allPostsLoaded);
return allPostsLoaded;
}
But I'm not sure if it's actually working. Porting their code into mine does not work.
Finally, it does look like Mongo supports what I want to do. The docs say that, by default, cursor.count() ignores the effects of limit.
Seems like all the pieces are there, but I'm having trouble putting them together.
None of the answers do what you really want becase none provide solution that is reactive.
This package does exactly what you want and also reactive.
publish-counts
I think you can see the demo: counts-by-room in meteor doc
It can help you publish the counts of your posts at server and get it at client
You can simply write this:
// server: publish the current size of your post collection
Meteor.publish("counts-by-room", function () {
var self = this;
var count = 0;
var initializing = true;
var handle = Posts.find().observeChanges({
added: function (id) {
count++;
if (!initializing)
self.changed("counts", 'postCounts', {count: count});
},
removed: function (id) {
count--;
self.changed("counts", postCounts, {count: count});
}
});
initializing = false;
self.added("counts", 'postCounts', {count: count});
self.ready();
self.onStop(function () {
handle.stop();
});
});
// client: declare collection to hold count object
Counts = new Mongo.Collection("counts");
// client: subscribe to the count for posts
Tracker.autorun(function () {
Meteor.subscribe("postCounts");
});
// client: simply use findOne, you can get the count object
Counts.findOne()
The idea of sub.loaded() is to help you with exactly this problem.
Posts.count() isn't going to return the right thing because, as you've guessed, on the client, Meteor has no way of knowing the real number of posts that live on the server. But what the client knows is how many posts it's tried to load. That's what that .loaded() tells you, and is why the line this.fetch().length < this.loaded() will tell you if there are more posts on the server or not.
What I would do is write a Meteor server side method that retrieves the count like so:
Meteor.methods({
getPostsCount: function () {
return Posts.find().count();
}
});
Then call it on the client, in observe to make it reactive:
function updatePostCount() {
Meteor.call('getPostsCount', function (err, count) {
Session.set('postCount', count);
});
}
Posts.find().observe({
added: updatePostCount,
removed: updatePostCount
});
Although this question is old, I thought I would provide an answer that ended up working for me. I did not create the solution, I found the basis for it here (so credit where credit is due): Discover Meteor
Anyway, in my case I was trying to get "size" of the database from client side, so I can determine when to hide the "load more" -button. I was using template level subscriptions. Oh and for this solution to work, you need to add reactive-var -package. Here is my (in short):
/*on the server we define the method which returns
the number of posts in total in the database*/
if(Meteor.isServer){
Meteor.methods({
postsTotal: function() {
return PostsCollection.find().count();
}
});
}
/*In the client side we first create the reactive variable*/
if(Meteor.isClient){
Template.Posts.onCreated(function() {
var self = this;
self.totalPosts = new ReactiveVar();
});
/*then in my case, when the user clicks the load more -button,
we call the postsTotal-method and set the returned value as
the value of the totalPosts-reactive variable*/
Template.Posts.events({
'click .load-more': function (event, instance){
Meteor.call('postsTotal', function(error, result){
instance.totalPosts.set(result);
});
}
});
}
Hope this helps someone (I recommend checking the link first). For template level subscriptions, I used this as my guide Discover Meteor - template level subscriptions. This was my first stacked-post and I am just learning Meteor, so please have mercy...:D
Ouch this post is old, anyway maybe it will help someone.
I had exactly the same issue. I managed to solve it with 2 simple lines...
Remember the :
handle = Meteor.subscribeWithPagination('posts', 10);
Well I used in client handle.loaded() and Posts.find().count(). Because when they are different it means that all the posts are loaded. So here is my code :
"click #nextPosts":function(event){
event.preventDefault();
handle.loadNextPage();
if(handle.loaded()!=Posts.find().count()){
$("#nextPosts").fadeOut();
}
}
I had the same problem, and using the publish-counts package didn't work with the subs-manager package. I created a package that can set a reactive server-to-client session, and keep the document count in this session. You can find an example here:
https://github.com/auweb/server-session/#getting-document-count-on-the-client-before-limit-is-applied
I'm doing something like this:
On cliente
Template.postCount.posts = function() {
return Posts.find();
};
Then you create a template:
<template name="postCount">
{{posts.count}}
</template>
Then, whatever you want to show the counter: {{> postCount}}
Much easier than any solution i have seen.

Facebook API Javascript Not Defined

This is a fairly general problem I am having here, so I figured I would ask, hopefully someone can help me out.
I have a flex app using the facebook js bridge to log in, and I am trying to pass the users name back to the app, but I am coming across the thing that I will explain after the code:
function getFriends() {
var nameOfPlayer = "";
FB.api('/me', function(response) {
nameOfPlayer = response.name;
});
return nameOfPlayer;
}
If I have my flex app print nameOfPlayer, it returns undefined, same as if I do an alert with nameOfPlayer, but if I do an alert inside of the FB.api call for nameOfPlayer, then it will pop up with my name, but it still won't return name. I tried having it return a general string, and that works, so it is passing the information over, it just isn't saving to the variable. Any idea how to fix this? This is probably very convoluted, so if I need to clarify anything let me know. Thanks in advance for any help you can give me :)
You should be doing this in an event-driven way. Some pseudo-code is written below to get you started.
var yourFacebookAPIClass = new yourFacebookAPIClass();
yourFacebookAPIClass.addEventListener(Event.UserCallComplete, function(ev){
var nameOfPlayer = SomeParser.ParseData(ev);
someFunctionThatUsesYourVar(nameOfPlayer);
});
yourFacebookAPIClass.getNameOfPlayer();
In your facebook api class:
function getNameOfPlayer(){
makeTheApiCallHere();
}
The function you're defining inside of the FB.api call is actually a callback function which means it's called asynchronously. So the line of code after the api call (the return statement) will execute right away without waiting for Facebook to return a response. The callback function, on the other hand, will wait for Facebook to response, in fact that's what it's designed for. You'd be better off doing all your logic that depends on the nameOfPlayer inside of that callback function.
If you definitely have to return a nameOfPlayer variable the way it's written you'd need to tell your code to wait for nameOfPlayer to be defined:
function getFriends() {
var nameOfPlayer = "";
FB.api('/me', function(response) {
nameOfPlayer = response.name;
});
while(typeof nameOfPlayer === "undefined" || nameOfPlayer == "") {}
return nameOfPlayer;
}
But I really would not recommend this because your app will freeze while it waits for a response. You'd be better off doing everything inside of your callback function instead of returning something.
Good luck! :)

What is the best control flow module for node.js?

I've used caolan's async module which is very good, however tracking errors and the varying way of passing data through for control flow causes development to sometimes be very difficult.
I would like to know if there are any better options, or what is currently being used in production environments.
Thanks for reading.
I use async as well. To help tracking errors it's recommended you name your functions, instead of having loads of anonymous functions:
async.series([
function doSomething() {...},
function doSomethingElse() {...},
function finish() {...}
]);
This way you'll get more helpful information in stack traces.
...however tracking errors and the varying way of passing data through for control flow causes development to sometimes be very difficult.
I've recently created a simple abstraction named "wait.for" to call async functions in sync mode (based on Fibers): https://github.com/luciotato/waitfor
Using wait.for, you can use 'try/catch' while still calling async functions, and you keep function scope (no closures needed). Example:
function inAFiber(param){
try{
var data= wait.for(fs.readFile,'someFile'); //async function
var result = wait.for(doSomethingElse,data,param); //another async function
otherFunction(result);
}
catch(e) {
//here you catch if some of the "waited.for"
// async functions returned "err" in callback
// or if otherFunction throws
};
see the examples at https://github.com/luciotato/waitfor
Sometimes it is hard to put all the functions in an array. When you have an array of objects and want to do something for each object, I use something like the example below.
read more in: http://coppieters.blogspot.be/2013/03/iterator-for-async-nodejs-operations.html
var list = [1, 2, 3, 4, 5];
var sum = 0;
Application.each(list, function forEachNumber(done) {
sum += this;
// next statement most often called as callback in an async operation
// file, network or database stuff
done(); // pass an error if something went wrong and automatically end here
}, function whenDone(err) {
if (err)
console.log("error: " + err);
else
console.log("sum = " + sum);
});
I name the functions, because it is easier to debug (and easier to read)

Resources