I am trying to write cypress tests that check if data is received into my data table from the API.
One test to check the data, another test to check for an error message if there is no data from the API.
To simulate the API's response (one with data and one without) I am using cy.intercept().
I can't seem to get two separate intercepts to work within my single test file.
What is the recommended procedure to test multiple different intercepts in the same cypress test file, or should they be in separate files?
Any help appreciated!
describe('Display Data Page', () => {
beforeEach(() => {
// Fake the backend Http response using the json file in our cypress fixtures folder.
cy.intercept('GET', 'https://localhost:3000/GetData', {fixture:'my_data.json'}).as('my_data');
cy.visit('/display-data');
});
it('Should display a list of my data', () => {
cy.wait('@my_data');
cy.get('.dataGrid').should('not.be.empty');
cy.get('.dataGrid').should("contain", "Blah");
});
it('Should display a error message when api is offline', () => {
// Reply with a HTTP 400 bad request to test error message
cy.intercept('GET', 'https://localhost:3000/GetData', {status: 400}).as('my_error_data');
cy.wait('@my_error_data');
cy.get('.errorMessage').should('not.be.empty');
cy.get('.errorMessage').should("contain", "Error: API is non-responsive!");
});
});
The basic problem is that the intercept
my_error_datais being set too late.Since intercepts are listeners, they need to be set up before they code that triggers the backend call. That trigger is
cy.visit(), but in the second test it runs before the intercept is set up.Move the visit out of the
beforeEach()and into the tests to fix it.Keeping the beforeEach()
If you really want to keep the
beforeEach()structure, make theStaticResponsepart of thecy.intercept()dynamic.You would only need one intercept with that pattern.
Example test
Here's an example test to verify the principle.
It turns out that Cypress is smart enough to know when the
staticResponseis a fixture that should be sent asbody, and when the object represents the whole response.