Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

test: retry touch in dir-add watch test to reduce flakiness (#6119)

Mostafa Nazari061cb529cdec
diff --git a/test/integration/helpers.cjs b/test/integration/helpers.cjs
index f966ad52..21584e61 100644
--- a/test/integration/helpers.cjs
+++ b/test/integration/helpers.cjs
@@ -654,7 +654,7 @@ async function shutdownWatchChild(mochaProcess, closed) {
* @param {RegExp} [opts.firstRunCrashPattern] - If set, expect the first run to fail to load, printing only an error stack (no stdout) with a message matching this pattern. No countable run output to wait for.
* @param {'json'|'marker'} [opts.runDetector] - How completed runs are counted; `runMochaWatchJSONAsync` sets `json`
* @param {number} [opts.budgetMs] - Shared deadline for everything this call waits on; must stay below the test's timeout
- * @param {Function} change - A potentially `Promise`-returning callback which changes a watched file; receives the child process and `{waitForRuns}`
+ * @param {Function} change - A potentially `Promise`-returning callback which changes a watched file; receives the child process and `{waitForRuns, runCount}` (`runCount()` reports completed runs observed so far)
* @returns {Promise<RawResult>}
*/
async function runMochaWatchAsync(args, opts, change) {
@@ -712,6 +712,7 @@ async function runMochaWatchAsync(args, opts, change) {
await change(mochaProcess, {
waitForRuns: observer.waitForRuns,
+ runCount: observer.runCount,
});
if (noRerun) {
diff --git a/test/integration/options/watch.spec.cjs b/test/integration/options/watch.spec.cjs
index 3738cca1..121c02b7 100644
--- a/test/integration/options/watch.spec.cjs
+++ b/test/integration/options/watch.spec.cjs
@@ -129,14 +129,17 @@ describe("--watch", function () {
return runMochaWatchJSONAsync(
[testFile, "--watch-files", "lib"],
{ cwd: tempDir, expectedRuns: expectedRunCount },
- async (mochaProcess, { waitForRuns }) => {
+ async (mochaProcess, { waitForRuns, runCount }) => {
fs.mkdirSync(libPath);
await waitForRuns(2); // wait for second run
- await sleep(graceMs); // grace
+ await sleep(graceMs); // let the watch on `lib` install
fs.mkdirSync(dirPath);
await waitForRuns(3); // wait for third run
- await sleep(graceMs);
- touchFile(watchedFile);
+ // re-touch until the new subdir's watch catches the change, only re-touches when idle (no rerun happened) so it can not abort a run
+ while (runCount() < expectedRunCount) {
+ touchFile(watchedFile);
+ await sleep(graceMs);
+ }
},
).then((results) => {
expect(results, "to have length", expectedRunCount);