@@ -8,35 +8,56 @@ describe("slurpFile", () => {
88 const getMockFileContents = ( path : string , options = UTF8 ) => JSON . stringify ( { path, options } ) ;
99
1010 beforeEach ( ( ) => {
11- ( promises . readFile as jest . Mock ) . mockImplementation ( ( path , options ) =>
12- Promise . resolve ( getMockFileContents ( path , options ) )
13- ) ;
11+ ( promises . readFile as jest . Mock ) . mockImplementation ( async ( path , options ) => {
12+ await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ;
13+ return getMockFileContents ( path , options ) ;
14+ } ) ;
1415 } ) ;
1516
1617 afterEach ( ( ) => {
1718 jest . clearAllMocks ( ) ;
1819 } ) ;
1920
20- it ( "makes one readFile call for a filepath irrepsective of slurpFile calls" , ( done ) => {
21- jest . isolateModules ( async ( ) => {
22- const { slurpFile } = require ( "./slurpFile" ) ;
23- const mockPath = "/mock/path" ;
24- const mockPathContent = getMockFileContents ( mockPath ) ;
21+ describe ( "makes one readFile call for a filepath irrespective of slurpFile calls" , ( ) => {
22+ // @ts -ignore: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/34617
23+ it . each ( [ 10 , 100 , 1000 , 10000 ] ) ( "parallel calls: %d " , ( num : number , done : Function ) => {
24+ jest . isolateModules ( async ( ) => {
25+ const { slurpFile } = require ( "./slurpFile" ) ;
26+ const mockPath = "/mock/path" ;
27+ const mockPathContent = getMockFileContents ( mockPath ) ;
28+
29+ expect ( promises . readFile ) . not . toHaveBeenCalled ( ) ;
30+ const fileContentArr = await Promise . all ( Array ( num ) . fill ( slurpFile ( mockPath ) ) ) ;
31+ expect ( fileContentArr ) . toStrictEqual ( Array ( num ) . fill ( mockPathContent ) ) ;
32+
33+ // There is one readFile call even through slurpFile is called in parallel num times.
34+ expect ( promises . readFile ) . toHaveBeenCalledTimes ( 1 ) ;
35+ expect ( promises . readFile ) . toHaveBeenCalledWith ( mockPath , UTF8 ) ;
36+ done ( ) ;
37+ } ) ;
38+ } ) ;
2539
26- expect ( promises . readFile ) . not . toHaveBeenCalled ( ) ;
27- const fileContentArr = await Promise . all ( [ slurpFile ( mockPath ) , slurpFile ( mockPath ) ] ) ;
28- expect ( fileContentArr ) . toStrictEqual ( [ mockPathContent , mockPathContent ] ) ;
40+ it ( "two parallel calls and one sequential call" , ( done ) => {
41+ jest . isolateModules ( async ( ) => {
42+ const { slurpFile } = require ( "./slurpFile" ) ;
43+ const mockPath = "/mock/path" ;
44+ const mockPathContent = getMockFileContents ( mockPath ) ;
2945
30- // There is one readFile call even through slurpFile is called in parallel twice.
31- expect ( promises . readFile ) . toHaveBeenCalledTimes ( 1 ) ;
32- expect ( promises . readFile ) . toHaveBeenCalledWith ( mockPath , UTF8 ) ;
46+ expect ( promises . readFile ) . not . toHaveBeenCalled ( ) ;
47+ const fileContentArr = await Promise . all ( [ slurpFile ( mockPath ) , slurpFile ( mockPath ) ] ) ;
48+ expect ( fileContentArr ) . toStrictEqual ( [ mockPathContent , mockPathContent ] ) ;
3349
34- const fileContent = await slurpFile ( mockPath ) ;
35- expect ( fileContent ) . toStrictEqual ( mockPathContent ) ;
50+ // There is one readFile call even through slurpFile is called in parallel twice.
51+ expect ( promises . readFile ) . toHaveBeenCalledTimes ( 1 ) ;
52+ expect ( promises . readFile ) . toHaveBeenCalledWith ( mockPath , UTF8 ) ;
3653
37- // There is one readFile call even through slurpFile is called for the third time.
38- expect ( promises . readFile ) . toHaveBeenCalledTimes ( 1 ) ;
39- done ( ) ;
54+ const fileContent = await slurpFile ( mockPath ) ;
55+ expect ( fileContent ) . toStrictEqual ( mockPathContent ) ;
56+
57+ // There is one readFile call even through slurpFile is called for the third time.
58+ expect ( promises . readFile ) . toHaveBeenCalledTimes ( 1 ) ;
59+ done ( ) ;
60+ } ) ;
4061 } ) ;
4162 } ) ;
4263
0 commit comments