this refers to the body element, which does not have an "href" attribute. So, your call to .attr() returns undefined which is converted to the string "undefined".
If you just want to add "#test" to the url, you don't need to specify a complete URI. Instead you can just specify the relative url, in this case "#test":
location.href = "#test";
If you are trying to append "#test" to a link in your document, you'll need to bind only to links with hrefs:
jQuery("a[href]").click(function(e) {
e.preventDefault();
location.href = jQuery(this).attr('href') + '#test';
});
A couple other notes. You don't need to use jQuery to get the href. It would be simpler and more readable (IMHO) to just do:
location.href = this.href + "#test";
And, rather than preventing default and using javascript to do the navigation, you can just modify the href of the link:
jQuery("a[href]").click(function(e) {
this.href += "#test";
});