@@ -10,8 +10,8 @@ import {createHash} from 'crypto';
1010import { EventEmitter } from 'events' ;
1111import { tmpdir } from 'os' ;
1212import * as path from 'path' ;
13+ import { FSWatcher as ChokidarFsWatcher , watch as chokidarWatch } from 'chokidar' ;
1314import type { Stats } from 'graceful-fs' ;
14- import { NodeWatcher , Watcher as SaneWatcher } from 'sane' ;
1515import type { Config } from '@jest/types' ;
1616import serializer from 'jest-serializer' ;
1717import Worker from 'jest-worker' ;
@@ -29,7 +29,6 @@ import normalizePathSep from './lib/normalizePathSep';
2929import watchmanCrawl = require( './crawlers/watchman' ) ;
3030// @ts -expect-error: not converted to TypeScript - it's a fork: https://github.com/facebook/jest/pull/5387
3131import WatchmanWatcher from './lib/WatchmanWatcher' ;
32- import FSEventsWatcher = require( './lib/FSEventsWatcher' ) ;
3332import * as fastPath from './lib/fast_path' ;
3433import type {
3534 ChangeEvent ,
@@ -96,7 +95,7 @@ type InternalOptions = {
9695} ;
9796
9897type Watcher = {
99- close ( callback : ( ) => void ) : void ;
98+ close ( ) : Promise < void > ;
10099} ;
101100
102101type 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 /**
0 commit comments