Launching a mocha test, but Error getting: ReferenceError: document not defined

I am using a mocha testing environment to test the Http rest-api. I want to create a test report for all test cases, but when I ran mocha --reporter html > report.html
Getting the following error

 /usr/local/lib/node_modules/mocha/lib/reporters/html.js:263 var div = document.createElement('div'); ^ ReferenceError: document is not defined at fragment (/usr/local/lib/node_modules/mocha/lib/reporters/html.js:263:13) at new HTML (/usr/local/lib/node_modules/mocha/lib/reporters/html.js:53:14) at Mocha.run (/usr/local/lib/node_modules/mocha/lib/mocha.js:459:18) at Object.<anonymous> (/usr/local/lib/node_modules/mocha/bin/_mocha:393:18) at Module._compile (module.js:456:26) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Function.Module.runMain (module.js:497:10) at startup (node.js:119:16) at node.js:902:3 
+5
source share
4 answers

A reporter named html should only be used when launching Mocha in a browser. Running Mocha from the command line will not work with it.

There is a reporter called doc that outputs the “html documentation” according to mocha --reporters . By the way, when you run mocha --reporters , you will not see the html specified in the reporter, for the reason I gave above.

+6
source

To avoid this problem, you should use a browser without a browser. Try mocha-phantomjs .

0
source

I was able to solve a simpler version of this problem by conditionally defining document :

 let document = (typeof document === "undefined") ? {} : document; 

I expect you can use the same approach, but you need to set document to something that mocks createElement .

0
source

if you don't need to test it in a browser, you can use jsdom-global :

 mocha -r jsdom-global/register 
0
source

Source: https://habr.com/ru/post/1232340/


All Articles