Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTML reporter used on CLI gets ReferenceError: document is not defined #1906

Closed
neelabhsingh opened this issue Sep 26, 2015 · 8 comments · Fixed by Urigo/tortilla#64
Closed
Labels
area: documentation anything involving docs or mochajs.org area: reporters involving a specific reporter faq a frequently asked question or common misconception status: accepting prs Mocha can use your help with this one!

Comments

@neelabhsingh
Copy link

I am using mocha testing framework to test Http rest-api. I want to generate test-report for all test case, but when I ran mocha --reporter html > report.html
Getting 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. (/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:

@tdrach
Copy link

tdrach commented Oct 14, 2015

This sounds like the same issue as #1816

@miparnisari
Copy link

I'm getting the same error with gulp-mocha 2.2.0 and mocha 2.1.0.

My gulpfile:

var gulp = require('gulp');
var mocha = require('gulp-mocha');
gulp.task('tests', function() {
  return gulp.src('./test/**/*.js', {read: false})
  .pipe(mocha({reporter: 'html', timeout: 2000}));
});

The error:

vagrant@vagrant-ubuntu-trusty-64:/home/app/app-api$ gulp tests
[14:48:47] Warning: gulp version mismatch:
[14:48:47] Global gulp is 3.9.1
[14:48:47] Local gulp is 3.9.0
[14:48:52] Using gulpfile /home/app/app-api/gulpfile.js
[14:48:52] Starting 'tests'...
starting api...
finished loading api...
reading config: development
[14:48:58] 'tests' errored after 6.41 s
[14:48:58] ReferenceError in plugin 'gulp-mocha'
Message:
    document is not defined
Stack:
ReferenceError: document is not defined
    at fragment (/home/app/app-api/node_modules/mocha/lib/reporters/html.js:228:13)
    at new HTML (/home/app/app-api/node_modules/mocha/lib/reporters/html.js:50:14)
    at Mocha.run (/home/app/app-api/node_modules/mocha/lib/mocha.js:427:18)
    at Domain.<anonymous> (/home/app/app-api/node_modules/gulp-mocha/index.js:56:20)
    at Domain.run (domain.js:228:14)
    at Stream.<anonymous> (/home/app/app-api/node_modules/gulp-mocha/index.js:54:5)
    at _end (/home/app/app-api/node_modules/through/index.js:65:9)
    at Stream.stream.end (/home/app/app-api/node_modules/through/index.js:74:5)
    at DestroyableTransform.onend (/home/app/app-api/node_modules/gulp/node_modules/vinyl-fs/node_modules/through2/node_modules/readable-stream/lib/_stream_readable.js:523:10)
    at DestroyableTransform.g (events.js:260:16)
    at emitNone (events.js:72:20)
    at DestroyableTransform.emit (events.js:166:7)
    at /home/app/app-api/node_modules/gulp/node_modules/vinyl-fs/node_modules/through2/node_modules/readable-stream/lib/_stream_readable.js:965:16
    at nextTickCallbackWith0Args (node.js:420:9)
    at process._tickDomainCallback (node.js:390:13)

@sugh01
Copy link

sugh01 commented Jul 14, 2016

hi any update on this issue is it fixed yet. I am getting this issue when working with abao and using --reporter as html

@mariohmol
Copy link

I'm getting this as well, when testing a angular2 app in a meteor app

ReferenceError: document is not defined
    at node_modules/meteor-client-side/dist/meteor-client-side.bundle.min.js:1:28719
    at node_modules/meteor-client-side/dist/meteor-client-side.bundle.min.js:1:29448
    at Object.<anonymous> (node_modules/meteor-client-side/dist/meteor-client-side.bundle.min.js:2:1609)

@drazisil drazisil added type: bug a defect, confirmed by a maintainer unconfirmed labels Mar 30, 2017
@ScottFreeCode ScottFreeCode added faq a frequently asked question or common misconception area: documentation anything involving docs or mochajs.org area: reporters involving a specific reporter area: usability concerning user experience or interface and removed type: bug a defect, confirmed by a maintainer unconfirmed labels Jun 14, 2017
@ScottFreeCode
Copy link
Contributor

The HTML reporter is not currently intended to be used on the CLI, but rather is Mocha's browser reporter.

We need to improve the documentation on the site concerning this, and/or remove this reporter's availability from the CLI (it isn't immediately obvious why html is considered a valid CLI reporter when it's not printed by mocha --reporters anyway...), unless and until someone can come up with a way to make it useful as a CLI reporter as well as an in-browser one.

Users who want HTML-formatted output but not necessarily the same interface as the browser usage may also be interested in the doc reporter.

@ScottFreeCode ScottFreeCode changed the title Running mocha test but Getting error: ReferenceError: document is not defined HTML reporter used on CLI gets ReferenceError: document is not defined Jun 14, 2017
@boneskull boneskull added status: accepting prs Mocha can use your help with this one! and removed area: usability concerning user experience or interface labels Oct 17, 2017
sgilroy pushed a commit to TwineHealth/mocha that referenced this issue Feb 27, 2019
@kunjarani
Copy link

Hi,
Any one got a solution for this error. I am facing the same issue. I am using gulp-spawn-mocha-nyc .

@AWolf81
Copy link

AWolf81 commented Mar 6, 2020

@kunjarani You could use JSDOM so it can run the html reporter from CLI with-out a failure about a missing document. I've created a basic script to start Mocha.js with that reporter here.

It's creating two globals (document & window) and also a mock of getContext for the canvas. Probably the mock will remove Canvas specific stuff but I needed that to have everything running.

Not perfect because just the html structure of the main file is rendered. So no working links and no JavaScript or CSS in the generated html file.

Maybe I'll check later if it's possible to improve these points.

@kunjarani
Copy link

@AWolf81 Tq for the response.

The issue got resolved in another way. Actually gulp-spawn-mocha-nyc is internally using mocha to execute the unit tests and nyc to create a code coverage report.

var gulp = require('gulp');
var mochaNyc = require('gulp-spawn-mocha-nyc');

gulp.task('tests', function() {
return gulp.src('./test/**/*.js', {read: false})
.pipe(mochaNyc({reporter: 'html'}));
});

here we are sending params reporter:'html' which internally accepted by Mocha and as mocha is not able to create browser dependent report hence throwing the error.

gulp.task('tests', () =>{
return gulp.src('test/unit//mocha-tests/.test.js')
.pipe(mochaNyc({ "nyc":{reporter:'html',
"all": true,
"include": [ 'lib/
/
.js' ]} } ) );

});

Try this out to execute unit tests and generate a code coverage report in html format using
gulp-spawn-mocha-nyc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: documentation anything involving docs or mochajs.org area: reporters involving a specific reporter faq a frequently asked question or common misconception status: accepting prs Mocha can use your help with this one!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants