`I.waitForFile` (FileSystem helper) fails if directory does not exist (yet)

Open Beginner friendly
#5,420 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Quiet
Tech stack
javascript, nodejs
Domain
testing

Research direction

Start in lib/helper/FileSystem.js at isFileExists and trace how I.waitForFile handles a missing parent directory and its timeout cleanup. Confirm the behavior with the existing FileSystem helper coverage if available; done means waiting does not throw when the directory is initially absent and still detects the file once it appears.

Written by the indexing model from the issue text.

Description

stale

I.waitForFile sometimes fails and throws weird exceptions if the directory containing the expected file does not exist yet.

Example (all output goes to ${codecept_dir}/output/):

  I.handleDownloads('downloads/example.txt');
  I.click('button.download');
  I.amInPath('output/downloads');
  I.waitForFile('example.txt', 5);

If the directory ${codecept_dir}/output/downloads does not exist yet (it will be created by the browser download at some point in time), the function I.waitForFile will throw an exception ReferenceError: Cannot access 'watcher' before initialization and the test will fail.

Root cause is in the function isFileExists of the FileSystem helper. The line const watcher = fs.watch(dir, (eventType, filename) => {... will throw if dir does not exist yet. This exception remains unhandled. Later, the timer for the 5sec timeout detection will try to close the uninitialized watcher, leading to the ReferenceError.

I will try to create a PR.

Dominant language
JavaScript
Stars
4.2k
Forks
756
Avg merge
2d 9h
Merged PRs (30d)
16

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from codeceptjs/CodeceptJS

All issues in codeceptjs/CodeceptJS

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.