QUnit unit testing in js, window.location mocks
So recently I need to unit test some js code but this code used the window.location.host variable, and in order to fully test the logic I had to mock the window.location global var.
In case anyone ever has to do something similar here is a snippet that works with QUnit:
test( "test initial mocks", function()
{
custom_window =
{
location:
{
host: "test.asu.edu"
}
};
(function(window)
{
equal( window.location.host, "test.asu.edu", "we expect test.asu.edu to be in the window.host mock" );
})(custom_window);
});
Written by Mike Olsen
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Js
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#