|
| 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 | +
|
| 4 | + 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. |
| 5 | +
|
| 6 | + The location of `boot.js` can be specified and/or overridden in `jasmine.yml`. |
| 7 | +
|
| 8 | + [jasmine-gem]: http://github.com/pivotal/jasmine-gem |
| 9 | + */ |
| 10 | + |
| 11 | +(function() { |
| 12 | + |
| 13 | + /** |
| 14 | + * ## Require & Instantiate |
| 15 | + * |
| 16 | + * Require Jasmine's core files. Specifically, this requires and attaches all of Jasmine's code to the `jasmine` reference. |
| 17 | + */ |
| 18 | + window.jasmine = jasmineRequire.core(jasmineRequire); |
| 19 | + |
| 20 | + /** |
| 21 | + * Create the Jasmine environment. This is used to run all specs in a project. |
| 22 | + */ |
| 23 | + var env = jasmine.getEnv(); |
| 24 | + |
| 25 | + /** |
| 26 | + * ## The Global Interface |
| 27 | + * |
| 28 | + * 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. |
| 29 | + */ |
| 30 | + var jasmineInterface = { |
| 31 | + describe: function(description, specDefinitions) { |
| 32 | + return env.describe(description, specDefinitions); |
| 33 | + }, |
| 34 | + |
| 35 | + xdescribe: function(description, specDefinitions) { |
| 36 | + return env.xdescribe(description, specDefinitions); |
| 37 | + }, |
| 38 | + |
| 39 | + it: function(desc, func) { |
| 40 | + return env.it(desc, func); |
| 41 | + }, |
| 42 | + |
| 43 | + xit: function(desc, func) { |
| 44 | + return env.xit(desc, func); |
| 45 | + }, |
| 46 | + |
| 47 | + beforeEach: function(beforeEachFunction) { |
| 48 | + return env.beforeEach(beforeEachFunction); |
| 49 | + }, |
| 50 | + |
| 51 | + afterEach: function(afterEachFunction) { |
| 52 | + return env.afterEach(afterEachFunction); |
| 53 | + }, |
| 54 | + |
| 55 | + expect: function(actual) { |
| 56 | + return env.expect(actual); |
| 57 | + }, |
| 58 | + |
| 59 | + pending: function() { |
| 60 | + return env.pending(); |
| 61 | + }, |
| 62 | + |
| 63 | + spyOn: function(obj, methodName) { |
| 64 | + return env.spyOn(obj, methodName); |
| 65 | + }, |
| 66 | + |
| 67 | + jsApiReporter: new jasmine.JsApiReporter({ |
| 68 | + timer: new jasmine.Timer() |
| 69 | + }) |
| 70 | + }; |
| 71 | + |
| 72 | + /** |
| 73 | + * 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`. |
| 74 | + */ |
| 75 | + if (typeof window == "undefined" && typeof exports == "object") { |
| 76 | + extend(exports, jasmineInterface); |
| 77 | + } else { |
| 78 | + extend(window, jasmineInterface); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Expose the interface for adding custom equality testers. |
| 83 | + */ |
| 84 | + jasmine.addCustomEqualityTester = function(tester) { |
| 85 | + env.addCustomEqualityTester(tester); |
| 86 | + }; |
| 87 | + |
| 88 | + /** |
| 89 | + * Expose the interface for adding custom expectation matchers |
| 90 | + */ |
| 91 | + jasmine.addMatchers = function(matchers) { |
| 92 | + return env.addMatchers(matchers); |
| 93 | + }; |
| 94 | + |
| 95 | + /** |
| 96 | + * Expose the mock interface for the JavaScript timeout functions |
| 97 | + */ |
| 98 | + jasmine.clock = function() { |
| 99 | + return env.clock; |
| 100 | + }; |
| 101 | + |
| 102 | + /** |
| 103 | + * The `jsApiReporter` also receives spec results, and is used by any environment that needs to extract the results from JavaScript. |
| 104 | + */ |
| 105 | + env.addReporter(jasmineInterface.jsApiReporter); |
| 106 | + |
| 107 | + /** |
| 108 | + * Setting up timing functions to be able to be overridden. Certain browsers (Safari, IE 8, phantomjs) require this hack. |
| 109 | + */ |
| 110 | + window.setTimeout = window.setTimeout; |
| 111 | + window.setInterval = window.setInterval; |
| 112 | + window.clearTimeout = window.clearTimeout; |
| 113 | + window.clearInterval = window.clearInterval; |
| 114 | + |
| 115 | + /** |
| 116 | + * ## Execution |
| 117 | + * |
| 118 | + * 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. |
| 119 | + */ |
| 120 | + var currentWindowOnload = window.onload; |
| 121 | + |
| 122 | + window.onload = function() { |
| 123 | + if (currentWindowOnload) { |
| 124 | + currentWindowOnload(); |
| 125 | + } |
| 126 | + env.execute(); |
| 127 | + }; |
| 128 | + |
| 129 | + /** |
| 130 | + * Helper function for readability above. |
| 131 | + */ |
| 132 | + function extend(destination, source) { |
| 133 | + for (var property in source) destination[property] = source[property]; |
| 134 | + return destination; |
| 135 | + } |
| 136 | + |
| 137 | +}()); |
0 commit comments