HtmlUnit has a good support for JavaScript which gets continuously improved, and test cases for complex libraries (Dojo, jQuery, Prototype, Google Web Toolkit, Yahoo User Interface and Sarissa) are already included. However, not all test cases succeed, and you would probably face an issue in real-life example.

Before reporting an issue with JavaScript, it is very preferred if you isolate the root cause of a single JavaScript property or method, but just saying 'jQuery' or a given web site is not working, doesn't help much.
To find the offending line, you can put alert() at various places, compare the result of HtmlUnit to a real browser (Chrome, Firefox or Internet Explorer), and you will mostly be able to provide a tiny failing test case which contains few lines of JavaScript (independent of the whole JavaScript library), please have a look at some sample test cases.
Once the cause is identified, it is very likely to be fixed in a timely manner.

If you need help in tracing, please don't hesitate to ask the team.

Suggested steps

The first step is to have a way to compare HtmlUnit behavior against real browser, and this can be done by installing a proxy that modifies the incoming content (so debugging statements can be put). There are many proxy servers that can be used, one suggested option is Charles.

Ensure you can correctly modify the content, and that the real browser does not cache the response.

function someFunction() {
    alert('I am called!!');
    anotherFunction();
}

Ensure you are using HtmlUnit with the same simulated BrowserVersion as the real browser.

You can encounter an error similar to:

net.sourceforge.htmlunit.corejs.javascript.EcmaError: TypeError: Cannot call method "getAttribute" of undefined
(http://download.dojotoolkit.org/release-1.4.0/dojo-release-1.4.0/dojo/dojo.js#16(eval)#16)

which means that in some_variable.getAttribute(), some_variable was evaluated to 'undefined' during execution of that line.

Once that line is found, you can start navigating the hierarchy of the caller functions to see where exactly the discrepancy is.

To quickly modify the content, you can save the file locally and tell the proxy to replace the content of a specific URL with that file. This is called MAP LOCAL in Charles.
You can also beautify the JavaScript, look for online tools.

Use the latest released version at least, if not the latest snapshot.

JavaScript hints

To know a variable value, you can simply use alert, and check the value in HtmlUnit and compare it with real browser.

function myfunction(a, b) {
    var c = a + b;
    alert(c);
    return c;
}

To know the caller function, you can use:

function myfunction(a, b) {
    var c = a + b;
    alert(arguments.callee.caller);
    return c;
}
which will print the content of the caller function.

To know what function will be called, you can use:

function myfunction(a, b) {
    var c = a + b;
    alert(anotherFunction);
    return anotherFunction(c + 'something');
}

If a function is called too many times, you can put conditional alert

function myfunction(a, b) {
    var c = a + b;
    if (a == 'properties') {
        alert(c);
    }
    return anotherFunction(c + 'something');
}
or use a global variable:
var debug = false;

function main() {
  debug = true; // to debug only during the next method execution
  myFunction(a, b);
  debug = false; // to stop debugging
}

function myfunction(a, b) {
    var c = a + b;
    if (debug) {
        alert(anotherFunction);
    }
    return anotherFunction(c + 'something');
}}