question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

cypress fails to set localstorage at travis CI

See original GitHub issue

Current behavior:

cypress fails to set localStorage key at travis CI

Reproduce:

Im trying to configure CI e2e automation of react app.

In my app i have ProtectedRoute component which redirects not-loggedin-user to “login” page:

export const ProtectedRoute = ({ component: Component, ...rest }) => (
  <Route
    {...rest}
    render={props =>
      localStorage.getItem("user") ? ( // here it checks if localstorage has "user" key
        <Component {...props} />
      ) : (
        <Redirect
          to={{ pathname: "/login", state: { from: props.location } }}
        />
      )
    }
  />
);

In my test i’m trying to access some “protected” route and do some checks on the page. As recommended at cypress documentation - i’m not logging-in using UI, but instead, i use to call some command i created to log-in programmatically:

beforeEach(() => {
  cy.login(); // the command
})

describe('dashboard', () => {
  it('should show dashboard page', () => {        
    cy.url().should('include', 'dashboard');
  })
})

Here is the command, it`s simply creates the “user” key inside localStorage:

Cypress.Commands.add('login', () => {
   const user =  {"UserName": "Ivan","UserLastName": "Susanin"}
   localStorage.setItem('user', JSON.stringify(user))
}

At my local environment i can see the tests passing. But, at travis-CI run i see the tests failing continuously, for the reason of url is “login”

screenshot

Versions

Here is my travis config file:

language: node_js
node_js:
  - 10.13
addons:
  apt:
    packages:
      # Ubuntu 16+ does not install this dependency by default, so we need to install it ourselves
      - libgconf-2-4
cache:
  # cache both npm modules and Cypress binary
  directories:
    - ~/.npm
    - ~/.cache
script:
  - npm run format:check
  - npm run lint
  - npm test
  - node fake-server/server &
  - npm run test:all

Please help .
Thanks

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
jennifer-shehanecommented, Mar 18, 2020

Unfortunately we have to close this issue as there is not enough information to reproduce the problem. This does not mean that your issue is not happening - it just means that we do not have a path to move forward.

Please comment in this issue with a reproducible example and we will consider reopening the issue.

0reactions
pjawscommented, Mar 18, 2020

I have a similar issue with a local storage related test randomly failing in CI.

Here is the test:

  it('sets the CJ event query parameter in local storage', () => {
    const expected = 'test_cjevent';

    cy.visit(`/?cjevent=${expected}`).should(async () => {
      const actual = await JSON.parse(localStorage.getItem('cjevent'));

      expect(actual.value).to.eq(expected);
      expect(actual.expiryDate).greaterThan(new Date().getTime());
    });
  });

And here is the error message when it fails:

  1) Conversion Tracking sets the CJ event query parameter in local storage:
     TypeError: Cannot read property 'value' of null
      at Context.<anonymous> (*********************/__cypress/tests?p=cypress/integration/conversionTracking.spec.js-718:2098:21)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Set local storage in Cypress - Stack Overflow
This function works when running in node. I'm trying to create a cypress command that will set local storage, then visit the application....
Read more >
cypress-io/cypress - Gitter
Anyone has any experience using Travis CI with Cypress-IO? ... Did you setup dashboard to collect them? because travis does not have built-in...
Read more >
Changelog - Cypress Documentation
Added messaging after failures in CI to direct people to set up recording to the Cypress Cloud if they are not already set...
Read more >
@cypress/request-promise - npm
Cypress's fork of the simplified HTTP request client 'request' with Promise support. Powered by Bluebird.. Latest version: 4.2.6, last published: 3 years ...
Read more >
Category: products | Better world by better software
How to run the failed tests from DevTools Console using @bahmutov/cy-grep plugin. ... The Simplest CI Setup For Running Cypress Tests.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found