☕️ Classic, reliable, trusted test framework for Node.js and the browser
fix: watch mode using chokidar v4 (#5379)
Jefferson Ryleec2667c3b3fca
diff --git a/lib/cli/watch-run.js b/lib/cli/watch-run.jsindex 6150ec33..5fc1f7d6 100644--- a/lib/cli/watch-run.js+++ b/lib/cli/watch-run.js@@ -4,16 +4,24 @@ const logSymbols = require('log-symbols');const debug = require('debug')('mocha:cli:watch');const path = require('node:path');const chokidar = require('chokidar');+const glob = require('glob');+const isPathInside = require('is-path-inside');+const {minimatch} = require('minimatch');const Context = require('../context');const collectFiles = require('./collect-files');-const glob = require('glob');/*** @typedef {import('chokidar').FSWatcher} FSWatcher+ * @typedef {import('glob').Glob['patterns'][number]} Pattern+ * The `Pattern` class is not exported by the `glob` package.+ * Ref [link](../../node_modules/glob/dist/commonjs/pattern.d.ts).* @typedef {import('../mocha.js')} Mocha* @typedef {import('../types.d.ts').BeforeWatchRun} BeforeWatchRun* @typedef {import('../types.d.ts').FileCollectionOptions} FileCollectionOptions* @typedef {import('../types.d.ts').Rerunner} Rerunner+ * @typedef {import('../types.d.ts').PathPattern} PathPattern+ * @typedef {import('../types.d.ts').PathFilter} PathFilter+ * @typedef {import('../types.d.ts').PathMatcher} PathMatcher*//**@@ -145,44 +153,240 @@ exports.watchRun = (mocha, {watchFiles, watchIgnore}, fileCollectParams) => {});};-class GlobFilesTracker {- constructor(watchFiles, watchIgnore) {- this.watchFilesSet = new Set();- this.watchFiles = watchFiles;- this.watchIgnore = watchIgnore;- }+/**+ * Extracts out paths without the glob part, the directory paths,+ * and the paths for matching from the provided glob paths.+ * @param {string[]} globPaths The list of glob paths to create a filter for.+ * @param {string} basePath The path where mocha is run (e.g., current working directory).+ * @returns {PathFilter} Object to filter paths.+ * @ignore+ * @private+ */+function createPathFilter(globPaths, basePath) {+ debug('creating path filter from glob paths: %s', globPaths);++ /**+ * The resulting object to filter paths.+ * @type {PathFilter}+ */+ const res = {+ dir: {paths: new Set(), globs: new Set()},+ match: {paths: new Set(), globs: new Set()}+ };- regenerate() {- const watchIgnoreSet = new Set();- for (const pattern of this.watchIgnore) {- glob.sync(pattern, { dot: true }).forEach(filePath => watchIgnoreSet.add(filePath));- }+ // for checking if a path ends with `/**/*`+ const globEnd = path.join(path.sep, '**', '*');- const globOpts = {+ /**+ * The current glob pattern to check.+ * @type {Pattern[]}+ */+ const patterns = globPaths.flatMap(globPath => {+ return new glob.Glob(globPath, {dot: true,- ignore: {- childrenIgnored: pathToCheck => watchIgnoreSet.has(pathToCheck.relative())+ magicalBraces: true,+ windowsPathsNoEscape: true+ }).patterns;+ }, []);++ // each pattern will have its own path because of the `magicalBraces` option+ for (const pattern of patterns) {+ debug('processing glob pattern: %s', pattern.globString());++ /**+ * Path segments before the glob pattern.+ * @type {string[]}+ */+ const segments = [];++ /**+ * The current glob pattern to check.+ * @type {Pattern | null}+ */+ let currentPattern = pattern;+ let isGlob = false;++ do {+ // save string patterns until a non-string (glob or regexp) is matched+ const entry = currentPattern.pattern();+ const isString = typeof entry === 'string';+ debug(+ 'found %s pattern: %s',+ isString ? 'string' : 'glob or regexp',+ entry+ );+ if (!isString) {+ // if the entry is a glob+ isGlob = true;+ break;}- };- this.watchFilesSet.clear();- for (const pattern of this.watchFiles) {- glob.sync(pattern, globOpts).forEach(pathToCheck => {- if (watchIgnoreSet.has(pathToCheck)) {- return;- }- this.watchFilesSet.add(pathToCheck);- });+ segments.push(entry);++ // go to next pattern+ } while ((currentPattern = currentPattern.rest()));+ if (!isGlob) {+ debug('all subpatterns of %j processed', pattern.globString());+ }++ // match `cleanPath` (path without the glob part) and its subdirectories+ const cleanPath = path.resolve(basePath, ...segments);+ debug('clean path: %s', cleanPath);+ res.dir.paths.add(cleanPath);+ res.dir.globs.add(path.resolve(cleanPath, '**', '*'));++ // match `absPath` and all of its contents+ const absPath = path.resolve(basePath, pattern.globString());+ debug('absolute path: %s', absPath);+ (isGlob ? res.match.globs : res.match.paths).add(absPath);++ // always include `/**/*` to the full pattern for matching+ // since it's possible for the last path segment to be a directory+ if (!absPath.endsWith(globEnd)) {+ res.match.globs.add(path.resolve(absPath, '**', '*'));}}- has(filePath) {- return this.watchFilesSet.has(filePath)+ debug('returning path filter: %o', res);+ return res;+}++/**+ * Checks if the provided path matches with the path pattern.+ * @param {string} filePath The path to match.+ * @param {PathPattern} pattern The path pattern for matching.+ * @param {boolean} [matchParent] Treats the provided path as a match if it's a valid parent directory from the list of paths.+ * @returns {boolean} Determines if the provided path matches the pattern.+ * @ignore+ * @private+ */+function matchPattern(filePath, pattern, matchParent) {+ if (pattern.paths.has(filePath)) {+ return true;+ }++ if (matchParent) {+ for (const childPath of pattern.paths) {+ if (isPathInside(childPath, filePath)) {+ return true;+ }+ }+ }++ // loop through the set of glob paths instead of converting it into an array+ for (const globPath of pattern.globs) {+ if (+ minimatch(filePath, globPath, {dot: true, windowsPathsNoEscape: true})+ ) {+ return true;+ }}++ return false;+}++/**+ * Creates an object for matching allowed or ignored file paths.+ * @param {PathFilter} allowed The filter for allowed paths.+ * @param {PathFilter} ignored The filter for ignored paths.+ * @param {string} basePath The path where mocha is run (e.g., current working directory).+ * @returns {PathMatcher} The object for matching paths.+ * @ignore+ * @private+ */+function createPathMatcher(allowed, ignored, basePath) {+ debug(+ 'creating path matcher from allowed: %o, ignored: %o',+ allowed,+ ignored+ );++ /**+ * Cache of known file paths processed by `matcher.allow()`.+ * @type {Map<string, boolean>}+ */+ const allowCache = new Map();++ /**+ * Cache of known file paths processed by `matcher.ignore()`.+ * @type {Map<string, boolean>}+ */+ const ignoreCache = new Map();++ const MAX_CACHE_SIZE = 10000;++ /**+ * Performs a `map.set()` but will delete the first key+ * for new key-value pairs whenever the limit is reached.+ * @param {Map<string, boolean>} map The map to use.+ * @param {string} key The key to use.+ * @param {boolean} value The value to set.+ */+ function cache(map, key, value) {+ // only delete the first key if the key doesn't exist in the map+ if (map.size >= MAX_CACHE_SIZE && !map.has(key)) {+ map.delete(map.keys().next().value);+ }+ map.set(key, value);+ }++ /**+ * @type {PathMatcher}+ */+ const matcher = {+ allow(filePath) {+ let allow = allowCache.get(filePath);+ if (allow !== undefined) {+ return allow;+ }++ allow = matchPattern(filePath, allowed.match);+ cache(allowCache, filePath, allow);+ return allow;+ },++ ignore(filePath, stats) {+ // Chokidar calls the ignore match function twice:+ // once without `stats` and again with `stats`+ // see `ignored` under https://github.com/paulmillr/chokidar?tab=readme-ov-file#path-filtering+ // note that the second call can also have no `stats` if the `filePath` does not exist+ // in which case, allow the nonexistent path since it may be created later+ if (!stats) {+ return false;+ }++ // resolve to ensure correct absolute path since, for some reason,+ // Chokidar paths for the ignore match function use slashes `/` even for Windows+ filePath = path.resolve(basePath, filePath);++ let ignore = ignoreCache.get(filePath);+ if (ignore !== undefined) {+ return ignore;+ }++ // `filePath` ignore conditions:+ // - check if it's ignored from the `ignored` path patterns+ // - otherwise, check if it's not ignored via `matcher.allow()` to also cache the result+ // - if no match was found and `filePath` is a directory,+ // check from the allowed directory paths if it's a valid+ // parent directory or if it matches any of the allowed patterns+ // since ignoring directories will have Chokidar ignore their contents+ // which we may need to watch changes for+ ignore =+ matchPattern(filePath, ignored.match) ||+ (!matcher.allow(filePath) &&+ (!stats.isDirectory() || !matchPattern(filePath, allowed.dir, true)));++ cache(ignoreCache, filePath, ignore);+ return ignore;+ }+ };++ return matcher;}/**- * Bootstraps a chokidar watcher. Handles keyboard input & signals+ * Bootstraps a Chokidar watcher. Handles keyboard input & signals* @param {Mocha} mocha - Mocha instance* @param {Object} opts* @param {BeforeWatchRun} [opts.beforeRun] - Function to call before@@ -206,17 +410,28 @@ const createWatcher = (watchFiles = fileCollectParams.extension.map(ext => `**/*.${ext}`);}+ debug('watching files: %s', watchFiles);debug('ignoring files matching: %s', watchIgnore);let globalFixtureContext;// we handle global fixtures manuallymocha.enableGlobalSetup(false).enableGlobalTeardown(false);- const tracker = new GlobFilesTracker(watchFiles, watchIgnore);- tracker.regenerate();-- const watcher = chokidar.watch('.', {- ignoreInitial: true+ // glob file paths are no longer supported by Chokidar since v4+ // first, strip the glob paths from `watchFiles` for Chokidar to watch+ // then, create path patterns from `watchFiles` and `watchIgnore`+ // to determine if the files should be allowed or ignored+ // by the Chokidar `ignored` match function++ const basePath = process.cwd();+ const allowed = createPathFilter(watchFiles, basePath);+ const ignored = createPathFilter(watchIgnore, basePath);+ const matcher = createPathMatcher(allowed, ignored, basePath);++ // Chokidar has to watch the directory paths in case new files are created+ const watcher = chokidar.watch(Array.from(allowed.dir.paths), {+ ignoreInitial: true,+ ignored: matcher.ignore});const rerunner = createRerunner(mocha, watcher, {@@ -224,6 +439,7 @@ const createWatcher = (});watcher.on('ready', async () => {+ debug('watcher ready');if (!globalFixtureContext) {debug('triggering global setup');globalFixtureContext = await mocha.runGlobalSetup();@@ -231,11 +447,9 @@ const createWatcher = (rerunner.run();});- watcher.on('all', (event, filePath) => {- if (event === 'add') {- tracker.regenerate();- }- if (tracker.has(filePath)) {+ watcher.on('all', (_event, filePath) => {+ // only allow file paths that match the allowed patterns+ if (matcher.allow(filePath)) {rerunner.scheduleRun();}});@@ -294,7 +508,7 @@ const createWatcher = (* Create an object that allows you to rerun tests on the mocha instance.** @param {Mocha} mocha - Mocha instance- * @param {FSWatcher} watcher - chokidar `FSWatcher` instance+ * @param {FSWatcher} watcher - Chokidar `FSWatcher` instance* @param {Object} [opts] - Options!* @param {BeforeWatchRun} [opts.beforeRun] - Function to call before `mocha.run()`* @returns {Rerunner}@@ -353,9 +567,9 @@ const createRerunner = (mocha, watcher, {beforeRun} = {}) => {};/**- * Return the list of absolute paths watched by a chokidar watcher.+ * Return the list of absolute paths watched by a Chokidar watcher.*- * @param watcher - Instance of a chokidar watcher+ * @param watcher - Instance of a Chokidar watcher* @return {string[]} - List of absolute paths* @ignore* @private@@ -399,7 +613,7 @@ const eraseLine = () => {/*** Blast all of the watched files out of `require.cache`- * @param {FSWatcher} watcher - chokidar FSWatcher+ * @param {FSWatcher} watcher - Chokidar FSWatcher* @ignore* @private*/diff --git a/lib/types.d.ts b/lib/types.d.tsindex 8da230d2..800aa04b 100644--- a/lib/types.d.ts+++ b/lib/types.d.ts@@ -4,7 +4,7 @@* For now, if you're an external user, you should use the types from @types/mocha.*/-import type {FSWatcher} from 'chokidar';+import type {FSWatcher, MatchFunction} from 'chokidar';import type {constants} from './error-constants.js';import type Mocha from './mocha.js';@@ -252,6 +252,47 @@ export interface Rerunner {scheduleRun: Function;}+/**+ * Object containing paths (without globs) and glob paths for matching.+ * @private+ */+export interface PathPattern {+ /** Set of absolute paths without globs. */+ paths: Set<string>;+ /** Set of absolute glob paths. */+ globs: Set<string>;+}++/**+ * Object containing path patterns for filtering from the provided glob paths.+ * @private+ */+export interface PathFilter {+ /** Path patterns for directories. */+ dir: PathPattern;+ /** Path patterns for matching. */+ match: PathPattern;+}++/**+ * Checks if the file path matches the allowed patterns.+ * @private+ * @param filePath The file path to check.+ * @returns Determines if there was a match.+ */+export type AllowMatchFunction = (filePath: string) => boolean;++/**+ * Object for matching paths to either allow or ignore them.+ * @private+ */+export interface PathMatcher {+ /** Checks if the file path matches the allowed patterns. */+ allow: AllowMatchFunction;+ /** The Chokidar `ignored` match function. */+ ignore: MatchFunction;+}+export interface StatsCollector {/** integer count of suites run */suites: number;diff --git a/package-lock.json b/package-lock.jsonindex 198342ee..df3c77af 100644--- a/package-lock.json+++ b/package-lock.json@@ -17,6 +17,7 @@"find-up": "^5.0.0","glob": "^10.4.5","he": "^1.2.0",+ "is-path-inside": "^3.0.3","js-yaml": "^4.1.0","log-symbols": "^4.1.0","minimatch": "^9.0.5",@@ -8661,7 +8662,7 @@"version": "3.0.3","resolved": "https://registry.npmjs.org/is-path-inside/-/is-path-inside-3.0.3.tgz","integrity": "sha512-Fd4gABb+ycGAmKou8eMftCupSir5lRxqf4aD/vd0cD2qc4HL07OjCeuHMr8Ro4CoMaeCKDB0/ECBOVWjTwUvPQ==",- "dev": true,+ "license": "MIT","engines": {"node": ">=8"}@@ -23548,8 +23549,7 @@"is-path-inside": {"version": "3.0.3","resolved": "https://registry.npmjs.org/is-path-inside/-/is-path-inside-3.0.3.tgz",- "integrity": "sha512-Fd4gABb+ycGAmKou8eMftCupSir5lRxqf4aD/vd0cD2qc4HL07OjCeuHMr8Ro4CoMaeCKDB0/ECBOVWjTwUvPQ==",- "dev": true+ "integrity": "sha512-Fd4gABb+ycGAmKou8eMftCupSir5lRxqf4aD/vd0cD2qc4HL07OjCeuHMr8Ro4CoMaeCKDB0/ECBOVWjTwUvPQ=="},"is-plain-obj": {"version": "1.1.0",diff --git a/package.json b/package.jsonindex 2ce6aab2..af5dce3d 100644--- a/package.json+++ b/package.json@@ -74,6 +74,7 @@"test-node-run-only": "nyc --no-clean --reporter=json node bin/mocha.js","test-node-run": "nyc --no-clean --reporter=json node bin/mocha.js --forbid-only","test-node:integration": "run-s clean build && npm run -s test-node-run -- --parallel --timeout 10000 --slow 3750 \"test/integration/**/*.spec.js\"",+ "test-node:integration:watch": "run-s clean build && npm run -s test-node-run -- --parallel --timeout 10000 --slow 3750 \"test/integration/options/watch.spec.js\"","test-node:interfaces:bdd": "npm run -s test-node-run -- --ui bdd test/interfaces/bdd.spec","test-node:interfaces:exports": "npm run -s test-node-run -- --ui exports test/interfaces/exports.spec","test-node:interfaces:qunit": "npm run -s test-node-run -- --ui qunit test/interfaces/qunit.spec",@@ -105,6 +106,7 @@"find-up": "^5.0.0","glob": "^10.4.5","he": "^1.2.0",+ "is-path-inside": "^3.0.3","js-yaml": "^4.1.0","log-symbols": "^4.1.0","minimatch": "^9.0.5",diff --git a/test/integration/options/watch.spec.js b/test/integration/options/watch.spec.jsindex 9b37ea1b..7e3759d0 100644--- a/test/integration/options/watch.spec.js+++ b/test/integration/options/watch.spec.js@@ -120,6 +120,32 @@ describe('--watch', function () {});});+ it('reruns test when file and directory paths under --watch-files are added', function () {+ const testFile = path.join(tempDir, 'test.js');+ copyFixture(DEFAULT_FIXTURE, testFile);++ // only create 'lib', 'dir', and 'file.xyz' when watching+ const libPath = path.join(tempDir, 'lib');+ const dirPath = path.join(libPath, 'dir');+ const watchedFile = path.join(dirPath, 'file.xyz');++ return runMochaWatchJSONAsync(+ [testFile, '--watch-files', 'lib'],+ tempDir,+ async () => {+ fs.mkdirSync(libPath);+ await sleep(1000);++ fs.mkdirSync(dirPath);+ await sleep(1000);++ touchFile(watchedFile);+ }+ ).then(results => {+ expect(results, 'to have length', 4);+ });+ });+it('reruns test when file matching --watch-files is removed', function () {const testFile = path.join(tempDir, 'test.js');copyFixture(DEFAULT_FIXTURE, testFile);@@ -138,6 +164,120 @@ describe('--watch', function () {});});+ it('reruns test when file matching exact --watch-files changes', function () {+ const testFile = path.join(tempDir, 'test.js');+ copyFixture(DEFAULT_FIXTURE, testFile);++ const watchedFile = path.join(tempDir, 'lib/file.xyz');+ touchFile(watchedFile);++ return runMochaWatchJSONAsync(+ [testFile, '--watch-files', 'lib/file.xyz'],+ tempDir,+ () => {+ touchFile(watchedFile);+ }+ ).then(results => {+ expect(results, 'to have length', 2);+ });+ });++ it('reruns test when file under --watch-files changes', function () {+ const testFile = path.join(tempDir, 'test.js');+ copyFixture(DEFAULT_FIXTURE, testFile);++ const watchedFile = path.join(tempDir, 'lib/dir/file.xyz');+ touchFile(watchedFile);++ return runMochaWatchJSONAsync(+ [testFile, '--watch-files', 'lib'],+ tempDir,+ () => {+ touchFile(watchedFile);+ }+ ).then(results => {+ expect(results, 'to have length', 2);+ });+ });++ it('reruns test when file matching --watch-files starting with a glob pattern changes', function () {+ const testFile = path.join(tempDir, 'test.js');+ copyFixture(DEFAULT_FIXTURE, testFile);++ const watchedFile = path.join(tempDir, 'lib/dir/file.xyz');+ touchFile(watchedFile);++ return runMochaWatchJSONAsync(+ [testFile, '--watch-files', '**/lib'],+ tempDir,+ () => {+ touchFile(watchedFile);+ }+ ).then(results => {+ expect(results, 'to have length', 2);+ });+ });++ it('reruns test when file matching --watch-files ending with a glob pattern changes', function () {+ const testFile = path.join(tempDir, 'test.js');+ copyFixture(DEFAULT_FIXTURE, testFile);++ const watchedFile = path.join(tempDir, 'lib/dir/file.xyz');+ touchFile(watchedFile);++ return runMochaWatchJSONAsync(+ [testFile, '--watch-files', 'lib/**/*'],+ tempDir,+ () => {+ touchFile(watchedFile);+ }+ ).then(results => {+ expect(results, 'to have length', 2);+ });+ });++ it('reruns test when file matching --watch-files with glob pattern in between changes', function () {+ const testFile = path.join(tempDir, 'test.js');+ copyFixture(DEFAULT_FIXTURE, testFile);++ const watchedFile = path.join(tempDir, 'lib/dir/file.xyz');+ touchFile(watchedFile);++ return runMochaWatchJSONAsync(+ [testFile, '--watch-files', 'lib/**/dir'],+ tempDir,+ () => {+ touchFile(watchedFile);+ }+ ).then(results => {+ expect(results, 'to have length', 2);+ });+ });++ it('reruns test when file matching --watch-files outside the current working directory changes', function () {+ // create a subdirectory to watch as the current working directory+ const tempCwd = path.join(tempDir, 'dir');+ const testFile = path.join(tempCwd, 'test.js');+ copyFixture(DEFAULT_FIXTURE, testFile);++ const watchedFile = path.join(tempDir, 'lib/file.xyz');+ touchFile(watchedFile);++ // use an absolute path for the watch file pattern,+ // otherwise it would be read relative to the current working directory+ const watchFilePattern = path.join(tempDir, 'lib/**/*.xyz');++ return runMochaWatchJSONAsync(+ [testFile, '--watch-files', watchFilePattern],+ tempCwd,+ () => {+ touchFile(watchedFile);+ }+ ).then(results => {+ expect(results, 'to have length', 2);+ });+ });+it('does not rerun test when file not matching --watch-files is changed', function () {const testFile = path.join(tempDir, 'test.js');copyFixture(DEFAULT_FIXTURE, testFile);@@ -267,6 +407,29 @@ describe('--watch', function () {});});+ // Workaround: escape via e.g. `dir/[[]ab[]].js`+ it('ignores files whose name is the watch glob', function () {+ const testFile = path.join(tempDir, 'test.js');+ copyFixture(DEFAULT_FIXTURE, testFile);++ const watchedFile = path.join(tempDir, 'dir/[ab].js');+ touchFile(watchedFile);++ return runMochaWatchJSONAsync(+ [+ testFile,+ '--watch-files',+ 'dir/[ab].js',+ ],+ tempDir,+ () => {+ touchFile(watchedFile);+ }+ ).then(results => {+ expect(results.length, 'to equal', 1);+ });+ });+it('reloads test files when they change', function () {const testFile = path.join(tempDir, 'test.js');copyFixture('options/watch/test-file-change', testFile);