I'm trying to make a programmatic login with Cypress before each test. My tokens are persisted in localStorage. If I alter it synchronously, i.e. simpy doing localStorage.setItem
without involving cy.request
it works fine. However, I would like to use my auth server (http://localhost:3004/login
below) to generate tokens.
The token is fetched correctly from the server, but as soon as I use the command below along with cy.visit('/')
I get "Failed to visit localhost:8080 - We attempted to make an http request to this URL but the request failed without a response" (see full log below).
As far as I can tell, this is the correct approach when using an auth server for headless authentication. What did I miss?
commands.js
Cypress.Commands.add('login', () => {
cy.request({
method: 'POST',
url: `http://localhost:3004/login`,
body: {
username: '[email protected]',
password: '123'
}
}).its('body').then((body) => {
const vuexData = { user: { authenticationData: { token: body.token } } }
window.localStorage.setItem('vuex', JSON.stringify(vuexData))
})
})
test.js
describe('A test', () => {
beforeEach(() => {
cy.login()
})
it('works', () => {
cy.visit('/')
cy.get('h1').contains('All Books')
})
})
CypressError: cy.visit() failed trying to load:
We attempted to make an http request to this URL but the request failed without a response.
We received this error at the network level:
Error: connect ECONNREFUSED ::1:8080
Common situations why this would fail: - you don't have internet access - you forgot to run / boot your web server - your web server isn't accessible - you have weird network configuration settings on your computer
The stack trace for this error is:
Error: connect ECONNREFUSED ::1:8080 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1056:14)
.its('body').then((body)
,it('works', () => {
, and the Vue appmounted()
they all have correct values and logs occur in correct order (hitting typicode with the POST). The only thing I can suggest is to logbody
after the POST and see if it has the correct form. Elsewise, how does the Vue app use the token? – Richard Matsen