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

218
Views
Jest - how to mock a nested function like document.head.querySelector?

I've a laravel vue setup, how can I mock document.head.querySelector with jest. I've to import a file in jest-setup.js in order to mock other functions and libraries but it also have this code which needs a spy function on document.head.querySelector:

let token = document.head.querySelector('meta[name="csrf-token"]');
if (token) {
    axios.headers.common['X-CSRF-TOKEN'] = token.content;
} else {
    console.error('CSRF token not found');
}

I want to mock document.head.querySelector to something like

jest.spyOn(document, 'querySelector').mockImplementation((selector) => {
    switch (selector) {
      case '.meta[name="csrf-token"]':
        return 'test-token';
    }
  });

but this doesn't works as I'm not mocking the head.querySelector which I don't know how to.

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!