Brak opisu

boot.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. Starting with version 2.0, this file "boots" Jasmine, performing all of the necessary initialization before executing the loaded environment and all of a project's specs. This file should be loaded after `jasmine.js`, but before any project source files or spec files are loaded. Thus this file can also be used to customize Jasmine for a project.
  3. If a project is using Jasmine via the standalone distribution, this file can be customized directly. If a project is using Jasmine via the [Ruby gem][jasmine-gem], this file can be copied into the support directory via `jasmine copy_boot_js`. Other environments (e.g., Python) will have different mechanisms.
  4. The location of `boot.js` can be specified and/or overridden in `jasmine.yml`.
  5. [jasmine-gem]: http://github.com/pivotal/jasmine-gem
  6. */
  7. (function() {
  8. /**
  9. * ## Require & Instantiate
  10. *
  11. * Require Jasmine's core files. Specifically, this requires and attaches all of Jasmine's code to the `jasmine` reference.
  12. */
  13. window.jasmine = jasmineRequire.core(jasmineRequire);
  14. /**
  15. * Since this is being run in a browser and the results should populate to an HTML page, require the HTML-specific Jasmine code, injecting the same reference.
  16. */
  17. jasmineRequire.html(jasmine);
  18. /**
  19. * Create the Jasmine environment. This is used to run all specs in a project.
  20. */
  21. var env = jasmine.getEnv();
  22. /**
  23. * ## The Global Interface
  24. *
  25. * Build up the functions that will be exposed as the Jasmine public interface. A project can customize, rename or alias any of these functions as desired, provided the implementation remains unchanged.
  26. */
  27. var jasmineInterface = jasmineRequire.interface(jasmine, env);
  28. /**
  29. * Add all of the Jasmine global/public interface to the proper global, so a project can use the public interface directly. For example, calling `describe` in specs instead of `jasmine.getEnv().describe`.
  30. */
  31. if (typeof window == "undefined" && typeof exports == "object") {
  32. extend(exports, jasmineInterface);
  33. } else {
  34. extend(window, jasmineInterface);
  35. }
  36. /**
  37. * ## Runner Parameters
  38. *
  39. * More browser specific code - wrap the query string in an object and to allow for getting/setting parameters from the runner user interface.
  40. */
  41. var queryString = new jasmine.QueryString({
  42. getWindowLocation: function() { return window.location; }
  43. });
  44. var catchingExceptions = queryString.getParam("catch");
  45. env.catchExceptions(typeof catchingExceptions === "undefined" ? true : catchingExceptions);
  46. /**
  47. * ## Reporters
  48. * The `HtmlReporter` builds all of the HTML UI for the runner page. This reporter paints the dots, stars, and x's for specs, as well as all spec names and all failures (if any).
  49. */
  50. var htmlReporter = new jasmine.HtmlReporter({
  51. env: env,
  52. onRaiseExceptionsClick: function() { queryString.setParam("catch", !env.catchingExceptions()); },
  53. getContainer: function() { return document.body; },
  54. createElement: function() { return document.createElement.apply(document, arguments); },
  55. createTextNode: function() { return document.createTextNode.apply(document, arguments); },
  56. timer: new jasmine.Timer()
  57. });
  58. /**
  59. * The `jsApiReporter` also receives spec results, and is used by any environment that needs to extract the results from JavaScript.
  60. */
  61. env.addReporter(jasmineInterface.jsApiReporter);
  62. env.addReporter(htmlReporter);
  63. /**
  64. * Filter which specs will be run by matching the start of the full name against the `spec` query param.
  65. */
  66. var specFilter = new jasmine.HtmlSpecFilter({
  67. filterString: function() { return queryString.getParam("spec"); }
  68. });
  69. env.specFilter = function(spec) {
  70. return specFilter.matches(spec.getFullName());
  71. };
  72. /**
  73. * Setting up timing functions to be able to be overridden. Certain browsers (Safari, IE 8, phantomjs) require this hack.
  74. */
  75. window.setTimeout = window.setTimeout;
  76. window.setInterval = window.setInterval;
  77. window.clearTimeout = window.clearTimeout;
  78. window.clearInterval = window.clearInterval;
  79. /**
  80. * ## Execution
  81. *
  82. * Replace the browser window's `onload`, ensure it's called, and then run all of the loaded specs. This includes initializing the `HtmlReporter` instance and then executing the loaded Jasmine environment. All of this will happen after all of the specs are loaded.
  83. */
  84. var currentWindowOnload = window.onload;
  85. window.onload = function() {
  86. if (currentWindowOnload) {
  87. currentWindowOnload();
  88. }
  89. htmlReporter.initialize();
  90. env.execute();
  91. };
  92. /**
  93. * Helper function for readability above.
  94. */
  95. function extend(destination, source) {
  96. for (var property in source) destination[property] = source[property];
  97. return destination;
  98. }
  99. }());