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

clientSync is used on a request when trying to create the server different from the index.js file #37

Open
c089 opened this issue Jul 12, 2013 · 2 comments

Comments

@c089
Copy link
Member

c089 commented Jul 12, 2013

I'd like to use visionmedia/supertest to test our rendr app, but have a problem: Supertest takes the express app function and creates a server itself, which is convenient:

   if ('function' == typeof app) app = http.createServer(app);

So, I thought I could test the rendr app like this:

        var supertest = require('supertest'),
            server = require('../../server/server');
        //...
        server.init(function (err) {
            supertest(server.app)
                .get('/products/1')
                .end(function (err, response) {
                    // assertions on response here
                    done(err);
                });
        });

This works as long as I only call urls which go to views which do not use models to fetch data. As soon as a fetch is done, it bails because backbone does not have $ and in the stacktrace we see that clientSync is used:

TypeError: Cannot read property 'ajax' of undefined
at Object.Backbone.ajax (/Users/cnr/Documents/dev/lusini/1up-mario/node_modules/rendr/node_modules/backbone/backbone.js:1202:22)
at Object.Backbone.sync (/Users/cnr/Documents/dev/lusini/1up-mario/node_modules/rendr/node_modules/backbone/backbone.js:1185:38)
at clientSync [as sync] (/Users/cnr/Documents/dev/lusini/1up-mario/node_modules/rendr/shared/syncer.js:33:19)
at _.extend.fetch (/Users/cnr/Documents/dev/lusini/1up-mario/node_modules/rendr/node_modules/backbone/backbone.js:442:19)
at Fetcher.fetchFromApi (/Users/cnr/Documents/dev/lusini/1up-mario/node_modules/rendr/shared/fetcher.js:220:9)
at Fetcher.<anonymous> (/Users/cnr/Documents/dev/lusini/1up-mario/node_modules/rendr/shared/fetcher.js:146:14)
at /Users/cnr/Documents/dev/lusini/1up-mario/node_modules/rendr/node_modules/async/lib/async.js:467:25

The only difference between starting the server through index.js and this approach of requiring the server and using server.initServer is that server.start is not called, but I don't understand where this makes a difference:

exports.start = function start(options) {
  options = options || {};
  var port = options.port || 3030;
  app.listen(port);
  console.log("server pid " + process.pid + " listening on port " + port + " in " + app.settings.env + " mode");
}
@c089
Copy link
Member Author

c089 commented Jul 12, 2013

We debugged this further down and made sure it is not even a problem with supertest: Starting the server ourselves in index.js like this:

server.init(function (err) {
    var httpServer = require('http').createServer(server.app).listen();
    console.log("Running on port" + httpServer.address().port();
});

and running it using 'node index.js', we can sucessfully request pages with data. But if we copy that same initialization code into a mocha test:

    it('should allow us to test against the running server', function (done) {
        server.init(function (err) {
            var httpServer = require('http').createServer(server.app).listen();
            request('http://localhost:' + httpServer.address().port + '/product/foo', function (error, response, body) {
                // asserts
                done(err);
            });
        });
    });

Will cause the exception on any request trying to fetch something.

To be clear: All the initialization of rendr seems to work: The routes are matched and our controller is executed and we can make requests against routes which use controllers which do not fetch() when running through mocha, but as soon as fetch is called, it fails.

@c089
Copy link
Member Author

c089 commented Jul 12, 2013

Okay, got it: We executed unit tests and this functional test in the same test run (which probably is a bad idea anyway, just playing around) and one of our models got included before the server.init() was called.

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

No branches or pull requests

1 participant