Can't define 'window' object after Browserifying my test file. Is that expected?
Closed this issue · 3 comments
Hello. I'm a little unclear on whether this package should work with Browserify or not. The README seems to indicate 'yes,' but my experience indicates 'no'. Maybe I'm doing something wrong :-/
In short, in a unit test, I can successfully use require('jsdom-global')()
to define the window
object. But if I then Browserify/Babelify the test file, then run execute the unit test, the window
object is no longer defined. Is that expected?
Here is a more detailed description.
- I created a file
test\tape.test.update.js
. It simply requiresjsdom-global
, defines a simple test (using Tape), and prints the value of thewindow
object:
require('jsdom-global')()
let tapeTest = require('tape')
tapeTest('Test update functions.', function(t) {
console.log(window)
t.end()
})
-
From the Node command line, if I execute
node test/tape.test.update.js
, thewindow
object is defined, as desired/expected. -
From the command line, I then Browserified (with a Babelify transform) the file, like so:
node_modules/.bin/browserify test/tape.test.update.js -t [ babelify ] --outfile test/test-bundle.js
- From the command line I then executed
node test/test-bundle.js
- It returns a ReferenceError:
ReferenceError: window is not defined
Is this expected? If so, is there a way to use jsdom-global in my case?
The README has a section about Browserify. The first sentence reads,
If you use Browserify on your tests (eg: smokestack, tape-run, budo, hihat, zuul, and so on), doing require('jsdom-global')() is a noop.
To me, this says that I should not expect the window
object to be defined in my case.
But the next sentence reads,
In practice, this means you can use jsdom-global even if your tests are powered by browserify, and your test will now work in both the browser and Node.
To me, this says that I should expect the window
object to be defined in my case.
Sorry to be a pain here--I know I'm getting into semantics a bit :-/ Just wondering if I'm doing something wrong, or if I should look for another solution.
Version Numbers
- jsdom: 16.5.2
- jsdom-global: 3.0.2
- browserify: 16.5.1
- babelify: 10.0.0
- tape: 5.0.1
Hey mate, sorry I don't have time to maintain this package anymore, but I might be able to help answer your question.
From the command line I then executed
node test/test-bundle.jsIt returns a ReferenceError:
ReferenceError: window is not defined
Running test-bundle.js with Node.js will indeed cause this error. Browserify bundles are assumed to run on the browser; if you were to make an HTML file that loads test-bundle.js and use that in the browser, it should work as expected where window
would be available.
If you do intend for test-bundle.js to be run in Node.js, maybe browserify's --no-browser-field
option would help.
OK thanks for your time. I couldn't get Browserify's --no-browser-field
option to work. But I think in this case, I'll go a different route entirely, so I won't need to use jsdom with Browserify. I'll close this issue--thanks!
Awesome that's great! There are better options today than browserify so I think you're on a good path there.