Skip to content

Commit 22226cd

Browse files
committed
feat: replace sane with chokidar
1 parent 245a582 commit 22226cd

8 files changed

Lines changed: 42 additions & 286 deletions

File tree

.eslintrc.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ module.exports = {
9595
'packages/jest-core/src/plugins/UpdateSnapshotsInteractive.ts',
9696
'packages/jest-fake-timers/src/legacyFakeTimers.ts',
9797
'packages/jest-haste-map/src/index.ts',
98-
'packages/jest-haste-map/src/lib/FSEventsWatcher.ts',
9998
'packages/jest-jasmine2/src/jasmine/SpyStrategy.ts',
10099
'packages/jest-jasmine2/src/jasmine/Suite.ts',
101100
'packages/jest-leak-detector/src/index.ts',

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- `[jest-circus]` Fixed the issue of beforeAll & afterAll hooks getting executed even if it is inside a skipped `describe` block [#10451](https://github.com/facebook/jest/issues/10451)
1717
- `[jest-circus]` Fix `testLocation` on Windows when using `test.each` ([#10871](https://github.com/facebook/jest/pull/10871))
1818
- `[jest-console]` `console.dir` now respects the second argument correctly ([#10638](https://github.com/facebook/jest/pull/10638))
19+
- `[jest-haste-map]` Replace `sane` with `chokidar` ([#10048](https://github.com/facebook/jest/pull/10048))
1920
- `[jest-jasmine2]` Fixed the issue of beforeAll & afterAll hooks getting executed even if it is inside a skipped `describe` block when it has child `tests` marked as either `only` or `todo` [#10451](https://github.com/facebook/jest/issues/10451)
2021
- `[jest-jasmine2]` Fixed the issues of child `tests` marked with `only` or `todo` getting executed even if it is inside a skipped parent `describe` block [#10451](https://github.com/facebook/jest/issues/10451)
2122
- `[jest-reporter]` Handle empty files when reporting code coverage with V8 ([#10819](https://github.com/facebook/jest/pull/10819))

packages/jest-haste-map/package.json

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,22 @@
1717
"@jest/types": "^26.6.2",
1818
"@types/graceful-fs": "^4.1.2",
1919
"@types/node": "*",
20-
"anymatch": "^3.0.3",
20+
"chokidar": "^3.4.2",
2121
"fb-watchman": "^2.0.0",
2222
"graceful-fs": "^4.2.4",
2323
"jest-regex-util": "^26.0.0",
2424
"jest-serializer": "^26.6.2",
2525
"jest-util": "^26.6.2",
2626
"jest-worker": "^26.6.2",
27-
"micromatch": "^4.0.2",
28-
"sane": "^4.0.3",
29-
"walker": "^1.0.7"
27+
"micromatch": "^4.0.2"
3028
},
3129
"devDependencies": {
3230
"@jest/test-utils": "^26.6.2",
33-
"@types/anymatch": "^1.3.1",
3431
"@types/fb-watchman": "^2.0.0",
3532
"@types/micromatch": "^4.0.0",
36-
"@types/sane": "^2.0.0",
3733
"jest-snapshot-serializer-raw": "^1.1.0",
3834
"slash": "^3.0.0"
3935
},
40-
"optionalDependencies": {
41-
"fsevents": "^2.1.2"
42-
},
4336
"engines": {
4437
"node": "^10.13.0 || ^12.13.0 || ^14.15.0 || >=15.0.0"
4538
},

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 {escapePathForRegex} from 'jest-regex-util';
1919
import serializer from 'jest-serializer';
@@ -26,7 +26,6 @@ import H from './constants';
2626
import nodeCrawl = require('./crawlers/node');
2727
import watchmanCrawl = require('./crawlers/watchman');
2828
import getMockName from './getMockName';
29-
import FSEventsWatcher = require('./lib/FSEventsWatcher');
3029
// @ts-expect-error: not converted to TypeScript - it's a fork: https://github.com/facebook/jest/pull/5387
3130
import WatchmanWatcher from './lib/WatchmanWatcher';
3231
import * as fastPath from './lib/fast_path';
@@ -101,7 +100,7 @@ type InternalOptions = {
101100
};
102101

103102
type Watcher = {
104-
close(callback: () => void): void;
103+
close(): Promise<void>;
105104
};
106105

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

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

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

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

822822
watcher.once('ready', () => {
823823
clearTimeout(rejectTimeout);
824-
watcher.on('all', onChange);
824+
825+
if (useWatchman) {
826+
watcher.on('all', onChange);
827+
} else {
828+
(watcher as ChokidarFsWatcher).on('all', (type, filePath, stat) => {
829+
onChange(type, filePath, root, stat);
830+
});
831+
}
825832
resolve(watcher);
826833
});
827834
});
@@ -832,10 +839,7 @@ class HasteMap extends EventEmitter {
832839
mustCopy = true;
833840
const changeEvent: ChangeEvent = {
834841
eventsQueue,
835-
hasteFS: new HasteFS({
836-
files: hasteMap.files,
837-
rootDir,
838-
}),
842+
hasteFS: new HasteFS({files: hasteMap.files, rootDir}),
839843
moduleMap: new HasteModuleMap({
840844
duplicates: hasteMap.duplicates,
841845
map: hasteMap.map,
@@ -1051,20 +1055,16 @@ class HasteMap extends EventEmitter {
10511055
}
10521056
}
10531057

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

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

10701070
/**

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)