07 / 07

What is the difference between cy.fixture() and cy.readFile()?

  1. 1

    cy.fixture(): Used to load static test data (usually .json) from the cypress/fixtures folder.

  2. 2

    cy.readFile(): Used to read the actual content of any file on your file system. This is not limited to fixtures, and can read files like .txt, .log, .json, .csv, etc.

Difficulty: 3/10
Topics: test-data-management, file-io, caching-behavior

Scenario Questions

0-2 years experience
  1. 1

    You're writing a test that needs a list of test users. The data lives in cypress/fixtures/users.json and never changes between runs. Which command do you reach for and why?

  2. 2

    A test needs to read a JSON config file that sits outside the project (e.g., /tmp/test-config.json) and might change between test runs. Would you use cy.fixture() or cy.readFile()? What's the practical difference you'd see?

2-5 years experience
  1. 1

    You're debugging a flaky test where cy.fixture('data.json') returns stale data after a previous test mutated the parsed object in memory. Walk me through why this happens and how you'd fix it without switching to cy.readFile().

  2. 2

    Your team stores test data in a shared fixtures folder. A new feature requires reading a CSV export that the app generates at runtime into the downloads folder. The file path includes a timestamp. How do you structure this test, and why doesn't cy.fixture() work here?

5-8 years experience
  1. 1

    In a large suite, you notice cy.fixture() calls are slowing down test startup because hundreds of JSON files are being parsed upfront. The data is only needed in a subset of specs. How would you refactor the data loading strategy to improve performance while keeping tests maintainable?

  2. 2

    You're migrating from Cypress 9 to 13 and discover that cy.fixture() now caches differently across spec files in parallel mode. A test that worked in serial now fails because it expects a fresh object each run. How do you diagnose this and what's the minimal change to restore isolation?

8+ years experience
  1. 1

    Your org has 50+ repos with inconsistent test data patterns — some use fixtures, some use readFile, some inline data. You're tasked with defining a cross-team test data strategy that supports parallel execution, deterministic runs, and easy onboarding. What principles do you establish, and how do you enforce them without blocking teams?

  2. 2

    A legacy codebase has tests that mutate fixture objects directly (e.g., fixtureData.user.id = 123) causing cascade failures. You can't rewrite all tests at once. Design a migration path that introduces safe defaults, detects mutations in CI, and lets teams opt in to strict mode incrementally.

Follow-up Questions

  • What happens if you modify a fixture file between tests in the same run?
  • How would you handle a test that needs to read a file generated by the app during the test?