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

XHR requests are not stubbed with intercept when server isn't running #9599

Closed
maximedupre opened this issue Dec 9, 2020 · 15 comments · Fixed by #9631
Closed

XHR requests are not stubbed with intercept when server isn't running #9599

maximedupre opened this issue Dec 9, 2020 · 15 comments · Fixed by #9631

Comments

@maximedupre
Copy link

maximedupre commented Dec 9, 2020

Current behavior

I posted this problem on gitter and another user (@genughaben_gitlab) confirmed he also has this problem.

I want to stub my local REST API to test my front end in insolation. When the REST API is turned on, everything works as expected - the XHR are stubbed. When my REST API is turned off, the XHR fail and are not stubbed.

For example, here's what happens when my API is not started:

Screen Shot 2020-12-09 at 2 20 12 PM
Screen Shot 2020-12-09 at 2 25 51 PM
Screen Shot 2020-12-09 at 2 26 29 PM

And here's what happens when I simply turn on my API and re-run the tests:

Screen Shot 2020-12-09 at 2 23 53 PM

Desired behavior

My server should not be started for XHR stubs to work

Test code to reproduce

Unable to provide a repro, but here is test code for the test shown in the screenshot

describe('Project page', () => {
    beforeEach(() => {
        helper.login(1);
    });

    describe('when the play button is clicked', () => {
        describe('when the stop button is clicked', () => {
            it.only('should stop the project', () => {
                defaultIntercepts();
                cy.visit('https://localhost:4200/projects/1');

                cy.get('#play-button').click();
                cy.get('#stop-button').click();

                const playheadLeftPosObj = { pos: 0 };

                setInitialPlayheadLeftPos(playheadLeftPosObj);
                cy.get('#playhead').should(($el: any) => {
                    expect($el[0].getBoundingClientRect().left).to.equal(
                        playheadLeftPosObj.pos
                    );
                });
            });
        });
    });
});

function defaultIntercepts() {
    cy.intercept('GET', '/users/1', {
        id: 1
    });
    cy.intercept(
        'GET',
        '/projects/1?used-space-wavs-and-videos',
        createProjectFixtures({})
    );
    cy.intercept('GET', '/projects/1/tracks', []);
    cy.intercept('GET', '/projects/1/files?is-consolidation', []);
}

Versions

This issue did not occur with cy.route.

Cypress version: 6.1
Chrome version: Version 87.0.4280.67 (Official Build) (x86_64)
OS version: mac os 10.15.7

@bahmutov
Copy link
Contributor

bahmutov commented Dec 9, 2020 via email

@maximedupre
Copy link
Author

Here is the result with aliases and my API turned on:
Screen Shot 2020-12-09 at 2 54 45 PM

@bahmutov
Copy link
Contributor

bahmutov commented Dec 9, 2020

could you screenshot when the API is off?

@maximedupre
Copy link
Author

Same as in my first post:
Screen Shot 2020-12-09 at 3 05 50 PM
Screen Shot 2020-12-09 at 3 06 42 PM

@bahmutov
Copy link
Contributor

bahmutov commented Dec 9, 2020

Hmm, in cypress-io/cypress-example-recipes#606 I am stubbing the second API endpoint that does not exist, but it works fine. Are your requests going to the right port? I see a mixture of 4200 and 3030. Is this app public or could you create a public reproduction?

@maximedupre
Copy link
Author

Requests are going to the right ports. API is at http://localhost:3030 and front-end is at https://localhost:4200. Unfortunately this isn't a public app and I don't have the time right now to do a repro.

I just tried "http://localhost:4200" (instead of https, to make both the api and front end http), but same problem.

Screen Shot 2020-12-09 at 3 34 26 PM

@tgugli
Copy link

tgugli commented Dec 9, 2020

This looks very similar to the issue that I just posted: #9602

@katia-aa
Copy link

katia-aa commented Dec 9, 2020

Having the same issue here with intercept. The api I'm trying to intercept is behind a proxy. When I turn off my VPN, the test fails. When I turn my VPN back on, it passes.

@kunalnagar
Copy link

Hey guys - do you think the problem may be related to pattern matching of the URL on cy.intercept? In my particular scenario, if I use:

// WORKS
cy.server()
cy.route('**/v1/answers**', 'fixture:answers.json')

image

// DOES NOT WORK
cy.intercept('GET', '**/v1/answers**', { fixture: 'answers.json' })

image

@bahmutov
Copy link
Contributor

bahmutov commented Dec 10, 2020 via email

@jennifer-shehane jennifer-shehane added topic: cy.intercept() stage: needs information Not enough info to reproduce the issue labels Dec 10, 2020
@maximedupre
Copy link
Author

maximedupre commented Dec 10, 2020

I do see a failed OPTIONS request for each failed GET request

Screen Shot 2020-12-10 at 10 07 30 AM

But for some reason the OPTIONS is after the GET?

@rhogeranacleto
Copy link

I have the same problem. My intercept stubs only work if my local server is running, otherwise, it fails.

@katia-aa
Copy link

katia-aa commented Dec 10, 2020

I've spotted a failed OPTIONS request as well after the GET

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Dec 17, 2020

The code for this is done in cypress-io/cypress#9631, but has yet to be released.
We'll update this issue and reference the changelog when it's released.

@cypress-bot cypress-bot bot removed the stage: needs review The PR code is done & tested, needs review label Dec 17, 2020
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Dec 21, 2020

Released in 6.2.0.

This comment thread has been locked. If you are still experiencing this issue after upgrading to
Cypress v6.2.0, please open a new issue.

@cypress-bot cypress-bot bot locked as resolved and limited conversation to collaborators Dec 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants