Skip to content

Commit

Permalink
test: switch to node test runner
Browse files Browse the repository at this point in the history
Switches to using the built-in node test runner and drops mocha.

Temporarily also introduces a `TEST_TIMEOUT` as `--test-timeout` is only
available in 20.x and above, but we run CI in 18.x.
  • Loading branch information
43081j committed May 26, 2024
1 parent ae0232b commit b879371
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 14 deletions.
24 changes: 18 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
"version": "4.0.0-alpha",
"homepage": "https://github.com/paulmillr/chokidar",
"author": "Paul Miller (https://paulmillr.com)",
"contributors": ["Paul Miller (https://paulmillr.com)", "Elan Shanker"],
"contributors": [
"Paul Miller (https://paulmillr.com)",
"Elan Shanker"
],
"engines": {
"node": ">= 18"
},
Expand All @@ -21,14 +24,16 @@
"@typescript-eslint/eslint-plugin": "6.21.0",
"chai": "4.3.4",
"eslint": "8.7.0",
"mocha": "9.1.4",
"rimraf": "5.0.5",
"sinon": "12.0.1",
"sinon-chai": "3.7.0",
"typescript": "5.4.2",
"upath": "2.0.1"
},
"files": ["lib/*.js", "lib/*.d.ts"],
"files": [
"lib/*.js",
"lib/*.d.ts"
],
"repository": {
"type": "git",
"url": "git+https://github.com/paulmillr/chokidar.git"
Expand All @@ -40,9 +45,16 @@
"scripts": {
"build": "tsc",
"lint": "eslint --ext=ts --report-unused-disable-directives --ignore-path .gitignore .",
"mocha": "mocha --exit --timeout 90000",
"test": "npm run build && npm run lint && npm run mocha"
"test": "npm run build && npm run lint && node --test"
},
"keywords": ["fs", "watch", "watchFile", "watcher", "watching", "file", "fsevents"],
"keywords": [
"fs",
"watch",
"watchFile",
"watcher",
"watching",
"file",
"fsevents"
],
"funding": "https://paulmillr.com/funding/"
}
42 changes: 34 additions & 8 deletions test.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-env mocha */

import fs from 'node:fs';
import sysPath from 'node:path';
import {describe, it, before, after, beforeEach, afterEach} from 'node:test';
import {fileURLToPath, pathToFileURL} from 'node:url';
import {promisify} from 'node:util';
import childProcess from 'node:child_process';
Expand Down Expand Up @@ -37,6 +36,7 @@ const FIXTURES_PATH_REL = 'test-fixtures';
const FIXTURES_PATH = sysPath.join(__dirname, FIXTURES_PATH_REL);
const allWatchers = [];
const PERM_ARR = 0o755; // rwe, r+e, r+e
const TEST_TIMEOUT = 8000;
let subdirId = 0;
let options;
let currentDir;
Expand All @@ -54,16 +54,34 @@ const aspy = (watcher, eventName, spy = null, noStat = false) => {
(event, path) => spy(event, path) :
(path) => spy(path)) :
spy;
watcher.on(EV.ERROR, reject);
watcher.on(EV.READY, () => resolve(spy));
const timeout = setTimeout(() => {
reject(new Error('timeout'));
}, TEST_TIMEOUT);
watcher.on(EV.ERROR, (...args) => {
clearTimeout(timeout);
reject(...args);
});
watcher.on(EV.READY, () => {
clearTimeout(timeout);
resolve(spy);
});
watcher.on(eventName, handler);
});
};

const waitForWatcher = (watcher) => {
return new Promise((resolve, reject) => {
watcher.on(EV.ERROR, reject);
watcher.on(EV.READY, resolve);
const timeout = setTimeout(() => {
reject(new Error('timeout'));
}, TEST_TIMEOUT);
watcher.on(EV.ERROR, (...args) => {
clearTimeout(timeout);
reject(...args);
});
watcher.on(EV.READY, (...args) => {
clearTimeout(timeout);
resolve(...args);
});
});
};

Expand Down Expand Up @@ -92,7 +110,10 @@ const chokidar_watch = (path = currentDir, opts = options) => {

const waitFor = async (spies) => {
if (spies.length === 0) throw new TypeError('SPies zero');
return new Promise((resolve) => {
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject(new Error('timeout'));
}, TEST_TIMEOUT);
const isSpyReady = (spy) => {
if (Array.isArray(spy)) {
return spy[0].callCount >= spy[1];
Expand All @@ -101,6 +122,7 @@ const waitFor = async (spies) => {
};
const checkSpiesReady = () => {
if (spies.every(isSpyReady)) {
clearTimeout(timeout);
resolve();
} else {
setTimeout(checkSpiesReady, 20);
Expand All @@ -111,13 +133,17 @@ const waitFor = async (spies) => {
};

const waitForEvents = (watcher, count) => {
return new Promise((resolve) => {
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject(new Error('timeout'));
}, TEST_TIMEOUT);
const events = [];
const handler = (event, path) => {
events.push(`[ALL] ${event}: ${path}`)

if (events.length === count) {
watcher.off('all', handler);
clearTimeout(timeout);
resolve(events);
}
};
Expand Down

0 comments on commit b879371

Please sign in to comment.