@@ -67,15 +67,50 @@ afterAll(() => {
67
67
test ( 'globalSetupPerWorker is triggered once before all test suites per worker' , ( ) => {
68
68
const setupPath = path . join ( e2eDir , 'setup.js' ) ;
69
69
const result = runWithJson ( e2eDir , [
70
+ '--maxWorkers=2' ,
71
+ '--workerIdleMemoryLimit=100MB' ,
70
72
`--globalSetupPerWorker=${ setupPath } ` ,
71
73
'--testPathPatterns=__tests__' ,
72
74
] ) ;
73
75
74
76
expect ( result . exitCode ) . toBe ( 0 ) ;
75
77
const files = fs . readdirSync ( DIR ) ;
76
- expect ( files ) . toHaveLength ( 1 ) ;
77
- const setup = fs . readFileSync ( path . join ( DIR , files [ 0 ] ) , 'utf8' ) ;
78
- expect ( setup ) . toBe ( 'setup' ) ;
78
+ expect ( files ) . toHaveLength ( 2 ) ;
79
+ const content = files . map ( file => {
80
+ const data = fs . readFileSync ( path . join ( DIR , file ) , 'utf8' ) ;
81
+ return data . split ( '\n' ) ;
82
+ } ) ;
83
+ for ( const [ firstLine ] of content ) {
84
+ expect ( firstLine ) . toBe ( 'setup-per-worker' ) ;
85
+ }
86
+ const secondLines = content . map ( ( [ , secondLine ] ) => secondLine ) ;
87
+ secondLines . sort ( ) ;
88
+ expect ( secondLines ) . toEqual ( [ '1' , '2' ] ) ;
89
+ } ) ;
90
+
91
+ test ( 'globalSetupPerWorker with worker threads' , ( ) => {
92
+ const setupPath = path . join ( e2eDir , 'setup.js' ) ;
93
+ const result = runWithJson ( e2eDir , [
94
+ '--maxWorkers=2' ,
95
+ '--workerIdleMemoryLimit=100MB' ,
96
+ `--globalSetupPerWorker=${ setupPath } ` ,
97
+ '--testPathPatterns=__tests__' ,
98
+ '--workerThreads' ,
99
+ ] ) ;
100
+
101
+ expect ( result . exitCode ) . toBe ( 0 ) ;
102
+ const files = fs . readdirSync ( DIR ) ;
103
+ expect ( files ) . toHaveLength ( 2 ) ;
104
+ const content = files . map ( file => {
105
+ const data = fs . readFileSync ( path . join ( DIR , file ) , 'utf8' ) ;
106
+ return data . split ( '\n' ) ;
107
+ } ) ;
108
+ for ( const [ firstLine ] of content ) {
109
+ expect ( firstLine ) . toBe ( 'setup-per-worker' ) ;
110
+ }
111
+ const secondLines = content . map ( ( [ , secondLine ] ) => secondLine ) ;
112
+ secondLines . sort ( ) ;
113
+ expect ( secondLines ) . toEqual ( [ '1' , '2' ] ) ;
79
114
} ) ;
80
115
81
116
test ( 'jest throws an error when globalSetupPerWorker does not export a function' , ( ) => {
@@ -84,6 +119,8 @@ test('jest throws an error when globalSetupPerWorker does not export a function'
84
119
'../global-setup-per-worker/invalidSetup.js' ,
85
120
) ;
86
121
const { exitCode, stderr} = runJest ( e2eDir , [
122
+ '--maxWorkers=2' ,
123
+ '--workerIdleMemoryLimit=100MB' ,
87
124
`--globalSetupPerWorker=${ setupPath } ` ,
88
125
'--testPathPatterns=__tests__' ,
89
126
] ) ;
@@ -99,18 +136,25 @@ test('globalSetupPerWorker function gets global config object and project config
99
136
const setupPath = path . resolve ( e2eDir , 'setupWithConfig.js' ) ;
100
137
101
138
const result = runJest ( e2eDir , [
139
+ '--maxWorkers=2' ,
140
+ '--workerIdleMemoryLimit=100MB' ,
102
141
`--globalSetupPerWorker=${ setupPath } ` ,
103
142
'--testPathPatterns=pass' ,
104
143
'--cache=true' ,
105
144
] ) ;
106
145
107
- expect ( result . stdout ) . toBe ( "[ 'pass' ]\ntrue" ) ;
146
+ const expected = [ "[ 'pass' ]" , 'true' , "[ 'pass' ]" , 'true' ] . join ( '\n' ) ;
147
+ expect ( result . stdout ) . toBe ( expected ) ;
108
148
} ) ;
109
149
110
150
test ( 'should call globalSetupPerWorker function of multiple projects' , ( ) => {
111
151
const configPath = path . resolve ( e2eDir , 'projects.jest.config.js' ) ;
112
152
113
- const result = runWithJson ( e2eDir , [ `--config=${ configPath } ` ] ) ;
153
+ const result = runWithJson ( e2eDir , [
154
+ '--maxWorkers=2' ,
155
+ '--workerIdleMemoryLimit=100MB' ,
156
+ `--config=${ configPath } ` ,
157
+ ] ) ;
114
158
115
159
expect ( result . exitCode ) . toBe ( 0 ) ;
116
160
@@ -123,6 +167,8 @@ test('should not call a globalSetupPerWorker of a project if there are no tests
123
167
const configPath = path . resolve ( e2eDir , 'projects.jest.config.js' ) ;
124
168
125
169
const result = runWithJson ( e2eDir , [
170
+ '--maxWorkers=2' ,
171
+ '--workerIdleMemoryLimit=100MB' ,
126
172
`--config=${ configPath } ` ,
127
173
'--testPathPatterns=setup1' ,
128
174
] ) ;
@@ -138,6 +184,8 @@ test('should not call any globalSetupPerWorker if there are no tests to run', ()
138
184
const configPath = path . resolve ( e2eDir , 'projects.jest.config.js' ) ;
139
185
140
186
const result = runWithJson ( e2eDir , [
187
+ '--maxWorkers=2' ,
188
+ '--workerIdleMemoryLimit=100MB' ,
141
189
`--config=${ configPath } ` ,
142
190
'--onlyChanged' ,
143
191
] ) ;
@@ -153,18 +201,23 @@ test('globalSetupPerWorker works with default export', () => {
153
201
const setupPath = path . resolve ( e2eDir , 'setupWithDefaultExport.js' ) ;
154
202
155
203
const result = runJest ( e2eDir , [
204
+ '--maxWorkers=2' ,
205
+ '--workerIdleMemoryLimit=100MB' ,
156
206
`--globalSetupPerWorker=${ setupPath } ` ,
157
207
'--testPathPatterns=pass' ,
158
208
'--cache=true' ,
159
209
] ) ;
160
210
161
- expect ( result . stdout ) . toBe ( "[ 'pass' ]\ntrue" ) ;
211
+ const expected = [ "[ 'pass' ]" , 'true' , "[ 'pass' ]" , 'true' ] . join ( '\n' ) ;
212
+ expect ( result . stdout ) . toBe ( expected ) ;
162
213
} ) ;
163
214
164
215
test ( 'globalSetupPerWorker throws with named export' , ( ) => {
165
216
const setupPath = path . resolve ( e2eDir , 'invalidSetupWithNamedExport.js' ) ;
166
217
167
218
const { exitCode, stderr} = runJest ( e2eDir , [
219
+ '--maxWorkers=2' ,
220
+ '--workerIdleMemoryLimit=100MB' ,
168
221
`--globalSetupPerWorker=${ setupPath } ` ,
169
222
'--testPathPatterns=__tests__' ,
170
223
] ) ;
@@ -178,6 +231,8 @@ test('globalSetupPerWorker throws with named export', () => {
178
231
179
232
test ( 'should not transpile the transformer' , ( ) => {
180
233
const { exitCode} = runJest ( 'global-setup-per-worker-custom-transform' , [
234
+ '--maxWorkers=2' ,
235
+ '--workerIdleMemoryLimit=100MB' ,
181
236
'--no-cache' ,
182
237
] ) ;
183
238
@@ -186,6 +241,8 @@ test('should not transpile the transformer', () => {
186
241
187
242
test ( 'should transform node_modules if configured by transformIgnorePatterns' , ( ) => {
188
243
const { exitCode} = runJest ( 'global-setup-per-worker-node-modules' , [
244
+ '--maxWorkers=2' ,
245
+ '--workerIdleMemoryLimit=100MB' ,
189
246
'--no-cache' ,
190
247
] ) ;
191
248
@@ -207,7 +264,11 @@ test('properly handle rejections', () => {
207
264
` ,
208
265
} ) ;
209
266
210
- const { exitCode, stderr} = runJest ( rejectionDir , [ '--no-cache' ] ) ;
267
+ const { exitCode, stderr} = runJest ( rejectionDir , [
268
+ '--maxWorkers=2' ,
269
+ '--workerIdleMemoryLimit=100MB' ,
270
+ '--no-cache' ,
271
+ ] ) ;
211
272
212
273
expect ( exitCode ) . toBe ( 1 ) ;
213
274
expect ( stderr ) . toContain (
@@ -217,9 +278,13 @@ test('properly handle rejections', () => {
217
278
} ) ;
218
279
219
280
test ( 'globalSetupPerWorker works with ESM modules' , ( ) => {
220
- const { exitCode} = runJest ( 'global-setup-per-worker-esm' , [ '--no-cache' ] , {
221
- nodeOptions : '--experimental-vm-modules --no-warnings' ,
222
- } ) ;
281
+ const { exitCode} = runJest (
282
+ 'global-setup-per-worker-esm' ,
283
+ [ '--maxWorkers=2' , '--workerIdleMemoryLimit=100MB' , '--no-cache' ] ,
284
+ {
285
+ nodeOptions : '--experimental-vm-modules --no-warnings' ,
286
+ } ,
287
+ ) ;
223
288
224
289
expect ( exitCode ) . toBe ( 0 ) ;
225
290
} ) ;
0 commit comments