SVG element has type HTMLUnknownElement when created inside jest test

时间:2017-04-10 01:16:46

标签: javascript svg jestjs

I'm trying to create a svg element to use for a test that accepts it as a parameter, but it keeps failing because the svg element does not seem to be getting created properly. I am using jest 19.0.2. Here is what I have:

describe('myFunc', () => {
  let div;
  let svg;

  beforeEach(() => {
    svg = document.createElementNS('http://www.w3.org/2000/svg',"svg");
    div = document.createElement('div');
    div.setAttribute('id', 'div-id');
    document.body.appendChild(div);
    document.getElementById("div-id").appendChild(svg);
    console.log(svg);
  });

  test('some test', () => {
    myFunc(svg);
    // some expectation
  });
});

The console.log above outputs HTMLUnknownElement {}. The strange thing is this exact code actually does work if used outside a test. For example, if I look in the browser console when running the code on a local server I see the log statement successfully print the svg element (<svg></svg>). I cannot figure out why this does not work in my jest test.

0 个答案:

没有答案