0

When I try to run this code in jasmine.js test

document.createElement(tag);

I get this error

ReferenceError: document is not defined

I understand, the document object is not available in jasmine.js tests, but how can I dynamically create a new HTML element?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
mspiderv
  • 509
  • 7
  • 15

2 Answers2

0

Use jsdom

var jsdom = require('jsdom').jsdom;
var element = jsdom('<div></div>').querySelector('div');
mspiderv
  • 509
  • 7
  • 15
-1

Document object is not part of JavaScript, it's browser's feature. Check out this solution

Community
  • 1
  • 1
Reski
  • 169
  • 7
  • Please do not just answer with a link to another answer. If the question is the same, flag as a duplicate. If the questions are different, but he answer is similar, copy the content of the answer here, **with changes specific to this question**, and **make sure to attribute the original author**. – Heretic Monkey May 17 '17 at 15:53