rstacruz / jsdom-global

Enable DOM in Node.js
477 stars 38 forks source link

Solution to conditionally including jsdom-global in node environment #35

Closed ctaylo21 closed 6 years ago

ctaylo21 commented 6 years ago

I have a situation where I am developing web modules with es6/mocha/chai/sinon/jsdom. I have some basic TDD-style mocha-watch commands set up to re-run tests in the terminal. The problem is, I also want to be able to run tests in the browser. I have an HTML file setup to do so, but I'm not sure how to handle the jsdom-global piece.

I would like to not have jsdom run in browser (to test actual browser functionality). Is there a recommended way to do this? I was thinking of some sort of setup script that would conditionally include jsdom if it detected it wasn't in a browser. Open to any ideas/recommendations, even if you say it's a terrible idea to try to do that.

ctaylo21 commented 6 years ago

So I've found the option to add -r jsdom-global/register to my mocha.opts file. This works out great because I have separate commands for running tests via terminal and building the test file for web via webpack. Now my main issue is how I would do dom cleanup. If I include that jsdom-global/register, I don't know how I can clean up the dom between tests in mocha's after() or afterEach().

Is the only way to do that to include it inside the tests like the following?

before(function () {
  this.jsdom = require('jsdom-global')()
})

after(function () {
  this.jsdom()
})
junejie commented 6 years ago

@ctaylo21 have you found a way?

ctaylo21 commented 6 years ago

@junejie No I moved away from jsdom in favor of puppeteer due to some limitations with jsdom.