Skip to content
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

Closed
yoshuawuyts opened this issue Jan 20, 2014 · 11 comments
Closed

Unit testing react + backbone without phantom.js #945

yoshuawuyts opened this issue Jan 20, 2014 · 11 comments

Comments

@yoshuawuyts
Copy link

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?

@mrjoes
Copy link

mrjoes commented Feb 19, 2014

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.

@yoshuawuyts
Copy link
Author

To what shim are you referring? I've tried karma initially, but couldn't get PhantomJS to play along nicely.

@mrjoes
Copy link

mrjoes commented Feb 20, 2014

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);
        };
    };
}

@zmbush
Copy link

zmbush commented Mar 11, 2014

I had trouble with @mrjoes's shim and (I believe) underscores _.bind, so I used the following shim which I pulled out of https://github.com/es-shims/es5-shim/blob/master/es5-shim.js#L48:

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;
    };
  }

@sophiebits
Copy link
Collaborator

sophiebits commented Apr 7, 2014

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.

@trotzig
Copy link

trotzig commented Apr 24, 2014

@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!

@aroc
Copy link

aroc commented Aug 28, 2014

@spicyj's phantomjs-shims.js worked for me as well.

@neemzy
Copy link

neemzy commented Sep 27, 2014

@spicyj, you are my hero today.

ropez added a commit to snaptv/mimosa-testem-require that referenced this issue Nov 19, 2014
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
watsonbox added a commit to watsonbox/facture that referenced this issue Dec 29, 2014
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
@loretoparisi
Copy link

I have tried to inject the shim script through page.injectJS, but it didn't work, I guess since page.injectJS does not support closures:

page.open(url, function(status) {
          if (status === "success") {

            if (page.injectJs('phantom-react-shim.js')) {
              page.render(pageCaptureFileName, {
                format: Settings.pageCaptureFileType,
                quality: Settings.pageCaptureQuality
              })
            }

          }
        });

http://phantomjs.org/api/webpage/method/inject-js.html

@neb42
Copy link

neb42 commented Jun 1, 2016

@spicyj That link is 404'ing. Do you have another copy of the code?

@sophiebits
Copy link
Collaborator

Edited my comment to work again.

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

No branches or pull requests

9 participants