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:

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-darkplugin, 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
- Update git submodules * Update docs/submodules/vid.git from branch 'master' to 607ea3dac27af31e3c8571b4f170dc7fd5c3b3b1 - Cypress>=3.3.1 readFile() does not expect a relative file-path starting w... — committed to onap/doc by ittays 5 years ago
- Cypress>=3.3.1 readFile() does not expect a relative file-path starting with / See: https://github.com/cypress-io/cypress/issues/4352#issuecomment-507963062 Issue-ID: VID-687 Change-Id: I4f3292e80d2... — committed to onap/archived-vid by ittays 5 years ago
- Update git submodules * Update docs/submodules/vid.git from branch 'master' to b0a5365842404f32667ba6cbda12853323624eaf - Cypress>=3.3.1 readFile() does not expect a relative file-path starting w... — committed to onap/doc by ittays 5 years ago
- Cypress>=3.3.1 readFile() does not expect a relative file-path starting with / See: https://github.com/cypress-io/cypress/issues/4352#issuecomment-507963062 Issue-ID: VID-687 Change-Id: I5f5f34be85... — committed to onap/archived-vid by ittays 5 years ago
Cypress cannot find this with the trailing
/in front ofnode_modules, so usingcy.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
3.3.1
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 callpath.resolve(projectRoot, file).I’m sure this was expected to cause no changes, but it has.
cy.readFile('cypress/fixtures/example.json'):/Users/jennifer/Dev/cypress-example-kitchensink/cypress/fixtures/example.jsoncy.readFile('/cypress/fixtures/example.json'):/Users/jennifer/Dev/cypress-example-kitchensink/cypress/fixtures/example.jsoncy.readFile('cypress/fixtures/example.json'):/Users/jennifer/Dev/cypress-example-kitchensink/cypress/fixtures/example.jsoncy.readFile('/cypress/fixtures/example.json'):/cypress/fixtures/example.json