How can I mock dependencies for unit testing in RequireJS?

So after reading this post I came up with a solution that use the requirejs config function to create a new context for your test where you can simply mock your dependencies: var cnt = 0; function createContext(stubs) { cnt++; var map = {}; var i18n = stubs.i18n; stubs.i18n = { load: sinon.spy(function(name, req, onLoad) … Read more

Why use Object.prototype.hasOwnProperty.call(myObj, prop) instead of myObj.hasOwnProperty(prop)?

Is there any practical difference [between my examples]? The user may have a JavaScript object created with Object.create(null), which will have a null [[Prototype]] chain, and therefore won’t have hasOwnProperty() available on it. Using your second form would fail to work for this reason. It’s also a safer reference to Object.prototype.hasOwnProperty() (and also shorter). You … Read more

What’s the difference between Require.js and simply creating a element in the DOM? [closed]

What advantages does Require.JS offer in comparison to simply creating a element in the DOM? In your example, you’re creating the script tag asynchronously, which means your needMe() function would be invoked before the need_me.js file finishes loading. This results in uncaught exceptions where your function is not defined. Instead, to make what you’re suggesting … Read more

What is ‘define’ used for in JavaScript (aside from the obvious)?

I can’t say for sure without seeing the entire script, but it’s likely to be the define function from RequireJS, in particular the “define with dependencies” form of that function. It is used to define a “module”: A module is different from a traditional script file in that it defines a well-scoped object that avoids … Read more

Loading Backbone and Underscore using RequireJS

RequireJS 2.X now organically addresses non-AMD modules such as Backbone & Underscore much better, using the new shim configuration. The shim configuration is simple to use: (1) one states the dependencies (deps), if any, (which may be from the paths configuration, or may be valid paths themselves). (2) (optionally) specify the global variable name from … Read more

Excluding files/directories from Gulp task

Quick answer On src, you can always specify files to ignore using “!”. Example (you want to exclude all *.min.js files on your js folder and subfolder: gulp.src([‘js/**/*.js’, ‘!js/**/*.min.js’]) You can do it as well for individual files. Expanded answer: Extracted from gulp documentation: gulp.src(globs[, options]) Emits files matching provided glob or an array of … Read more

Prevent RequireJS from Caching Required Scripts

RequireJS can be configured to append a value to each of the script urls for cache busting. From the RequireJS documentation (http://requirejs.org/docs/api.html#config): urlArgs: Extra query string arguments appended to URLs that RequireJS uses to fetch resources. Most useful to cache bust when the browser or server is not configured correctly. Example, appending “v2” to all … Read more