cypress: cy.readFile("path-to-file") failed because the file does not exist, even though it does

Hello! 😃 cy.readFile doesn’t seem to find files at a given, correct path anymore. I noticed this behavior using cypress-dark plugin and my own theme for the test runner, details as following.

Current behavior:

When using dark theme from cypress-dark plugin and my own theme I get the following error message right at the beginning of every test:

image

CypressError: Timed out retrying: cy.readFile("/node_modules/cypress-dark/src/dark.css") failed because the file does not exist at the following path:

/node_modules/cypress-dark/src/dark.css

Because this error occurred during a 'before all' hook we are skipping all of the remaining tests.

Actual file and path itself are correct and work as soon as I rollback to 3.3.0. I can reproduce that behavior as I use cy.readFile in my tests as well.

Desired behavior:

I would love to use cy.readFile as before to use dark- and my custom theme for Cypress’ test runner.

Steps to reproduce: (app code and test code)

  • Install version 3.3.1
  • Easiest way: Use dark theme from cypress-dark plugin, set "useDarkTheme": true, in cypress.json and run any test
  • Alternative: Use the following command anywhere in your test:
cy.readFile('./a/valid/path-to-file.spec.js');

Versions

  • Cypress 3.3.1, older versions are not affected
  • Browser: Chrome 74
  • Operating system: Manjaro linux (local development), Cypress on docker image cypress/browsers:node8.15.1-chrome73

Thank you very much in advance! ❤️

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 1
  • Comments: 15 (6 by maintainers)

Commits related to this issue

Most upvoted comments

Cypress cannot find this with the trailing / in front of node_modules, so using cy.readFile("node_modules/cypress-dark/src/dark.css") should work as a workaround today until a new Cypress fix goes out.

This is a bug and has nothing to do with node_modules, so can be replicated with any directory relative to the projectRoot.

With my file being present at projectRoot/cypress/fixtures/example.json, there are the following differences in the versions.

3.3.0

it('Reads file', () => {
  cy.readFile('cypress/fixtures/example.json')   // passes
  cy.readFile('/cypress/fixtures/example.json')  // passes
})

3.3.1

it('Reads file', () => {
  cy.readFile('cypress/fixtures/example.json')   // passes
  cy.readFile('/cypress/fixtures/example.json')  // fails
})

Looking through the commits between 3.3.0 and 3.3.1, I picked out this little commit https://github.com/cypress-io/cypress/pull/4241/commits/c75a643d42c1ae36c80111bf8e2d9556f06c60e3

Instead of setting the filePath to read/write by calling path.join(projectRoot, file) we now call path.resolve(projectRoot, file).

I’m sure this was expected to cause no changes, but it has.

  • path.join()
    • cy.readFile('cypress/fixtures/example.json'): /Users/jennifer/Dev/cypress-example-kitchensink/cypress/fixtures/example.json
    • cy.readFile('/cypress/fixtures/example.json'): /Users/jennifer/Dev/cypress-example-kitchensink/cypress/fixtures/example.json
  • path.resolve()
    • cy.readFile('cypress/fixtures/example.json') : /Users/jennifer/Dev/cypress-example-kitchensink/cypress/fixtures/example.json
    • cy.readFile('/cypress/fixtures/example.json'): /cypress/fixtures/example.json