Skip to content

Instantly share code, notes, and snippets.

@commadelimited
Created December 30, 2013 15:30
Show Gist options
  • Save commadelimited/8183457 to your computer and use it in GitHub Desktop.
Save commadelimited/8183457 to your computer and use it in GitHub Desktop.

The file I want to test is set up as an IIFE, which works great in the browser. I'm able to use the method renderInteractions like so:

var something = utils.renderInteractions(doc);

However, when I run the test suite I get the following error:

TypeError: Object #<Object> has no method 'renderInteraction'

I found this Stack Overflow post which seems to indicate that your JavaScript file HAS to export something that Mocha can tap into: http://stackoverflow.com/questions/10204021/how-do-i-test-normal-non-node-specific-javascript-functions-with-mocha

But I'm not sure how to accomplish that.

var utils = (function() {
var publicMethods = {};
publicMethods.renderInteraction = function(doc) {
var interaction = {
id: doc._id,
account: doc.stream
};
if (doc.type === "tweet" && doc.data.retweeted_status && typeof doc.data.retweeted_status === "object") {
// Retweet
$.extend(interaction, {
"native_id": doc.data.id_str,
"user_id": doc.data.user.id_str,
"name": doc.data.user.name,
"screen_name": doc.data.user.screen_name
});
}
return interaction;
};
return publicMethods;
})();
var assert = require("assert"),
utils = require("../src/emmasocial/assets/js/es/src/application.utils");
describe('Interactions', function() {
describe('render retweet', function() {
it('should not emit anything when doc is empty', function(done) {
var document = {},
rendered = utils.renderInteraction(document);
assert.equal(1, 1);
done();
});
});
});
@commadelimited
Copy link
Author

Thanks Cory, that seems to cause it to fail in the browser though. This fork by @jcreamer898 does the trick:

https://gist.github.com/jcreamer898/8183519

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment