@@ -45,36 +45,76 @@ test('globalTeardownPerWorker is triggered once after all test suites per worker
45
45
createDirectory ( DIR ) ;
46
46
const teardownPath = path . resolve ( e2eDir , 'teardown.js' ) ;
47
47
const result = runWithJson ( 'global-teardown-per-worker' , [
48
- `--globalTeardown=${ teardownPath } ` ,
48
+ '--maxWorkers=2' ,
49
+ '--workerIdleMemoryLimit=100MB' ,
50
+ `--globalTeardownPerWorker=${ teardownPath } ` ,
49
51
'--testPathPatterns=__tests__' ,
50
52
] ) ;
51
53
52
54
expect ( result . exitCode ) . toBe ( 0 ) ;
53
55
const files = fs . readdirSync ( DIR ) ;
54
- expect ( files ) . toHaveLength ( 1 ) ;
55
- const teardown = fs . readFileSync ( path . join ( DIR , files [ 0 ] ) , 'utf8' ) ;
56
- expect ( teardown ) . toBe ( 'teardown' ) ;
56
+ expect ( files ) . toHaveLength ( 2 ) ;
57
+ const content = files . map ( file => {
58
+ const data = fs . readFileSync ( path . join ( DIR , file ) , 'utf8' ) ;
59
+ return data . split ( '\n' ) ;
60
+ } ) ;
61
+ for ( const [ firstLine ] of content ) {
62
+ expect ( firstLine ) . toBe ( 'teardown-per-worker' ) ;
63
+ }
64
+ const secondLines = content . map ( ( [ , secondLine ] ) => secondLine ) ;
65
+ secondLines . sort ( ) ;
66
+ expect ( secondLines ) . toEqual ( [ '1' , '2' ] ) ;
67
+ } ) ;
68
+
69
+ test ( 'globalTeardownPerWorker with worker threads' , ( ) => {
70
+ createDirectory ( DIR ) ;
71
+ const teardownPath = path . resolve ( e2eDir , 'teardown.js' ) ;
72
+ const result = runWithJson ( 'global-teardown-per-worker' , [
73
+ '--maxWorkers=2' ,
74
+ '--workerIdleMemoryLimit=100MB' ,
75
+ `--globalTeardownPerWorker=${ teardownPath } ` ,
76
+ '--testPathPatterns=__tests__' ,
77
+ '--workerThreads' ,
78
+ ] ) ;
79
+
80
+ expect ( result . exitCode ) . toBe ( 0 ) ;
81
+ const files = fs . readdirSync ( DIR ) ;
82
+ expect ( files ) . toHaveLength ( 2 ) ;
83
+ const content = files . map ( file => {
84
+ const data = fs . readFileSync ( path . join ( DIR , file ) , 'utf8' ) ;
85
+ return data . split ( '\n' ) ;
86
+ } ) ;
87
+ for ( const [ firstLine ] of content ) {
88
+ expect ( firstLine ) . toBe ( 'teardown-per-worker' ) ;
89
+ }
90
+ const secondLines = content . map ( ( [ , secondLine ] ) => secondLine ) ;
91
+ secondLines . sort ( ) ;
92
+ expect ( secondLines ) . toEqual ( [ '1' , '2' ] ) ;
57
93
} ) ;
58
94
59
95
test ( 'jest throws an error when globalTeardownPerWorker does not export a function' , ( ) => {
60
96
const teardownPath = path . resolve ( e2eDir , 'invalidTeardown.js' ) ;
61
97
const { exitCode, stderr} = runJest ( e2eDir , [
62
- `--globalTeardown=${ teardownPath } ` ,
98
+ '--maxWorkers=2' ,
99
+ '--workerIdleMemoryLimit=100MB' ,
100
+ `--globalTeardownPerWorker=${ teardownPath } ` ,
63
101
'--testPathPatterns=__tests__' ,
64
102
] ) ;
65
103
66
104
expect ( exitCode ) . toBe ( 1 ) ;
67
- expect ( stderr ) . toContain ( 'Jest: Got error running globalTeardown ' ) ;
105
+ expect ( stderr ) . toContain ( 'Jest: Got error running globalTeardownPerWorker ' ) ;
68
106
expect ( stderr ) . toContain (
69
- `globalTeardown file must export a function at ${ teardownPath } ` ,
107
+ `globalTeardownPerWorker file must export a function at ${ teardownPath } ` ,
70
108
) ;
71
109
} ) ;
72
110
73
111
test ( 'globalTeardownPerWorker function gets global config object and project config as parameters' , ( ) => {
74
112
const teardownPath = path . resolve ( e2eDir , 'teardownWithConfig.js' ) ;
75
113
76
114
const result = runJest ( e2eDir , [
77
- `--globalTeardown=${ teardownPath } ` ,
115
+ '--maxWorkers=2' ,
116
+ '--workerIdleMemoryLimit=100MB' ,
117
+ `--globalTeardownPerWorker=${ teardownPath } ` ,
78
118
'--testPathPatterns=pass' ,
79
119
'--cache=true' ,
80
120
] ) ;
@@ -86,6 +126,8 @@ test('should call globalTeardownPerWorker function of multiple projects', () =>
86
126
const configPath = path . resolve ( e2eDir , 'projects.jest.config.js' ) ;
87
127
88
128
const result = runWithJson ( 'global-teardown-per-worker' , [
129
+ '--maxWorkers=2' ,
130
+ '--workerIdleMemoryLimit=100MB' ,
89
131
`--config=${ configPath } ` ,
90
132
] ) ;
91
133
@@ -100,6 +142,8 @@ test('should not call a globalTeardownPerWorker of a project if there are no tes
100
142
const configPath = path . resolve ( e2eDir , 'projects.jest.config.js' ) ;
101
143
102
144
const result = runWithJson ( 'global-teardown-per-worker' , [
145
+ '--maxWorkers=2' ,
146
+ '--workerIdleMemoryLimit=100MB' ,
103
147
`--config=${ configPath } ` ,
104
148
'--testPathPatterns=teardown1' ,
105
149
] ) ;
@@ -115,7 +159,9 @@ test('globalTeardownPerWorker works with default export', () => {
115
159
const teardownPath = path . resolve ( e2eDir , 'teardownWithDefaultExport.js' ) ;
116
160
117
161
const result = runJest ( e2eDir , [
118
- `--globalTeardown=${ teardownPath } ` ,
162
+ '--maxWorkers=2' ,
163
+ '--workerIdleMemoryLimit=100MB' ,
164
+ `--globalTeardownPerWorker=${ teardownPath } ` ,
119
165
'--testPathPatterns=pass' ,
120
166
'--cache=true' ,
121
167
] ) ;
@@ -130,21 +176,27 @@ test('globalTeardownPerWorker throws with named export', () => {
130
176
) ;
131
177
132
178
const { exitCode, stderr} = runJest ( e2eDir , [
133
- `--globalTeardown=${ teardownPath } ` ,
179
+ '--maxWorkers=2' ,
180
+ '--workerIdleMemoryLimit=100MB' ,
181
+ `--globalTeardownPerWorker=${ teardownPath } ` ,
134
182
'--testPathPatterns=__tests__' ,
135
183
] ) ;
136
184
137
185
expect ( exitCode ) . toBe ( 1 ) ;
138
- expect ( stderr ) . toContain ( 'Jest: Got error running globalTeardown ' ) ;
186
+ expect ( stderr ) . toContain ( 'Jest: Got error running globalTeardownPerWorker ' ) ;
139
187
expect ( stderr ) . toContain (
140
- `globalTeardown file must export a function at ${ teardownPath } ` ,
188
+ `globalTeardownPerWorker file must export a function at ${ teardownPath } ` ,
141
189
) ;
142
190
} ) ;
143
191
144
192
test ( 'globalTeardownPerWorker works with ESM modules' , ( ) => {
145
- const { exitCode} = runJest ( 'global-teardown-per-worker-esm' , [ '--no-cache' ] , {
146
- nodeOptions : '--experimental-vm-modules --no-warnings' ,
147
- } ) ;
193
+ const { exitCode} = runJest (
194
+ 'global-teardown-per-worker-esm' ,
195
+ [ '--maxWorkers=2' , '--workerIdleMemoryLimit=100MB' , '--no-cache' ] ,
196
+ {
197
+ nodeOptions : '--experimental-vm-modules --no-warnings' ,
198
+ } ,
199
+ ) ;
148
200
149
201
expect ( exitCode ) . toBe ( 0 ) ;
150
202
} ) ;
0 commit comments