Setting up baseurl in jsdom with Jest to test React component giving error

Viewed 193

I am try to run a unit test on React component using Jest and jsdom. The component has document.getElementsByTagName("base")[0].getAttribute("href"). The test fails with "TypeError: Cannot read property 'getAttribute' of undefined". Jsdom setup code is below"

  const dom = (new JSDOM(`<html><head><base href="/en-us/dashboard/analytics"></head></html>`));
  console.log(dom.window.document.getElementsByTagName("base")[0]) //returns HTMLBaseElement {}
  global.window = dom.window;
  global.document = dom.window.document;
  console.log(global.document.getElementsByTagName("base")[0])  //undefined
0 Answers
Related