rstacruz/jsdom-global

Solution to conditionally including jsdom-global in node environment

ctaylo21 opened this issue · 3 comments

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.

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()
})

@ctaylo21 have you found a way?

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