I have an Angular app with Cypress for end-to-end testing.
I am testing my login page to see if the localstorage is set correctly.
This is the cypress js file:
describe('LOGIN MODULE', () => {
it('sets the LocalStorage correctly', () => {
cy.visit('https://www.example.com');
cy.get('[formcontrolname="username"]').type('user');
cy.get('[formcontrolname="password"]').type('password', { log: false });
cy.get('form')
.submit()
.then(() => {
localStorage.getItem('user').then($user => { <=== Error on this line
expect($user.includes('customerNr')).to.equal(true);
});
});
});
});
The user item in the localstorage is as json string and looks like this:
{
"fullName": "my name",
"email": "my email",
"customerNr": "1234",
"token": "my token",
"userId": "my userid",
"userName": "my username",
"roles": [
"role 1"
]
}
I want to check if the user item exists and if the nested properties (fullName, email,...) have the correct values for this user.
But I am stuck on this error: "Cannot read properties of null (reading 'then')" on localStorage.getItem('user')