Skip to content

Commit 2535515

Browse files
committed
feat: replace sane with chokidar
1 parent 621b8ea commit 2535515

8 files changed

Lines changed: 53 additions & 278 deletions

File tree

.eslintrc.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ module.exports = {
5252
'packages/jest-core/src/plugins/update_snapshots_interactive.ts',
5353
'packages/jest-fake-timers/src/legacyFakeTimers.ts',
5454
'packages/jest-haste-map/src/index.ts',
55-
'packages/jest-haste-map/src/lib/FSEventsWatcher.ts',
5655
'packages/jest-jasmine2/src/jasmine/SpyStrategy.ts',
5756
'packages/jest-jasmine2/src/jasmine/Suite.ts',
5857
'packages/jest-leak-detector/src/index.ts',

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
### Chore & Maintenance
1414

15+
- `[jest-haste-map]` Replace `sane` with `chokidar` ([#10048](https://github.com/facebook/jest/pull/10048))
16+
1517
### Performance
1618

1719
## 26.4.2

packages/jest-haste-map/package.json

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,21 @@
1313
"@jest/types": "^26.3.0",
1414
"@types/graceful-fs": "^4.1.2",
1515
"@types/node": "*",
16-
"anymatch": "^3.0.3",
16+
"chokidar": "^3.4.2",
1717
"fb-watchman": "^2.0.0",
1818
"graceful-fs": "^4.2.4",
1919
"jest-regex-util": "^26.0.0",
2020
"jest-serializer": "^26.3.0",
2121
"jest-util": "^26.3.0",
2222
"jest-worker": "^26.3.0",
23-
"micromatch": "^4.0.2",
24-
"sane": "^4.0.3",
25-
"walker": "^1.0.7"
23+
"micromatch": "^4.0.2"
2624
},
2725
"devDependencies": {
2826
"@jest/test-utils": "^26.3.0",
29-
"@types/anymatch": "^1.3.1",
3027
"@types/fb-watchman": "^2.0.0",
3128
"@types/micromatch": "^4.0.0",
32-
"@types/sane": "^2.0.0",
3329
"slash": "^3.0.0"
3430
},
35-
"optionalDependencies": {
36-
"fsevents": "^2.1.2"
37-
},
3831
"engines": {
3932
"node": ">= 10.14.2"
4033
},

packages/jest-haste-map/src/__tests__/index.test.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,13 @@ jest.mock('../crawlers/watchman', () =>
7272
const mockWatcherConstructor = jest.fn(root => {
7373
const EventEmitter = require('events').EventEmitter;
7474
mockEmitters[root] = new EventEmitter();
75-
mockEmitters[root].close = jest.fn(callback => callback());
75+
mockEmitters[root].close = jest.fn();
7676
setTimeout(() => mockEmitters[root].emit('ready'), 0);
7777
return mockEmitters[root];
7878
});
7979

80-
jest.mock('sane', () => ({
81-
NodeWatcher: mockWatcherConstructor,
82-
WatchmanWatcher: mockWatcherConstructor,
80+
jest.mock('chokidar', () => ({
81+
watch: jest.fn((patten, opts) => mockWatcherConstructor(opts.cwd)),
8382
}));
8483

8584
jest.mock('../lib/WatchmanWatcher', () => mockWatcherConstructor);

packages/jest-haste-map/src/index.ts

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import {createHash} from 'crypto';
1212
import {EventEmitter} from 'events';
1313
import {tmpdir} from 'os';
1414
import * as path from 'path';
15+
import {FSWatcher as ChokidarFsWatcher, watch as chokidarWatch} from 'chokidar';
1516
import type {Stats} from 'graceful-fs';
16-
import {NodeWatcher, Watcher as SaneWatcher} from 'sane';
1717
import type {Config} from '@jest/types';
1818
import serializer from 'jest-serializer';
1919
import Worker from 'jest-worker';
@@ -31,7 +31,6 @@ import normalizePathSep from './lib/normalizePathSep';
3131
import watchmanCrawl = require('./crawlers/watchman');
3232
// @ts-expect-error: not converted to TypeScript - it's a fork: https://github.com/facebook/jest/pull/5387
3333
import WatchmanWatcher from './lib/WatchmanWatcher';
34-
import FSEventsWatcher = require('./lib/FSEventsWatcher');
3534
import * as fastPath from './lib/fast_path';
3635
import type {
3736
ChangeEvent,
@@ -98,7 +97,7 @@ type InternalOptions = {
9897
};
9998

10099
type Watcher = {
101-
close(callback: () => void): void;
100+
close(): Promise<void>;
102101
};
103102

104103
type WorkerInterface = {worker: typeof worker; getSha1: typeof getSha1};
@@ -789,14 +788,6 @@ class HasteMap extends EventEmitter {
789788
this._options.throwOnModuleCollision = false;
790789
this._options.retainAllFiles = true;
791790

792-
// WatchmanWatcher > FSEventsWatcher > sane.NodeWatcher
793-
const Watcher: SaneWatcher =
794-
canUseWatchman && this._options.useWatchman
795-
? WatchmanWatcher
796-
: FSEventsWatcher.isSupported()
797-
? FSEventsWatcher
798-
: NodeWatcher;
799-
800791
const extensions = this._options.extensions;
801792
const ignorePattern = this._options.ignorePattern;
802793
const rootDir = this._options.rootDir;
@@ -807,12 +798,21 @@ class HasteMap extends EventEmitter {
807798
let mustCopy = true;
808799

809800
const createWatcher = (root: Config.Path): Promise<Watcher> => {
810-
// @ts-expect-error: TODO how? "Cannot use 'new' with an expression whose type lacks a call or construct signature."
811-
const watcher = new Watcher(root, {
812-
dot: true,
813-
glob: extensions.map(extension => '**/*.' + extension),
814-
ignored: ignorePattern,
815-
});
801+
const useWatchman = canUseWatchman && this._options.useWatchman;
802+
const patterns = extensions.map(extension => '**/*.' + extension);
803+
// Prefer Watchman over Chokidar
804+
const watcher = useWatchman
805+
? new WatchmanWatcher(root, {
806+
dot: true,
807+
glob: patterns,
808+
ignored: ignorePattern,
809+
})
810+
: chokidarWatch(patterns, {
811+
alwaysStat: true,
812+
cwd: root,
813+
ignoreInitial: true,
814+
ignored: ignorePattern,
815+
});
816816

817817
return new Promise((resolve, reject) => {
818818
const rejectTimeout = setTimeout(
@@ -822,7 +822,14 @@ class HasteMap extends EventEmitter {
822822

823823
watcher.once('ready', () => {
824824
clearTimeout(rejectTimeout);
825-
watcher.on('all', onChange);
825+
826+
if (useWatchman) {
827+
watcher.on('all', onChange);
828+
} else {
829+
(watcher as ChokidarFsWatcher).on('all', (type, filePath, stat) => {
830+
onChange(type, filePath, root, stat);
831+
});
832+
}
826833
resolve(watcher);
827834
});
828835
});
@@ -833,10 +840,7 @@ class HasteMap extends EventEmitter {
833840
mustCopy = true;
834841
const changeEvent: ChangeEvent = {
835842
eventsQueue,
836-
hasteFS: new HasteFS({
837-
files: hasteMap.files,
838-
rootDir,
839-
}),
843+
hasteFS: new HasteFS({files: hasteMap.files, rootDir}),
840844
moduleMap: new HasteModuleMap({
841845
duplicates: hasteMap.duplicates,
842846
map: hasteMap.map,
@@ -1052,20 +1056,16 @@ class HasteMap extends EventEmitter {
10521056
}
10531057
}
10541058

1055-
end(): Promise<void> {
1059+
async end(): Promise<void> {
10561060
// @ts-expect-error: TODO TS cannot decide if `setInterval` and `clearInterval` comes from NodeJS or the DOM
10571061
clearInterval(this._changeInterval);
10581062
if (!this._watchers.length) {
1059-
return Promise.resolve();
1063+
return;
10601064
}
10611065

1062-
return Promise.all(
1063-
this._watchers.map(
1064-
watcher => new Promise(resolve => watcher.close(resolve)),
1065-
),
1066-
).then(() => {
1067-
this._watchers = [];
1068-
});
1066+
await Promise.all(this._watchers.map(watcher => watcher.close()));
1067+
1068+
this._watchers = [];
10691069
}
10701070

10711071
/**

packages/jest-haste-map/src/lib/FSEventsWatcher.ts

Lines changed: 0 additions & 192 deletions
This file was deleted.

packages/jest-haste-map/src/lib/WatchmanWatcher.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,14 +278,12 @@ WatchmanWatcher.prototype.emitEvent = function (
278278
/**
279279
* Closes the watcher.
280280
*
281-
* @param {function} callback
282-
* @private
283281
*/
284282

285-
WatchmanWatcher.prototype.close = function (callback) {
283+
WatchmanWatcher.prototype.close = function () {
286284
this.client.removeAllListeners();
287285
this.client.end();
288-
callback && callback(null, true);
286+
return Promise.resolve();
289287
};
290288

291289
/**

0 commit comments

Comments
 (0)