Local scripts won't run at all on Mac
See original GitHub issueI’m using the following code on both a Windows machine and a Macbook in Node.js environment (with no module loaders of any kind):
var jsdom = require('jsdom');
module.exports = function(url, inject, injectPath) {
var jsdomConfig = {};
jsdomConfig.url = url;
jsdomConfig.scripts = [`${__root}lib/jquery.js`];
if ( inject ) jsdomConfig.scripts.push(injectPath);
jsdomConfig.done = function(error, window) {
// Expecting all the scripts to be executed
console.log(window.$ === undefined); // Prints true
};
jsdom.env(jsdomConfig);
};
_root
resolves to the project path (example: C:/myproject/
).
The code runs perfectly on my Windows machine and the console.log prints false
. However, on my Macbook it prints true
.
I’ve tried all the suggested fixes (waiting for window
to finish loading, etc.) but as long as the scripts are local, they never run on my Macbook.
I tried using a URL to load jQuery and it works! When I change the 5th line to jsdomConfig.scripts = ['https://code.jquery.com/jquery-3.1.1.min.js'];
the console.log prints false
in both my Macbook and the Windows machine. So I came up with a workaround (since the code is part of an Express.js webservice) and added a route to serve my local files using res.download()
method. So instead of adding the local path of jQuery, I used something like http://localhost:8000/jquery
and got it loaded when done()
is called. Used the same method for other scripts passed to the function and they all work too.
But this adds unnecessary time to the whole process and I assume the downloaded file should be saved temporarily somewhere, so it occupies unnecessary space.
window
will be a fully-loaded window, with all external resources downloaded and<script>
s executed.
…quoting from the documentation. Clearly that is not happening!
Any ideas why local scripts are not executed when done()
is called as opposed to non-local scripts and why is it Mac specific?
Issue Analytics
- State:
- Created 7 years ago
- Comments:9 (5 by maintainers)
Top GitHub Comments
Be sure to try the new v10 API documented in the readme, which should make things much clearer! There you can inject script tags the normal way, using
document.createElement("script")
for example.That might actually be a bug. It seems like we’re assuming that the protocol in that URL is “C:” and the rest is a data-segment and then at a lower level we just check if a file on that path exists (which will return false for all http:// URLs). Haven’t checked if that’s actually the case though.
Yes, it should.