Skip to content

Instantly share code, notes, and snippets.

@techieBrandon
Created January 13, 2014 17:06
Show Gist options
  • Save techieBrandon/8403899 to your computer and use it in GitHub Desktop.
Save techieBrandon/8403899 to your computer and use it in GitHub Desktop.
Timeout to temp resolve race condition for spec file loading on jasmine-requirejs.html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Jasmine Spec Runner</title>
<% css.forEach(function(style){ %>
<link rel="stylesheet" type="text/css" href="<%= style %>">
<% }) %>
<script src="<%= temp %>/require.js"></script>
<script>
require.onError = function(error) {
var message = error.requireType + ': ';
if (error.requireType === 'scripterror' || error.requireType === 'notloaded' && error.requireModules) {
message += 'Illegal path or script error: ' + '[\'' + error.requireModules.join("', '") + '\']';
} else {
message += error.message;
}
throw Error(message);
};
</script>
<% with (scripts) { %>
<% [].concat(jasmine, vendor, helpers).forEach(function(script){ %>
<script src="<%= script %>"></script>
<% }) %>
<% }; %>
<script>
<% if (options.mainRequireConfig) { %>
require.config(<%= serializeRequireConfig(options.mainRequireConfig) %>);
<% } %>
<% var hasCallback = false;
if (options.requireConfig) {
if ('callback' in options.requireConfig) {
/* Inserting launchTest() as the last statement in callback function
to make sure spec is called after all initialization stuffs */
// capture the arguments and body fo callback function
options.requireConfig['callback'].toString().replace(/^function\s+\w*\(([^\)]*)\)\s*\{((?:.|[\n\r])*)}/m, function(match, args, body) {
var being_recreated_function = [];
if (args.length > 0) {
args = args.split(/,\s*/);
being_recreated_function = being_recreated_function.concat(args);
}
// append launchTest() to the end of function body
body += '\nsetTimeout(function(){';
body += '\nlaunchTest();';
body += '\n},5000);';
being_recreated_function = being_recreated_function.concat(body);
// recreate an anonymouse function with modified body
// re-assign callback property by just created new function
// (return value dosen't matter)
options.requireConfig['callback'] = Function.apply(this, being_recreated_function);
hasCallback = true;
});
}
%>
require.config(<%= serializeRequireConfig(options.requireConfig) %>);
<% } %>
</script>
<script>
function launchTest() {
require([
<% scripts.src.forEach(function(script, i){ %>
<% script = script.replace(/\\/g, '/') // replace all backward slashes to forward slash %>
'<%= script %>' <%= i < scripts.src.length-1 ? ',' : '' %>
<% }) %>
],
function(){
setTimeout(function(){
require(['<%= [].concat(scripts.specs,scripts.reporters).join("','") %>'], function(){
require(['<%= scripts.start.join("','") %>'], function(){
// good to go! Our tests should already be running.
});
});
},5000);
}
)
}
</script>
<% if (!hasCallback) { %>
<script>
launchTest();
</script>
<% } %>
</head>
<body>
</body>
</html>
@techieBrandon
Copy link
Author

Reordering scripts to load after body doesn't have impact. Added some tracing to the iteration loops:

...
Running "jasmine:src" (jasmine) task
Building Spec Runner { version: '1.3.1',
timeout: 10000,
styles: [],
specs: [ '.tmp/spec//*Spec.js', 'spec//_Spec.js' ],
helpers: 'spec/__/_Helper.js',
vendor: [],
outfile: '_SpecRunner.html',
host: 'http://127.0.0.1:9000/',
template: { process: [Function] },
templateOptions: { requireConfigFile: 'grails-app/assets/scripts/js/config.js' },
phantomjs: {},
junit: {},
keepRunner: true }
Getting relative [.] file list for pattern [.tmp/spec//*Spec.js,spec//*Spec.js] with options: undefined
/

/\

!
!
Getting relative [.] file list for pattern [.grunt/grunt-contrib-jasmine/jasmine.css] with options: { nonull: true }
/

/\

!
Getting relative [.] file list for pattern [.grunt/grunt-contrib-jasmine/phantom-polyfill.js] with options: undefined
/

/\

!
Getting relative [.] file list for pattern [.grunt/grunt-contrib-jasmine/jasmine.js,.grunt/grunt-contrib-jasmine/jasmine-html.js] with options: undefined
/

/\

!
!
Getting relative [.] file list for pattern [spec/*_/_Helper.js] with options: { nonull: true }
/

/\

!
Getting relative [.] file list for pattern [.tmp/scripts/config.js,.tmp/scripts/init.js,.tmp/scripts/solutions/default.js,.tmp/scripts/templates.js,.tmp/scripts/tnd/coreui/collection/imsis.js,.tmp/scripts/tnd/coreui/collection/pcapSearchResults.js,.tmp/scripts/tnd/coreui/collection/ratingOverlays.js,.tmp/scripts/tnd/coreui/collection/settingPermissions.js,.tmp/scripts/tnd/coreui/data/topics/topic.js,.tmp/scripts/tnd/coreui/data/topics/topicManager.js,.tmp/scripts/tnd/coreui/layout/component/dashboard.js,.tmp/scripts/tnd/coreui/layout/component/dialog.js,.tmp/scripts/tnd/coreui/layout/component/notice.js,.tmp/scripts/tnd/coreui/layout/component/tabContainer.js,.tmp/scripts/tnd/coreui/layout/component/tabContent.js,.tmp/scripts/tnd/coreui/layout/component/tabLabel.js,.tmp/scripts/tnd/coreui/layout/router/router.js,.tmp/scripts/tnd/coreui/layout/view/alerts/content.js,.tmp/scripts/tnd/coreui/layout/view/alerts/label.js,.tmp/scripts/tnd/coreui/layout/view/arim/content.js,.tmp/scripts/tnd/coreui/layout/view/arim/label.js,.tmp/scripts/tnd/coreui/layout/view/cluster/content.js,.tmp/scripts/tnd/coreui/layout/view/cluster/label.js,.tmp/scripts/tnd/coreui/layout/view/config/content.js,.tmp/scripts/tnd/coreui/layout/view/config/label.js,.tmp/scripts/tnd/coreui/layout/view/imsi/content.js,.tmp/scripts/tnd/coreui/layout/view/imsi/dialog.js,.tmp/scripts/tnd/coreui/layout/view/imsi/label.js,.tmp/scripts/tnd/coreui/layout/view/netperf/content.js,.tmp/scripts/tnd/coreui/layout/view/netperf/label.js,.tmp/scripts/tnd/coreui/layout/view/node/content.js,.tmp/scripts/tnd/coreui/layout/view/node/label.js,.tmp/scripts/tnd/coreui/layout/view/pcap/content.js,.tmp/scripts/tnd/coreui/layout/view/pcap/dialog.js,.tmp/scripts/tnd/coreui/layout/view/pcap/label.js,.tmp/scripts/tnd/coreui/layout/view/pegmap/content.js,.tmp/scripts/tnd/coreui/layout/view/pegmap/label.js,.tmp/scripts/tnd/coreui/layout/view/port/content.js,.tmp/scripts/tnd/coreui/layout/view/port/label.js,.tmp/scripts/tnd/coreui/layout/view/rca/content.js,.tmp/scripts/tnd/coreui/layout/view/rca/label.js,.tmp/scripts/tnd/coreui/layout/view/scope/content.js,.tmp/scripts/tnd/coreui/layout/view/scope/label.js,.tmp/scripts/tnd/coreui/layout/view/templates.js,.tmp/scripts/tnd/coreui/layout/view/zoomin/content.js,.tmp/scripts/tnd/coreui/layout/view/zoomin/label.js,.tmp/scripts/tnd/coreui/model/imsi.js,.tmp/scripts/tnd/coreui/model/pcapSearch.js,.tmp/scripts/tnd/coreui/model/pcapSearchResult.js,.tmp/scripts/tnd/coreui/model/ratingOverlay.js,.tmp/scripts/tnd/coreui/model/settingPermission.js,.tmp/scripts/tnd/coreui/utils/ObjectHelper.js,.tmp/scripts/tnd/coreui/utils/config.js,.tmp/scripts/tnd/coreui/utils/constants.js,.tmp/scripts/tnd/coreui/utils/googleLoader.js,.tmp/scripts/tnd/coreui/utils/googleMaps.js,.tmp/scripts/tnd/coreui/utils/googleMapsAPI.js,.tmp/scripts/tnd/coreui/utils/googleVisualization.js,.tmp/scripts/tnd/coreui/utils/solutionLoader.js,.tmp/scripts/tnd/coreui/widget/chart/bullseye.js,.tmp/scripts/tnd/coreui/widget/chart/composite.js,.tmp/scripts/tnd/coreui/widget/chart/pie.js,.tmp/scripts/tnd/coreui/widget/control/alertBadge.js,.tmp/scripts/tnd/coreui/widget/control/button.js,.tmp/scripts/tnd/coreui/widget/control/dashboardButton.js,.tmp/scripts/tnd/coreui/widget/control/dataGrid.js,.tmp/scripts/tnd/coreui/widget/control/googleMap.js,.tmp/scripts/tnd/coreui/widget/control/googleTree.js,.tmp/scripts/tnd/coreui/widget/control/timeControl.js,.tmp/scripts/tnd/coreui/widget/control/topography.js,.tmp/scripts/tnd/coreui/widget/input/anyTimeInput.js,.tmp/scripts/tnd/coreui/widget/input/textInput.js,.tmp/scripts/tnd/viewport.js] with options: { nonull: true }
/

/\

!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
Getting relative [.] file list for pattern [] with options: { nonull: true }
/

/\

Getting relative [.] file list for pattern [.grunt/grunt-contrib-jasmine/reporter.js] with options: undefined
/

/\

!
Getting relative [.] file list for pattern [.grunt/grunt-contrib-jasmine/jasmine-helper.js] with options: undefined
/

/\

!
SpecRunner built
Testing jasmine specs via phantom
Warning: No specs executed, is there a configuration error? Use --force to continue.

Aborted due to warnings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment