@@ -8,35 +8,56 @@ describe("slurpFile", () => {
8
8
const getMockFileContents = ( path : string , options = UTF8 ) => JSON . stringify ( { path, options } ) ;
9
9
10
10
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
+ } ) ;
14
15
} ) ;
15
16
16
17
afterEach ( ( ) => {
17
18
jest . clearAllMocks ( ) ;
18
19
} ) ;
19
20
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
+ } ) ;
25
39
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 ) ;
29
45
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 ] ) ;
33
49
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 ) ;
36
53
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
+ } ) ;
40
61
} ) ;
41
62
} ) ;
42
63
0 commit comments