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.

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

See original GitHub issue

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! ❤️

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
jennifer-shehanecommented, May 31, 2019

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
0reactions
jennifer-shehanecommented, Feb 28, 2020
Read more comments on GitHub >

github_iconTop Results From Across the Web

readFile - Cypress Documentation
By default, cy.readFile() asserts that the file exists and will fail if it does not exist. It will retry reading the file if...
Read more >
What is the difference between cy.readFile and cy.fixture in ...
There are two major differences. First, the two functions handle filepaths differently. cy.readFile() starts at your cypress project folder, ...
Read more >
reading files in a directory -- how to get a list of ... - Ask TOM
Original Answer: We cannot do this with PLSQL directly however, using Java (or a C extproc) we can do this pretty easily. The...
Read more >
Python Tutorial - File and Text Processing
We can use a with -statement to open a file, which will be closed automatically ... does not exist".format(fileIn)) sys.exit(1) # Verify destination...
Read more >
VS Code API | Visual Studio Code Extension API
However, when invoking an editor command not all argument types are supported. This is a sample that registers a command handler and adds...
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