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.

Fixture returns outdated/false data

See original GitHub issue

Current behavior:

Reading and writing fixtures seems to not work as expected (please let me know if this is my error). Across two different tests (within the same spec) the returned value from cy.fixture is outdated and should have been updated by a previous call to cy.writeFile. This looks to me like a caching issue?

Screen Shot 2019-07-15 at 16 06 50

Desired behavior:

It should always return the latest data from the fixture and not something outdated.

Steps to reproduce: (app code and test code)

  1. Create a empty spec and paste the below code.
  2. Run the spec and see the output in the dashboard.
describe('fixture', () => {
  it('step 1', () => {
    // Create the fixture first
    cy.writeFile("cypress/fixtures/test-temp.json", {
      id: 1,
      name: 'Step 1'
    });

    // Let's see the data, it should be fine
    cy.fixture("test-temp").then(data => cy.log(data));

    // Update the fixture again
    cy.writeFile("cypress/fixtures/test-temp.json", {
      id: 1,
      name: 'Step 2'
    });
  });

  it('step 2', () => {
    // Let's wait 5 seconds just to be sure
    cy.wait(5000);

    // The returned data is { id: 1, name: 'Step 1' }
    // Should be { id: 1, name: 'Step 2' }
    cy.fixture("test-temp").then(data => cy.log(data));
  });
});

Versions

Cypress 3.4.0 & 3.3.2 MacOS Mojave Chrome 75

UPDATE: No need to create 2 different tests, it also happens inside the very same test.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:7
  • Comments:27 (3 by maintainers)

github_iconTop GitHub Comments

11reactions
jennifer-shehanecommented, Aug 6, 2019

So I asked the team about this. We are indeed caching the reference to the fixture to save memory. This was intended at the time, but is likely not a great solution now.

We likely need to rework this logic, to not use the cache when the file has been changed. We can detect that the file has changed by initially getting the SHA, compare if SHA has changed, otherwise use cache. Alternatively look at modifiedAt time of the file (if works on all platforms)

Workaround:

You can use cy.readFile() instead of fixture as this will not use the cacheing.

3reactions
x-yuricommented, Jan 21, 2021

I don’t really see a case for changing fixtures. Fixtures is something prepared in advance and shouldn’t change. If you want to randomize test data, you need faker or something.

Change fixtures to match changes made by API requests? Do you even need fixtures in this case? To answer that question I need more details. What data you have in fixtures? What requests you make?

The bottom line is if you need to change fixtures, that’s most likely a sign you’re doing something wrong. Or so I think for now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I use return of fixture function and data which will be ...
I found answer as fixture function will be given as a test argument: @pytest.mark.parametrize("name,surname,age",test_data) def ...
Read more >
pytest fixtures: explicit, modular, scalable
Once pytest finds them, it runs those fixtures, captures what they returned (if anything), and passes those objects into the test function as...
Read more >
Pytest - Fixtures - Tutorialspoint
Therefore, instead of running the same code for every test, we can attach fixture function to the tests and it will run and...
Read more >
fixture | Cypress Documentation
Load a fixed set of data located in a file. Syntax Usage Correct Usage Arguments filePath ... Using null explicitly will return the...
Read more >
pytest-cases
New current_cases fixture to easily know the current case for each parameter ! ... def user_bob(): return "bob" @parametrize_with_cases("data", cases='.
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