I am trying to create a mochajs test to detect the script.onload event has been executed or the script.onerror. I have setup tests to detect the script exists in the DOM but am not sure how to check the actual loading.
describe("Load external ABC Library", function() {
var h = document.getElementsByTagName('head')[0];
var s = document.createElement('script');
s.src="http://host.com/script.js";
s.id="abc";
s.async=false;
h.appendChild(s);
var l = document.getElementById('abc');
it.skip("is in the DOM", function () {
expect(l).to.not.equal(null);
console.log('is in the DOM was skipped');
});
it("is a child of the head", function () {
expect(l.parentElement).to.equal(document.head);
});
});
One way to do this uses a virtual DOM via
mocha-jsdom
.Example in CoffeeScript using
mocha
,chai
,sinon
, andsinon-chai
:addDynamicScript.coffee:
addDynamicScript.spec.coffee: