question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

cy.intercept not breaking if request object is wrong

See original GitHub issue

Hello,

I want to test that an outgoing request has the correct body. I saw in the documentation that you can do it like this.

describe('Check Marketing logging', () => {
	before(function () {
		cy.visit('https://www.independer.nl/zorgverzekering/intro.aspx');
	});

	it('Check button click - intercept req', function () {
		const requestBody = { "wrong": "request body" };

		cy.intercept('POST', '/api/ue/c', (req) => {
			expect(JSON.stringify(req.body)).to.include(JSON.stringify(requestBody));
		}).as('sLIdentifier');

		cy.getByUe('man', 'input').click({ force: true });

		cy.wait('@sLIdentifier');
	});
});

The only thing is if the condition is right or wrong the runner always shows succeded. See image below.

image

It feels like this is a bug. The workarround I have now it doing it on the wait like this.

it('Check button click - intercept wait', function () {
	const requestBody = { "wrong": "request body" };

	cy.intercept('POST', '/api/ue/c').as('sLIdentifier');
	cy.getByUe('vrouw', 'input').click({ force: true });
	cy.wait('@sLIdentifier').should(({ request }) => {
		expect(JSON.stringify(request.body)).to.include(JSON.stringify(requestBody));
	})
});

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
bvanderneutcommented, Jan 22, 2021

Ah I found the issue. We are using a Cypress Helper package with custom function etc. But in there this code was placed

Cypress.on('uncaught:exception', (err, runnable) => {
	// returning false here prevents Cypress from
	// failing the test
	return false;
});

So when I removed this code it works fine. Don’t know why we put this code here the first place 😦

Thanks for the help.

0reactions
bahmutovcommented, Jan 21, 2021

I cannot recreate this problem with Cypress v6.3.0 using https://github.com/cypress-io/cypress-test-tiny/tree/intercept-issue-14671

Screen Shot 2021-01-21 at 10 41 22 AM
Read more comments on GitHub >

github_iconTop Results From Across the Web

Cypress cy.intercept Problems - Gleb Bahmutov
Use specific intercepts and make sure the stubs are not "breaking" any listeners defined afterwards. Change the first intercept to only work ...
Read more >
intercept - Cypress Documentation
cy.intercept can be used solely for spying: to passively listen for matching routes and apply aliases to them without manipulating the request or...
Read more >
Having trouble asserting on requests using cy.intercept. 'cy.its ...
All is good upto verfying the status code then it breaks. This is the error thrown: Timed out retrying: cy.its() errored because the...
Read more >
A Practical Guide to Intercepting Network Requests in Cypress
In this article, I'd like to walk you through some of the capabilities of Cypress' .intercept() command. It is a super useful tool,...
Read more >
cypress-io/cypress - Gitter
I'm trying to use the new cy.intercept for GraphQL responses. I do not want to stub or mock. I simply want to grab...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found