Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

128
Views
mocking document.domain and other objects in jest

I am trying to run tests in jest at the moment, at the moment I am getting an error that looks like this,

TypeError: Cannot read property 'indexOf' of undefined

      194 |     if (this.config.storage_host) {
      195 |       domain = this.config.storage_host;
    > 196 |     } else if (document.domain.indexOf("example") > 0) {
          |                                ^
      197 |       let domainParts: string[] = document.domain.split(".");
      198 |       domainParts.splice(0, 1);
      199 |       domain = domainParts.join(".");

The problem it would seem is that document.domain is undefined, how would I go about creating a document.domain for the tests? I assume I would mock it somehow, I have tried,

    Object.defineProperty(document, 'domain' {
        writable: true,
        value: 'https://www.example.com'
    });

but that did not make any change. I will also need to mock an object that is used throughout the application how would I go about that? The object is called ProvideState and has attributes like,

const ProvideState = {
    state: {
        app: 'client'
        context: '',
   }
}
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!