Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Support loading of jasmine more than once during a page load #249

Closed
wants to merge 1 commit into from

2 participants

@coli

Hi, this prabably doesn't handle the commonJS case correctly.

But this is the change I've made to support loading jasmin.js more than once for a single page.

This fixes the issue where jasmine goes into an infinite loop on the timeout methods
#247

To reproduce, have a page with something like

@infews
Owner

If you use jasmine.getGlobal() instead of window, you'd solve the non-browser case. But do you have a spec to show this failing/passing?

@infews infews closed this
@infews infews reopened this
@infews
Owner

(sorry for the accidental close)

@coli

Hi, the spec would have to load jasmine.js twice, then call setTimeout, and it'd get a max stack exceeded error in the console, not sure how to write one...

@infews infews closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 5, 2012
This page is out of date. Refresh to see the latest.
Showing with 47 additions and 38 deletions.
  1. +43 −34 src/core/base.js
  2. +4 −4 src/core/mock-timeout.js
View
77 src/core/base.js
@@ -5,7 +5,49 @@ var isCommonJS = typeof window == "undefined";
*
* @namespace
*/
-var jasmine = {};
+var initedBefore=!!window.jasmine;
+
+var jasmine = initedBefore? {
+ setTimeout:jasmine.setTimeout,
+ clearTimeout:jasmine.clearTimeout,
+ setInterval:jasmine.setInterval,
+ clearInterval:jasmine.clearInterval
+
+} : {};
+
+/**
+ * Allows for bound functions to be compared. Internal use only.
+ *
+ * @ignore
+ * @private
+ * @param base {Object} bound 'this' for the function
+ * @param name {Function} function to find
+ */
+jasmine.bindOriginal_ = function(base, name) {
+ var original = base[name];
+ if (original.apply) {
+ return function() {
+ return original.apply(base, arguments);
+ };
+ } else {
+ // IE support
+ return jasmine.getGlobal()[name];
+ }
+};
+
+jasmine.getGlobal = function() {
+ function getGlobal() {
+ return this;
+ }
+
+ return getGlobal();
+};
+if (!initedBefore){
+ jasmine.setTimeout = jasmine.bindOriginal_(jasmine.getGlobal(), 'setTimeout');
+ jasmine.clearTimeout = jasmine.bindOriginal_(jasmine.getGlobal(), 'clearTimeout');
+ jasmine.setInterval = jasmine.bindOriginal_(jasmine.getGlobal(), 'setInterval');
+ jasmine.clearInterval = jasmine.bindOriginal_(jasmine.getGlobal(), 'clearInterval');
+}
if (isCommonJS) exports.jasmine = jasmine;
/**
* @private
@@ -46,39 +88,6 @@ jasmine.DEFAULT_TIMEOUT_INTERVAL = 5000;
*/
jasmine.CATCH_EXCEPTIONS = true;
-jasmine.getGlobal = function() {
- function getGlobal() {
- return this;
- }
-
- return getGlobal();
-};
-
-/**
- * Allows for bound functions to be compared. Internal use only.
- *
- * @ignore
- * @private
- * @param base {Object} bound 'this' for the function
- * @param name {Function} function to find
- */
-jasmine.bindOriginal_ = function(base, name) {
- var original = base[name];
- if (original.apply) {
- return function() {
- return original.apply(base, arguments);
- };
- } else {
- // IE support
- return jasmine.getGlobal()[name];
- }
-};
-
-jasmine.setTimeout = jasmine.bindOriginal_(jasmine.getGlobal(), 'setTimeout');
-jasmine.clearTimeout = jasmine.bindOriginal_(jasmine.getGlobal(), 'clearTimeout');
-jasmine.setInterval = jasmine.bindOriginal_(jasmine.getGlobal(), 'setInterval');
-jasmine.clearInterval = jasmine.bindOriginal_(jasmine.getGlobal(), 'clearInterval');
-
jasmine.MessageResult = function(values) {
this.type = 'log';
this.values = values;
View
8 src/core/mock-timeout.js
@@ -128,10 +128,10 @@ jasmine.Clock = {
},
real: {
- setTimeout: jasmine.getGlobal().setTimeout,
- clearTimeout: jasmine.getGlobal().clearTimeout,
- setInterval: jasmine.getGlobal().setInterval,
- clearInterval: jasmine.getGlobal().clearInterval
+ setTimeout: jasmine.setTimeout,
+ clearTimeout: jasmine.clearTimeout,
+ setInterval: jasmine.setInterval,
+ clearInterval: jasmine.clearInterval
},
assertInstalled: function() {
Something went wrong with that request. Please try again.