Last Updated: February 25, 2016
·
4.81K
· artfulhacker

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);
});