4qetmq
Last Updated: February 25, 2016
·
2.668K
· artfulhacker
10425126 10103995008712411 6222461894198440691 n

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);
});
Say Thanks
Respond