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

the times parameter in cy.interceptor is not respected #16821

Closed
A-ZC-Lau opened this issue Jun 7, 2021 · 6 comments · Fixed by #17138
Closed

the times parameter in cy.interceptor is not respected #16821

A-ZC-Lau opened this issue Jun 7, 2021 · 6 comments · Fixed by #17138

Comments

@A-ZC-Lau
Copy link

A-ZC-Lau commented Jun 7, 2021

Current behavior

When the times parameter is used in RouteMatcher, it doesn't respect the limit (i.e. using times: 1 will still match more than once

Screen Shot 2021-06-07 at 5 14 59 pm

Desired behavior

It should only match up to the times specified.

Test code to reproduce

/// <reference types="Cypress" />
/* global cy */

before(() => {
	cy.visit("https://mocki.io/fake-json-api");
});

describe(
	"interceptor times test",
	function () {
		it(
			"should not intercept twice",
			function () {
				const routeMatcher = {
					method: "POST",
					times: 1,
					url: /graphql$/
				};
				cy.intercept(
					routeMatcher,
					(req) => {
						req.continue(res => {
							console.log("hello i am here");
							console.log(res);
						});
					}
				)
					.as("interceptor");
				cy.get("#success").should("not.be.visible");
				cy.get("#body").type("{ \"abc\": 123 }", { parseSpecialCharSequences: false });
				cy.get("#create").click();
				cy.get("#success").should("be.visible", { timeout: 10000 });

				cy.get("#body").clear().type("{ \"another\": 123 }", { parseSpecialCharSequences: false });
				cy.get("#create").click();
				cy.get("#success").should("not.be.visible");
				cy.get("#success").should("be.visible", { timeout: 10000 });
			});
	});

Versions

7.4

@jennifer-shehane
Copy link
Member

I can verify the issue described above. I modified the example slightly so that there is a failing test (that should be passing).

The test should only mock the data the first time the request matches (because times: 1 was passed), but it passes on 2 requests.

it('should not intercept twice', () => {
  cy.visit('https://mocki.io/fake-json-api')
  
  cy.intercept({
    method: 'POST',
    times: 1,
    url: /graphql$/
  },
    (req) => {
      req.continue(res => {
        res.body.data.createPublicStage.url = 'foobar'
      })
    }
  ).as('interceptor')

  cy.get('#success').should('not.be.visible')
  cy.get('#body').type('{ "abc": 123 }', { parseSpecialCharSequences: false })
  cy.get('#create').click() // submit form that will generate url
  // ✅ success msg contains mocked 'url' dats in 1st match
  cy.get('#success').should('contain', 'foobar') 

  cy.get('#body').clear().type('{ "another": 123 }', { parseSpecialCharSequences: false })
  cy.get('#create').click() // submit form that will generate url
  cy.get('#success').should('not.be.visible') // make sure the succes msg rerenders
  cy.get('#success').should('be.visible')
    // ❗️ success msg shouldn't contain mocked 'url' data in 2nd match
    .and('not.contain', 'foobar') 
})

Screen Shot 2021-06-15 at 11 19 26 AM

@sainthkh
Copy link
Contributor

I ran the example above and found out that cy.intercept intercepts once correctly.

Peek 2021-06-28 16-01

The thing we need to check is why foobar is applied twice.

@jennifer-shehane
Copy link
Member

@sainthkh I'm seeing the exact opposite behavior, in Cypress 7.6.0.

@sainthkh
Copy link
Contributor

@jennifer-shehane It's because I didn't restart Cypress after modifying code. I fixed the problem and opened the PR.

@A-ZC-Lau After fixing the req.continue behavior`, @jennifer-shehane's test is still failing. I guess it's because of the cache problem on mocki.io's side.

I genereated the api urls with {"abe":123} and {"another":123}, but they were same. Is this intended behavior?

@cypress-bot cypress-bot bot added stage: needs review The PR code is done & tested, needs review and removed stage: work in progress labels Jun 29, 2021
@cypress-bot cypress-bot bot added stage: pending release and removed stage: needs review The PR code is done & tested, needs review labels Jul 7, 2021
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Jul 7, 2021

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

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Jul 20, 2021

Released in 8.0.0.

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

@cypress-bot cypress-bot bot locked as resolved and limited conversation to collaborators Jul 20, 2021
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.

3 participants