I've got a URL containing a lot of params, and I need to extract just the domain name in order to perform a check on that domain name only. The app I'm coding in is a typescript application, it treats URL as a type instead of a string.
Originally I was writing code that was along these lines:
const hostnameExample = "http://www.foo.bar.com?search=www.google.com"
//example of URL I'm trying to get just a domain of
const cleanupParams2 = (url: string) => {
let domain = new URL(url);
return domain.hostname;
}
export const isExternalLink = (hostname?: string, goodDomains: string[] = []) => {
return goodDomains.every((domain) => {
if (hostname && !hostname.startsWith("#")) {
return cleanupParams2(hostname).indexOf(domain) < 0;
}
});
};
I'm getting an error message in my unit test scripts, I really don't even understand the error messages I'm getting or why I'm getting them to be honest.
expect(jest.fn()).not.toHaveBeenCalled()
Expected number of calls: 0
Received number of calls: 14
1: "Invalid URL: www.foo.bar.com·
What would be wrong with the source code that it would be throwing an error, and/or what in the world is that error? It's being thrown from basically any file that appears to directly or indirectly be using the isExternalLink function. I know I'm not showing all of the code here, but this is the problematic code basically.