Skip to content

Instantly share code, notes, and snippets.

@callumlocke
Last active December 19, 2015 00:09
Show Gist options
  • Save callumlocke/5867186 to your computer and use it in GitHub Desktop.
Save callumlocke/5867186 to your computer and use it in GitHub Desktop.
Mocha Spec Runner Spec

Mocha Spec Runner Spec

This is a spec to verify that your Mocha Spec Runner page can actually load all its script tags.

Paste it into your Spec Runner page as an inline script, just before mocha.run();.

Why?

If one of your source scripts fails to load (e.g. due to a malformed URL), you usually don't get any error message. This can cause confusing test failures that are hard to debug.

How it works

It runs through all external script tags in the page, and attempts to reload them over AJAX. If any of them fails to load, this spec will fail (with a message specifying the URL that it couldn't load).

Selectively allowing some script tags to fail

If you have a script tag that you don't mind failing to load in some cases (e.g. /testem.js, when you are serving your spec runner through something other than Testem's server, such as during grunt test in a Yeoman webapp project)... just add a data-optional attribute:

<script src="/testem.js" data-optional></script>
describe('Mocha Spec Runner', function () {
it('should be able to load all script tags', function (done) {
$(function () {
var scriptRequests = [];
$('script[src]:not([data-optional])').each(function () {
var src = this.src;
scriptRequests.push($.ajax({
url: src,
dataType: 'text',
error: function () {
done(new Error('Failed to load script: ' + src));
}
}));
});
$.when.apply($, scriptRequests).done(function() {
done();
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment