-
Notifications
You must be signed in to change notification settings - Fork 46.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unit testing react + backbone without phantom.js #945
Comments
Try http://karma-runner.github.io/0.10/index.html - we used it with PhantomJS plugin (with small shim to implement missing functionality) and Chrome/Firefox plugins. |
To what shim are you referring? I've tried karma initially, but couldn't get PhantomJS to play along nicely. |
Include this snippet to make React work in PhantomJS: if (!Function.prototype.bind) {
Function.prototype.bind = function(scope){
var self = this;
return function(){
return self.apply(scope, arguments);
};
};
} |
I had trouble with @mrjoes's shim and (I believe) underscores if (!Function.prototype.bind) {
var Empty = function(){};
Function.prototype.bind = function bind(that) { // .length is 1
var target = this;
if (typeof target != "function") {
throw new TypeError("Function.prototype.bind called on incompatible " + target);
}
var args = Array.prototype.slice.call(arguments, 1); // for normal call
var binder = function () {
if (this instanceof bound) {
var result = target.apply(
this,
args.concat(Array.prototype.slice.call(arguments))
);
if (Object(result) === result) {
return result;
}
return this;
} else {
return target.apply(
that,
args.concat(Array.prototype.slice.call(arguments))
);
}
};
var boundLength = Math.max(0, target.length - args.length);
var boundArgs = [];
for (var i = 0; i < boundLength; i++) {
boundArgs.push("$" + i);
}
var bound = Function("binder", "return function(" + boundArgs.join(",") + "){return binder.apply(this,arguments)}")(binder);
if (target.prototype) {
Empty.prototype = target.prototype;
bound.prototype = new Empty();
// Clean up dangling references.
Empty.prototype = null;
}
return bound;
};
} |
We use phantomjs for the React unit tests and polyfill .bind here: https://github.com/facebook/react/blob/0.13-stable/src/test/phantomjs-shims.js Closing this out; let me know if anything else is unclear. |
@spicyj, you have no idea how happy your last comment made me. I was neck deep in debugging why Jasmine wouldn't run our specs. After applying the shim you referenced, it now works. Thanks! |
@spicyj's |
@spicyj, you are my hero today. |
PhantomJS doesn't have a Function#bind method, which is required by React. This file is copied from React's own test suite, and solves the problem by adding the required function if it doesn't already exist. Reference: facebook/react#945
Seems that the polyfilled teaspoon Function.prototype.bind support for PhantomJS is not compatible with Ember 1.9. Used a shim from React instead. See also facebook/react#945
I have tried to inject the shim script through page.open(url, function(status) {
if (status === "success") {
if (page.injectJs('phantom-react-shim.js')) {
page.render(pageCaptureFileName, {
format: Settings.pageCaptureFileType,
quality: Settings.pageCaptureQuality
})
}
}
}); |
@spicyj That link is 404'ing. Do you have another copy of the code? |
Edited my comment to work again. |
This is closely related to #455, but not entirely the same.
I'm curious how one'd go around to testing browserified backbone + react apps. It appears that react doesn't play well with phantomjs, so I'm looking for alternatives. Jsdom is next on the list, and I'm looking for suggestions how to properly handle this.
An issue that arises when using
browserify
and not loading the app through an html template is that you're not actually testing against the DOM and can't do things such as<script src='vendor/react.js'></script>
to load packages into the global scope.Do you have any ideas, theorems or suggestions on how to approach unit testing without phantomjs?
The text was updated successfully, but these errors were encountered: