https://cypress.io logo
Tests not retaining location between iterations
# i-need-help
m
First time running cypress and the tests are not retaining the location as expected. Instead, I have to run the previous commands to have them pass. Note the sample code has the lines needed to pass commented out. Am I misunderstanding something?
Copy code
describe("Cypress", () => {
  it("opens the app", () => {
    cy.visit("http://localhost:3000")
  })

  it("opens the app and clicks on a hotel", () => {
    //cy.visit("http://localhost:3000")
    cy.get("a").first().click()
    cy.location("pathname").should("include", "hotel")
  })

  it("navigates to the form to add a review", () => {
    //cy.visit("http://localhost:3000")
    //cy.get("a").first().click()
    cy.get("button").contains("+ Add Review").click()
    cy.location("pathname").should("include", "new")
  })
})
b
You can move your cy.visit() and .click()into a BeforeEach() hook.
m
right, but that's not going to scale because each test is going to build on the previous. 😕
do the tests not build on the previous tests final state?
l
No, Cypress tests are, by design, isolated. Best practice is for each test to have its own state.
m
interesting, the book I'm reading says exactly the opposite. was this ever the case? (not arguing, just a major discrepancy)
thanks for the reading material though. 👍
l
Based on that one paragraph it's a bit open to interpretation. I read that is "we don't have to navigate to the application anymore *during this test*"
m
I suppose what threw me off is
this test builds upon the previous test and is therefore already at the correct page
so be it, the cypress docs are clear enough
l
OK yeah that is pretty wrong, haha 😄
e
If you are on the latest version of Cypress and you want the behavior of persistent test state behavior you can set
Test Isolation
to false but it's also not recommended due to previously mentioned best practices. I would say the book you are reading is likely out-of-date and not the ideal source material..... https://docs.cypress.io/guides/core-concepts/test-isolation
That sample code is stressing me out lol
m
the second edition was released in April of '22. First Edition in Dec '21. Likely this is when the chapter on Cypress was made.
Don't need to drag this out further. Thanks again.
OK, sorry, last bit. Looks like this book uses version 9.5.4 of Cypress. We've certainly come a long way since those days!
3 Views