EthanRBrown / web-development-with-node-and-express

Companion repository to Web Development With Node and Express, first edition.
1.02k stars 502 forks source link

ch05 tests-crosspage.js fails to find 'referrer' field #48

Closed andwaredev-zz closed 8 years ago

andwaredev-zz commented 8 years ago

I've tried multiple versions of zombie but I haven't been able to solve this issue. My tests fail with error saying it cannot read value of null.

assert(browser.field('referrer').value === referrer);

I have the latest versions of Xcode command line tools and node.js

EthanRBrown commented 8 years ago

Hi, Andrew...sorry to hear you're having this problem. Could you tell me what branch/tag you are in the repo? I just tried this on a Linux machine (branch master), and the crosspage tests for ch05 all passed:

image

befreestudios commented 8 years ago

Somewhat related... In the code in the book the call to done() should be within the test and not the nested function as it is. This was throwing some timeout errors on the tests for me... This was true for all of the tests in this test spec.. fix is below...

test('requesting a group rate quote from the hood river tour page should populate the referrer field', function(done) {
            var referrer = 'http://localhost:3000/tours/hood-river';
            browser.visit(referrer, function() {
                browser.clickLink('.requestGroupRate', function() {
                    assert(browser.field('referrer').value === referrer);
                    // CALL TO DONE WAS HERE, WHICH FAILS TESTS
                    // done();
                });
            });
            // CALL TO DONE SHOULD BE HERE
            done();
    });
EthanRBrown commented 8 years ago

Hi, @befreestudios, your statement is not correct. Putting done() where you have it basically circumvents the whole point of the test. That will kick off the page load as an asynchronous event, and then mark the test is done(). It will be listed as successful, but that's a lie! Any errors would have been swallowed up by the asynchronous execution. The test is correct as written, and the call do done is exactly where it should be.... If your test is failing, something else is the issue....

befreestudios commented 8 years ago

Yea, I see what you're saying. However when the call to done() is within the browser.clickLink handler I'm getting this:

1) Cross-Page Tests requesting a group rate quote from the hood river tour page should populate the referrer field:
     Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.
andwaredev-zz commented 8 years ago

@befreestudios, I was getting this error previously as well. I got down to the heart of the issue by adding a try catch block which caught the actual error, which is the one I posted above.

andwaredev-zz commented 8 years ago

@EthanRBrown I cannot get it working with the master branch version either. I guess the issue must be with some package not installing correctly?

EthanRBrown commented 8 years ago

@andrew-ware just to make sure, you are running the server in a separate window, correct? That is, in one window you have node meadowlark.js running, and in the other window you run grunt? The server has to be running for the tests to complete....

andwaredev-zz commented 8 years ago

Yes I was running the server in a separate window. I figured it out.. there was some issue with the package install of zombie in my version of Mac OS. I was able to fix it by sudo installing the packages. Thanks for your time!

EthanRBrown commented 8 years ago

No problem, I'm glad you figured it out! Zombie is a great project, but it's been through a lot of churn...and what they're trying to do is not easy!

kas commented 7 years ago

@andrew-ware I am having the same issue as you. Would you be able to explain specifically what you did to resolve this issue? The only thing that fixed it for me so far is using an uglier assert statement:

assert(browser.resources[0].request.headers._headers[0][1] === referrer);

But I'd rather stick to what the book says and try to do what you did.

I appreciate your help in advance

Edit: Seems like this is a known bug in zombie:

https://github.com/assaf/zombie/issues/908

https://github.com/assaf/zombie/issues/932

https://github.com/assaf/zombie/pull/947

ASudri commented 7 years ago

Hello! I fix this by adding in history.js (zombie) line this.browser.referrer = this.current.url || this.current.window.document.referrer; before line var _document = loadDocument(args); (line 396) And in test use some "brouser.referrer === null " expression to chek referrer. Also, add "--timeout 5000" in mocha command parameter. In my case if I use browser.resources[0].request.headers._headers[0][1] === referrer then come to the err, because the method visit (zombie) does not set the referrer in field-array _headers.