Mike Olsen

  • ask me anything
  • rss
  • archive
  • 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);
    });
    
    • July 9, 2013 (12:00 am)
© 2013–2021 Mike Olsen