@@ -12,8 +12,8 @@ import {createHash} from 'crypto';
1212import { EventEmitter } from 'events' ;
1313import { tmpdir } from 'os' ;
1414import * as path from 'path' ;
15+ import { FSWatcher as ChokidarFsWatcher , watch as chokidarWatch } from 'chokidar' ;
1516import type { Stats } from 'graceful-fs' ;
16- import { NodeWatcher , Watcher as SaneWatcher } from 'sane' ;
1717import type { Config } from '@jest/types' ;
1818import serializer from 'jest-serializer' ;
1919import Worker from 'jest-worker' ;
@@ -31,7 +31,6 @@ import normalizePathSep from './lib/normalizePathSep';
3131import watchmanCrawl = require( './crawlers/watchman' ) ;
3232// @ts -expect-error: not converted to TypeScript - it's a fork: https://github.com/facebook/jest/pull/5387
3333import WatchmanWatcher from './lib/WatchmanWatcher' ;
34- import FSEventsWatcher = require( './lib/FSEventsWatcher' ) ;
3534import * as fastPath from './lib/fast_path' ;
3635import type {
3736 ChangeEvent ,
@@ -98,7 +97,7 @@ type InternalOptions = {
9897} ;
9998
10099type Watcher = {
101- close ( callback : ( ) => void ) : void ;
100+ close ( ) : Promise < void > ;
102101} ;
103102
104103type 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 /**
0 commit comments