I'm writing an automated test script using TestCafe and Node.JS. One of the tests is to download a file and validate that the download is complete. I don't want to write a hard coded
await t.wait(4000);
because since my test is based on a data driven framework I can feed it lots of data with lots of different files and file types. So the file size could vary by extreme amounts from a few Kilobytes to Gigabytes.
So writing
await t.wait(4000);
might work for one test case but will almost certainly fail for other test cases.
I'm using dependency 'downloads-folder' to discover the path to the downloads folder on the local system. From there I look for the specific file that I expect to be there. It works for small text files and small zip files that download fast. But as soon as I try to download a really large zip file with lots of data it fails
if (fs.existsSync(zipFileNameAndPath)) {
await t.expect(true).eql(true); // Report it as a success.
} else {
console.log('file does NOT exist: ' + zipFileNameAndPath);
await t.expect(false).eql(true); // Report it as a failure!
}
So my question is:
Is there a way to do something like
if (fs.existsSync(zipFileNameAndPath){[ timeout: 50000000]}) {...} else {...}
Where the resulting timeout would work as a dynamic timeout, waiting to see when the file is found, and if it is found before the timeout expired then it would return true, and only after the timeout period has passed would it return false?
I'm looking for a Synchronous answer that doesn't have any dependencies on things like Lodash or JQuery and works fine in ES6.