Skip to content

Instantly share code, notes, and snippets.

@patkujawa-wf
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patkujawa-wf/9fd4a8ff7f42a00e908a to your computer and use it in GitHub Desktop.
Save patkujawa-wf/9fd4a8ff7f42a00e908a to your computer and use it in GitHub Desktop.
In-browser jasmine2 testing with requirejs
// You can paste this code into a browser console and it should work
// (provided preconditions below are met).
// Preconditions:
// 1. The page you load uses requirejs (so that window.require is defined).
// 2. A local server is running inside a folder with the correct dep's.
// a. `mkdir bower_components && bower install jasmine`
// 3. Finally, run `python -m SimpleHTTPServer` to serve the directory
// Set up loading of the necessary libs from the local server using requirejs
// (assumed to be on the page).
// http://stackoverflow.com/questions/23365972/require-js-lazy-loading-remote-url
var localRequire = require.config({
waitSeconds: 90, // give it time
baseUrl: 'http://0.0.0.0:8000/bower_components',
paths: { // NOTE: no .js suffixes
jasmineRequire: 'jasmine/lib/jasmine-core/jasmine',
jasmineHtml: 'jasmine/lib/jasmine-core/jasmine-html',
jasmine: 'jasmine/lib/jasmine-core/boot',
},
shim: {
jasmineHtml: { deps: ['jasmineRequire'] },
jasmine: {
deps: [
'jasmineRequire',
'jasmineHtml',
],
exports: 'jasmine',
},
},
});
localRequire(['jasmine'],
function(jasmine) {
// jasmine has already added 'describe', 'expect', and 'it' to the window
// Run specs
describe('jasmine', function() {
it('should load and put define and it in the global scope', function() {
// No failed assertions or thrown exceptions means jasmine lists a console error
});
describe('async', function() {
// http://stackoverflow.com/questions/9867601/how-do-i-change-the-timeout-on-a-jasmine-node-async-spec
jasmine.DEFAULT_TIMEOUT_INTERVAL = 90000; // let us run longer than the 5s default
var timeout = 6000; // allow specs to run up to 6s
it('should be built-in', function(done) {
setTimeout(function () {
console.log('async done');
done();
}, timeout - 100);
console.log('sync done');
}, timeout);
});
describe('failing', function() {
it('should happen with thrown exceptions', function() {
throw new Error('hope this fails!');
});
it('should happen with wrong expectations', function() {
expect(false).toBeTruthy('That\'s right, fail');
});
});
});
// Run the specs
// jasmine-boot patches window.onload, so call it
window.onload(); // Will be a problem if other client code handles onload
showResults();
console.log('done with snippet');
});
function showResults() {
// The jasmine html reporter creates this element
var reporterHostEl = document.getElementsByClassName('jasmine_html-reporter')[0];
if (!reporterHostEl) {
console.log('no host element');
return;
}
reporterHostEl.style.position = 'absolute';
reporterHostEl.style.backgroundColor = 'rgba(255,255,255,0.8)';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment