@@ -45,47 +45,90 @@ 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
] ) ;
81
121
82
- expect ( result . stdout ) . toBe ( "[ 'pass' ]\ntrue" ) ;
122
+ const expected = [ "[ 'pass' ]" , 'true' , "[ 'pass' ]" , 'true' ] . join ( '\n' ) ;
123
+ expect ( result . stdout ) . toBe ( expected ) ;
83
124
} ) ;
84
125
85
126
test ( 'should call globalTeardownPerWorker function of multiple projects' , ( ) => {
86
127
const configPath = path . resolve ( e2eDir , 'projects.jest.config.js' ) ;
87
128
88
129
const result = runWithJson ( 'global-teardown-per-worker' , [
130
+ '--maxWorkers=2' ,
131
+ '--workerIdleMemoryLimit=100MB' ,
89
132
`--config=${ configPath } ` ,
90
133
] ) ;
91
134
@@ -100,6 +143,8 @@ test('should not call a globalTeardownPerWorker of a project if there are no tes
100
143
const configPath = path . resolve ( e2eDir , 'projects.jest.config.js' ) ;
101
144
102
145
const result = runWithJson ( 'global-teardown-per-worker' , [
146
+ '--maxWorkers=2' ,
147
+ '--workerIdleMemoryLimit=100MB' ,
103
148
`--config=${ configPath } ` ,
104
149
'--testPathPatterns=teardown1' ,
105
150
] ) ;
@@ -115,12 +160,15 @@ test('globalTeardownPerWorker works with default export', () => {
115
160
const teardownPath = path . resolve ( e2eDir , 'teardownWithDefaultExport.js' ) ;
116
161
117
162
const result = runJest ( e2eDir , [
118
- `--globalTeardown=${ teardownPath } ` ,
163
+ '--maxWorkers=2' ,
164
+ '--workerIdleMemoryLimit=100MB' ,
165
+ `--globalTeardownPerWorker=${ teardownPath } ` ,
119
166
'--testPathPatterns=pass' ,
120
167
'--cache=true' ,
121
168
] ) ;
122
169
123
- expect ( result . stdout ) . toBe ( "[ 'pass' ]\ntrue" ) ;
170
+ const expected = [ "[ 'pass' ]" , 'true' , "[ 'pass' ]" , 'true' ] . join ( '\n' ) ;
171
+ expect ( result . stdout ) . toBe ( expected ) ;
124
172
} ) ;
125
173
126
174
test ( 'globalTeardownPerWorker throws with named export' , ( ) => {
@@ -130,21 +178,27 @@ test('globalTeardownPerWorker throws with named export', () => {
130
178
) ;
131
179
132
180
const { exitCode, stderr} = runJest ( e2eDir , [
133
- `--globalTeardown=${ teardownPath } ` ,
181
+ '--maxWorkers=2' ,
182
+ '--workerIdleMemoryLimit=100MB' ,
183
+ `--globalTeardownPerWorker=${ teardownPath } ` ,
134
184
'--testPathPatterns=__tests__' ,
135
185
] ) ;
136
186
137
187
expect ( exitCode ) . toBe ( 1 ) ;
138
- expect ( stderr ) . toContain ( 'Jest: Got error running globalTeardown ' ) ;
188
+ expect ( stderr ) . toContain ( 'Jest: Got error running globalTeardownPerWorker ' ) ;
139
189
expect ( stderr ) . toContain (
140
- `globalTeardown file must export a function at ${ teardownPath } ` ,
190
+ `globalTeardownPerWorker file must export a function at ${ teardownPath } ` ,
141
191
) ;
142
192
} ) ;
143
193
144
194
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
- } ) ;
195
+ const { exitCode} = runJest (
196
+ 'global-teardown-per-worker-esm' ,
197
+ [ '--maxWorkers=2' , '--workerIdleMemoryLimit=100MB' , '--no-cache' ] ,
198
+ {
199
+ nodeOptions : '--experimental-vm-modules --no-warnings' ,
200
+ } ,
201
+ ) ;
148
202
149
203
expect ( exitCode ) . toBe ( 0 ) ;
150
204
} ) ;
0 commit comments