Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8389159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:38:58+00:00 2026-06-09T18:38:58+00:00

Let’s say I have a utility function that, for simplicity’s sake (the real thing

  • 0

Let’s say I have a utility function that, for simplicity’s sake (the real thing is complicated and irrelevant), returns the current window’s querystring.

var someUtilityFunction = () {
    return window.location.search.substring(1);
};

Now I want to unit test this function in qUnit (not sure if the testing harness is relevant or not):

test('#1 someUtilityFunction works', function () {
    // setup
    var oldQS = window.location.search;
    window.location.search = '?key1=value1&key2=value2&key3=value3';

    var expectedOutput = 'key1=value1&key2=value2&key3=value3';

    // test
    equals(someUtilityFunction(),
        expectedOutput,
        'someUtilityFunction works as expected.');

    // teardown
    window.location.search = oldQS;
});

The problem here is that setting the window.location.search to a different querystring is causing the page to reload, essentially entering an infinite request loop. Is there any way to mock out the window.location object without making any changes to the someUtilityFunction function?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T18:39:00+00:00Added an answer on June 9, 2026 at 6:39 pm

    We run into the same problem a few days ago. Mainly there are 2 approaches:

    Rewrite your code

    This might not be the best (if any) solution, but consider passing the window object to your function to make mocking easier. Even better, use a closure and encapsulate your code. This has a few more advantages:

    • You can shadow global vars
    • You can use private local vars
    • You can avoid naming collisions
    • The shadowing makes mocking really easy, just pass in something else

    Wrap your code

    You can wrap all your code inside a function which mocks the window object into a local variable. You have basically two possibilities there as well:

    Suppose this is the mock:

    var customWindow = {
        location: {
            search: "",
            hash: ""
        }
    };
    

    Use a closure

    var someUtilityFunction;
    
    (function(window) {
        // window is now shadowed by your local variable
        someUtilityFunction = () {
            return window.location.search.substring(1);
        };
    })(customWindow);
    

    This shadows the global window with a local window.

    Use the with statement

    Although I am usually strongly against with, it could really solve a lot of problems here. Since it basically remaps your scope, you can very easily mock your environment.

    // first some more preparation for our mock
    customWindow.window = customWindow;
    
    with(customWindow) {
    
        // this still creates the var in the global scope
        var someUtilityFunction = () {
            // window is a property of customWindow
            return window.location.search.substring(1);
        };
    
        // since customWindow is our scope now
        // this will work also
        someUtilityFunction = () {
            // location is a property of customWindow too
            return location.search.substring(1);
        };
    
    }
    

    By the way: I don’t know if the search property suffers from the same symptoms as the hash property – namely sometimes including the question mark and sometimes not. But you might want to consider using

    window.location.search.replace(/^\?/, "");
    

    instead of

    window.location.substr(1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let me explain best with an example. Say you have node class that can
Let's say that I have a SQLite database that I create in a separate
Let's say I have multiple requirements for a password. The first is that the
Let's say that I have a date in R and it's formatted as follows.
Let's say you have a method that expects a numerical value as an argument.
Let's say I have a bunch of links that share a click event: <a
Let's say that I have a set of relations that looks like this: relations
Let's say I have the following function in C#: void ProcessResults() { using (FormProgress
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Let's say that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.