Created
June 30, 2017 17:38
-
-
Save senica/cf5cec2ce54f18025d43d63e26acf50c to your computer and use it in GitHub Desktop.
JSDOM simplification for loading scripts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* If you are use to pre-version 10 jsdom, post 10 is an absolute nuisance for loading scripts...especially when you just | |
* want to test some code. | |
* This just creates a nice little wrapper to simplify everythign. | |
**/ | |
const __jsdom = require("jsdom"); | |
const { JSDOM } = __jsdom; | |
global.jsdom = function(html = '', scripts = []){ | |
return new Promise((resolve, reject)=>{ | |
try{ | |
let _scripts = []; | |
for(let script of scripts){ | |
_scripts.push(`<script src="${script}"></script>`) | |
} | |
let dom = new JSDOM(`<body> | |
${html} | |
${_scripts.join('\n')} | |
</body>`, { | |
resources: 'usable', | |
runScripts: 'dangerously' | |
}); | |
dom.window.addEventListener('load', ()=>{ | |
resolve(dom); | |
}) | |
}catch(e){ | |
reject(e); | |
} | |
}); | |
} |
Author
senica
commented
Jun 30, 2017
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment