martyjs / marty

A Javascript library for state management in React applications
http://martyjs.org
MIT License
1.09k stars 77 forks source link

Running headless tests with PhantomJS as a 'browser' fails #367

Closed maikdiepenbroek closed 8 years ago

maikdiepenbroek commented 8 years ago

After setting up a test suite and trying to test with PhantomJS, i get the following error:

Error: Could not find React
  at [my own path, which i removed]:linenumber:0 <- webpack:///~/marty/marty.js:44:0

Looking into this file i see the following code:

if (typeof global === "undefined" && windowDefined) {
      window.global = window;
    }

    __webpack_require__(148).polyfill();
    __webpack_require__(151);

    var Marty = __webpack_require__(153);
    var marty = new Marty('0.10.4', react());

    marty.use(__webpack_require__(270));
    marty.use(__webpack_require__(280));
    marty.use(__webpack_require__(282));
    marty.use(__webpack_require__(299));
    marty.use(__webpack_require__(315));
    marty.use(__webpack_require__(320));
    marty.use(__webpack_require__(323));
    marty.use(__webpack_require__(327));
    marty.use(__webpack_require__(329));
    marty.use(__webpack_require__(333));
    marty.use(__webpack_require__(341));
    marty.use(__webpack_require__(344));
    marty.use(__webpack_require__(346));
    marty.use(__webpack_require__(349));
    marty.use(__webpack_require__(351));

    module.exports = marty;

    function react() {
      try {
        return module.parent.require("react");
      } catch (e) {
        try {
          return __webpack_require__(353);
        } catch (e) {
          if (windowDefined && window.React) {
            return window.React;
          }
        }
      }

      throw new Error('Could not find React');
    }
/* WEBPACK VAR INJECTION */}.call(exports, (function() { return this; }()), __webpack_require__(147)(module)))

Which is the contents of this file: https://github.com/martyjs/marty/blob/master/marty.js

It looks to me as if this has something to do with the window object missing, since using 'Chrome' as a browser works fine.

Edit: Looking at the code again it looks like the bottom part causes the error, which seems weird since react is resolved as a dependency through webpack.

maikdiepenbroek commented 8 years ago

I've been able to find a workaround for this by using the following

npm install --save-dev phantomjs-polyfill
require('phantomjs-polyfill')
var React = require('react');

Props to https://github.com/conversocial/phantomjs-polyfill for the npm installable polyfill.

taion commented 8 years ago

This issue was fixed in PhantomJS 2, though? https://github.com/ariya/phantomjs/issues/10522

Anyway, good to see that you've found a solution.