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.

should('have.data') caches data values

See original GitHub issue

Current behavior:

The value of data is cached, such that the same values are returned for all future should('have.data')

Desired behavior:

The value should be updated so as to be able to assert changes in data attributes

Steps to reproduce: (app code and test code)

https://github.com/iota-pi/cypress-bug-data-cache (using React)

// App.js
import React from 'react';

class App extends React.Component {
  constructor (props) {
    super(props);
    this.state = {
      count: 0,
    };
  }

  render() {
    return (
      <button
        onClick={this.handleClick}
        data-count={this.state.count}
      >
        Hello world
      </button>
    );
  }

  handleClick = () => {
    this.setState({ count: this.state.count + 1 });
  }
}

export default App;
/// <reference types="Cypress" />

context('data caching', () => {
  beforeEach(() => {
    cy.visit('http://localhost:3000/')
  })

  it('fails to get count from data', () => {
    cy.get('button')
      .should('have.data', 'count', 0)
      .click()
      .should('have.data', 'count', 1)
  })

  it('gets count from data once', () => {
    cy.get('button')
      .click()
      .click()
      .should('have.data', 'count', 2)
  })
})

Versions

Cypress: 3.6.1 Browser: Chrome 78 / Electron 73 OS: Windows 10

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
BlueWindscommented, Nov 17, 2022

This will be resolved in Cypress 12, coming soon to an npm near you (by soon, I mean “early December”). See #7306 and more specifically https://github.com/cypress-io/cypress/pull/24628 for details.

2reactions
brunolemoscommented, Dec 21, 2020

My current workaround:

Instead of: .should('have.data', 'key', 'value') Do: .should(haveData('key', 'value'))

function haveData(name: string, value: string) {
  return ($el: Element) => {
    expect($el[0].getAttribute(`data-${name}`)).to.be.equal(value);
  };
}

This works without any cache issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What Is Cached Data? Why & How Should You Clear It? - Okta
Common reasons for doing so include: Speed and performance. A full cache needs memory, and if you're full, a bogged down memory doesn't...
Read more >
Caching Best Practices | Amazon Web Services
However, other times, data is best cached in a format that combines multiple records together. Because caches are simple key-value stores, you might...
Read more >
What is Cached Data? What does Clear Cache Mean and ...
A cache's primary purpose is to speed up retrieval of web page resources, decreasing page load times. Another critical aspect of a cache...
Read more >
All things caching- use cases, benefits, strategies, choosing a ...
Since the application writes only to the caching service, it does not need to wait till data is written to the underlying data...
Read more >
Caching guidance - Azure Architecture Center | Microsoft Learn
Caching can dramatically improve performance, scalability, and availability. The more data that you have and the larger the number of users that need...
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