According to the JSDom README, you can pass an array of Javascript Strings to execute, but it seems like this feature isn't working for me.
This is my node app:
var fs = require('fs');
var jsdom = require('jsdom-no-contextify');
var scripts = ['manipulate-dom.js'];
var src = scripts.map(function(filename) {
return fs.readFileSync(filename, 'utf8');
});
console.log(src[0]);
jsdom.env({
html: '<div>Hi</div>',
src: src,
done: function(errors, window) {
console.log(window.document.querySelector('div').textContent);
window.doStuff();
console.log(window.document.querySelector('a').textContent);
}
});
and this is my other file:
console.log('Running JS');
function doStuff() {
var a = document.createElement('a');
a.textContent = 'TEXT!';
window.document.body.appendChild(a);
}
window.doStuff = doStuff;
The output is as follows:
console.log('Running JS');
function doStuff() {
var a = document.createElement('a');
a.textContent = 'TEXT!';
window.document.appendChild(a);
}
window.doStuff = doStuff;
Hi
...
window.doStuff();
^
TypeError: Object #<Window> has no method 'doStuff'
It doesn't even print "Running JS". What am I doing wrong?