Dependency free wrapping function for browser natives to allow stubbing in unit tests.
Accessing natives through a thin wrapper makes stubbing possible, where it would otherwise be impossible.
You can use it with pretty much all globals defined on the
wrapple uses a couple of methods from ES5.1
If you need to support old browsers, you should ensure that these have been polyfilled.
wrapple can work in Node.js environments just like in browser environments.
Access browser natives through wrapple
// use whatever local name you likevar wrap = ;// ensure that wrapple has wrapped the property you're interested in// this is idempotent, call it many times with no ill effects;// directly use the returned globalvar hostname = hostname;
Now that your application code is using wrapped globals, you can target the wrapper function for stubbing, spying, etc.
var stub = sinon;// ...
Tidy up your tests
// using sinonstub;// or using wrapple.resetwrap;
// wrap - adds a wrapped property method to the wrapple api// returns a function that returns window.location// also creates wrap.location method as target for stubbing;// add and use immediatelyvar hostname = hostname;// use via dedicated method;// ...var hostname = wraplocationhostname;
// reset, removes all wrapper methods from wrapple apiwrap;
These globals on the
window object are not wrappable, as creating the returning methods on
wrapple would interfere with the
That shouldn't be too much of a problem, as they seem unlikely targets for stubbing.
In no way affiliated with, but admiring wrapple.jp