Software Alternatives, Accelerators & Startups

How to mock `window` and `window.location.href` when unit testing a function that relies on `window` and `window.location.href`?

jsdom
  1. 1
    A JavaScript implementation of various web standards, for use with Node.js - jsdom/jsdom
    Pricing:
    • Open Source

    #Browser Testing #Automated Testing #Development 29 social mentions

  2. This domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission.
    Export const useLanguageInternal = (window: { location: { href: string } }) => (siteUrl: string): string => { const currentUrl = typeof window !== "undefined" ? window.location.href : siteUrl const regex = new RegExp(`^https://example.com/es/.*$`, "gi") const httpsCurrentUrl = currentUrl.replace(/^http:\/\//, "https://") return regex.test(httpsCurrentUrl) ? "es" : "en" } Export const useLanguage = useLanguageInternal(typeof window !== "undefined" ? Window : undefined).

    #Software Development #Software Development Tools #Web Development Tools 2424 social mentions

Discuss: How to mock `window` and `window.location.href` when unit testing a function that relies on `window` and `window.location.href`?

Log in or Post with