Preserve database data across page navigation (Cypress)
See original GitHub issueI have a create-react-app + cypress setup following the guide here: https://miragejs.com/quickstarts/cypress/
I would like to write a single test case that does the following:
- Visit /register and post some user data.
- Visit /login and login with the newly registered user.
It appears that the registration works just fine, but when Cypress navigates to /login the mirage server is being re-initialized and the created user entry is lost, thus not being able to login:
Here are the register and login handlers:
this.post('/api/auth/login', (schema, request) => {
const { email } = JSON.parse(request.requestBody)
const user = schema.users.findBy({ email })
if (!user) {
return new Response(401)
}
return {
data: {
user,
access_token: 'blah'
}
}
})
this.post('/api/auth/register', function (schema, request) {
const { user: { name, password, surname, email }} = JSON.parse(request.requestBody)
const user = schema.users.create({
name,
password,
surname,
email
})
return { data: { user }}
})
I have a separate test case that programatically creates a user and then logs in, allowing me to assume the login handler works just fine.
Any hints on how to preserve the registered user entry across page visits?
Issue Analytics
- State:
- Created 2 years ago
- Comments:5
Top Results From Across the Web
Share Data Across Specs Using cypress-data-session Plugin
Invalidate cy.session From cypress - data -session · Cypress database handling - Rainer Hahnekamp · Introduction to cy. · Environment Variable with ...
Read more >Stop using Page Objects and Start using App Actions - Cypress
The two items should be there and the completed state should be preserved. The original test in Cypress does everything through the UI....
Read more >Testing Your App | Cypress Documentation
Step 1: Start your server. Assuming you've successfully installed Cypress and opened Cypress in your project, the first thing you'll want to do...
Read more >Best Practices - Cypress Documentation
Best Practice: Use data-* attributes to provide context to your selectors and isolate them from CSS or JS changes. Every test you write...
Read more >visit - Cypress Documentation
Visit a local server running on http://localhost:8000 cy. visit() resolves when the remote page fires its load event.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I believe this has to do with the way that cypress works, and not a problem with Mirage. In between tests, cypress clears everything out of the browser, including cookies, local storage, etc., and that will include the memory where mirage keeps its database.
There are some good instructions in the cypress docs about how to deal with this (https://docs.cypress.io/guides/getting-started/testing-your-app#Logging-in), but mostly it involves using
cy.request()
to interact directly with the api (in your case, mirage) to add the user and log in, and you can put that into a custom cypress command so you can just run something likecy.login()
in abeforeEach
hook in your specs.Cypress recommends against keeping state around between tests, so instead of keeping the same user for all your tests, you’ll want to set up whatever state you need for the individual test by seeding the state, as described in https://miragejs.com/quickstarts/cypress/#step-5-write-tests-using-your-mirage-server.
I’m still getting cypress up and running for my own application, so I’m not 100% positive on the best approaches just yet, but hopefully this gives you something to get started with.
Ah, yes,
cy.visit()
clears everything out (it’s the same as typing the address into the address bar and pressing enter). I think you might be able to get what you want by using history methods instead. See https://github.com/cypress-io/cypress/issues/128#issuecomment-406155816 for an example.I’m going to close the issue since it’s not a miragejs bug, but happy to keep discussing here if it’s useful.