☕️ Classic, reliable, trusted test framework for Node.js and the browser
fix: keep watching when the last test file is removed (#6355)
Mostafa Nazari921c161f90a0
diff --git a/lib/cli/collect-files.cjs b/lib/cli/collect-files.cjsindex 0440fbfb..2626816c 100644--- a/lib/cli/collect-files.cjs+++ b/lib/cli/collect-files.cjs@@ -28,6 +28,7 @@ const { castArray } = require("../utils.cjs");* @private*/module.exports = ({+ allowEmpty,ignore,extension,file: fileArgs,@@ -94,11 +95,17 @@ module.exports = ({// give full message details when only 1 file is missingconst noneFoundMsg =unmatchedSpecFiles.length === 1- ? `Error: No test files found: ${JSON.stringify(+ ? `No test files found: ${JSON.stringify(unmatchedSpecFiles[0].pattern,)}` // stringify to print escaped characters raw- : "Error: No test files found";- console.error(pc.red(noneFoundMsg));+ : "No test files found";++ if (allowEmpty) {+ console.warn(pc.yellow(`Warning: ${noneFoundMsg}`));+ return { files: [], unmatchedFiles: [] };+ }++ console.error(pc.red(`Error: ${noneFoundMsg}`));process.exit(1);} else {// print messages as a warningdiff --git a/lib/cli/run-helpers.cjs b/lib/cli/run-helpers.cjsindex b7607ad6..8b855f40 100644--- a/lib/cli/run-helpers.cjs+++ b/lib/cli/run-helpers.cjs@@ -229,6 +229,7 @@ exports.runMocha = async (mocha, options) => {} = options;const fileCollectParams = {+ allowEmpty: watch,ignore,extension,file,diff --git a/lib/nodejs/file-unloader.js b/lib/nodejs/file-unloader.jsindex e97977d0..5d130897 100644--- a/lib/nodejs/file-unloader.js+++ b/lib/nodejs/file-unloader.js@@ -10,8 +10,18 @@ const require = createRequire(import.meta.url);/*** Deletes a file from the `require` cache.+ * A file that no longer exists on disk has nothing left to unload.* @param {string} file - File*/export function unloadFile(file) {- delete require.cache[require.resolve(file)];+ let resolved;+ try {+ resolved = require.resolve(file);+ } catch (err) {+ if (err.code === "MODULE_NOT_FOUND") {+ return;+ }+ throw err;+ }+ delete require.cache[resolved];}diff --git a/lib/types.d.ts b/lib/types.d.tsindex 433aafca..41caffd5 100644--- a/lib/types.d.ts+++ b/lib/types.d.ts@@ -194,6 +194,8 @@ export type PluginFinalizer = (* An object to configure how Mocha gathers test files*/export interface FileCollectionOptions {+ /** Return an empty list instead of exiting when no test files are found */+ allowEmpty?: boolean;/** File extensions to use */extension?: string[];/** Files, dirs, globs to run */diff --git a/test/integration/options/watch.spec.cjs b/test/integration/options/watch.spec.cjsindex 65dcc9fe..8d9e53f6 100644--- a/test/integration/options/watch.spec.cjs+++ b/test/integration/options/watch.spec.cjs@@ -67,6 +67,26 @@ describe("--watch", function () {});});+ it("keeps watching when the last test file is removed", function () {+ const testFile = path.join(tempDir, "test/a.js");+ copyFixture(DEFAULT_FIXTURE, testFile);++ return runMochaWatchJSONAsync(+ ["test/**/*.js", "--watch-files", "test/**/*.js"],+ { cwd: tempDir, expectedRuns: 3 },+ async (_mochaProcess, { waitForRuns }) => {+ fs.rmSync(testFile, { force: true });+ await waitForRuns(2);+ copyFixture("passing.fixture.cjs", path.join(tempDir, "test/b.js"));+ },+ ).then((results) => {+ expect(results, "to have length", 3);+ expect(results[0].passes, "to have length", 1);+ expect(results[1].passes, "to have length", 0);+ expect(results[2].passes, "to have length", 2);+ });+ });+describe("when in parallel mode", function () {it("reruns test when watched test file is touched", function () {const testFile = path.join(tempDir, "test.js");@@ -78,6 +98,26 @@ describe("--watch", function () {expect(results, "to have length", 2);});});++ it("keeps watching when the last test file is removed", function () {+ const testFile = path.join(tempDir, "test/a.js");+ copyFixture(DEFAULT_FIXTURE, testFile);++ return runMochaWatchJSONAsync(+ ["--parallel", "test/**/*.js", "--watch-files", "test/**/*.js"],+ { cwd: tempDir, expectedRuns: 3 },+ async (_mochaProcess, { waitForRuns }) => {+ fs.rmSync(testFile, { force: true });+ await waitForRuns(2);+ copyFixture("passing.fixture.cjs", path.join(tempDir, "test/b.js"));+ },+ ).then((results) => {+ expect(results, "to have length", 3);+ expect(results[0].passes, "to have length", 1);+ expect(results[1].passes, "to have length", 0);+ expect(results[2].passes, "to have length", 2);+ });+ });});it("reruns test when file matching --watch-files changes", function () {