@@ -45,36 +45,76 @@ test('globalTeardownPerWorker is triggered once after all test suites per worker
4545 createDirectory ( DIR ) ;
4646 const teardownPath = path . resolve ( e2eDir , 'teardown.js' ) ;
4747 const result = runWithJson ( 'global-teardown-per-worker' , [
48- `--globalTeardown=${ teardownPath } ` ,
48+ '--maxWorkers=2' ,
49+ '--workerIdleMemoryLimit=100MB' ,
50+ `--globalTeardownPerWorker=${ teardownPath } ` ,
4951 '--testPathPatterns=__tests__' ,
5052 ] ) ;
5153
5254 expect ( result . exitCode ) . toBe ( 0 ) ;
5355 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' ] ) ;
5793} ) ;
5894
5995test ( 'jest throws an error when globalTeardownPerWorker does not export a function' , ( ) => {
6096 const teardownPath = path . resolve ( e2eDir , 'invalidTeardown.js' ) ;
6197 const { exitCode, stderr} = runJest ( e2eDir , [
62- `--globalTeardown=${ teardownPath } ` ,
98+ '--maxWorkers=2' ,
99+ '--workerIdleMemoryLimit=100MB' ,
100+ `--globalTeardownPerWorker=${ teardownPath } ` ,
63101 '--testPathPatterns=__tests__' ,
64102 ] ) ;
65103
66104 expect ( exitCode ) . toBe ( 1 ) ;
67- expect ( stderr ) . toContain ( 'Jest: Got error running globalTeardown ' ) ;
105+ expect ( stderr ) . toContain ( 'Jest: Got error running globalTeardownPerWorker ' ) ;
68106 expect ( stderr ) . toContain (
69- `globalTeardown file must export a function at ${ teardownPath } ` ,
107+ `globalTeardownPerWorker file must export a function at ${ teardownPath } ` ,
70108 ) ;
71109} ) ;
72110
73111test ( 'globalTeardownPerWorker function gets global config object and project config as parameters' , ( ) => {
74112 const teardownPath = path . resolve ( e2eDir , 'teardownWithConfig.js' ) ;
75113
76114 const result = runJest ( e2eDir , [
77- `--globalTeardown=${ teardownPath } ` ,
115+ '--maxWorkers=2' ,
116+ '--workerIdleMemoryLimit=100MB' ,
117+ `--globalTeardownPerWorker=${ teardownPath } ` ,
78118 '--testPathPatterns=pass' ,
79119 '--cache=true' ,
80120 ] ) ;
@@ -86,6 +126,8 @@ test('should call globalTeardownPerWorker function of multiple projects', () =>
86126 const configPath = path . resolve ( e2eDir , 'projects.jest.config.js' ) ;
87127
88128 const result = runWithJson ( 'global-teardown-per-worker' , [
129+ '--maxWorkers=2' ,
130+ '--workerIdleMemoryLimit=100MB' ,
89131 `--config=${ configPath } ` ,
90132 ] ) ;
91133
@@ -100,6 +142,8 @@ test('should not call a globalTeardownPerWorker of a project if there are no tes
100142 const configPath = path . resolve ( e2eDir , 'projects.jest.config.js' ) ;
101143
102144 const result = runWithJson ( 'global-teardown-per-worker' , [
145+ '--maxWorkers=2' ,
146+ '--workerIdleMemoryLimit=100MB' ,
103147 `--config=${ configPath } ` ,
104148 '--testPathPatterns=teardown1' ,
105149 ] ) ;
@@ -115,7 +159,9 @@ test('globalTeardownPerWorker works with default export', () => {
115159 const teardownPath = path . resolve ( e2eDir , 'teardownWithDefaultExport.js' ) ;
116160
117161 const result = runJest ( e2eDir , [
118- `--globalTeardown=${ teardownPath } ` ,
162+ '--maxWorkers=2' ,
163+ '--workerIdleMemoryLimit=100MB' ,
164+ `--globalTeardownPerWorker=${ teardownPath } ` ,
119165 '--testPathPatterns=pass' ,
120166 '--cache=true' ,
121167 ] ) ;
@@ -130,21 +176,27 @@ test('globalTeardownPerWorker throws with named export', () => {
130176 ) ;
131177
132178 const { exitCode, stderr} = runJest ( e2eDir , [
133- `--globalTeardown=${ teardownPath } ` ,
179+ '--maxWorkers=2' ,
180+ '--workerIdleMemoryLimit=100MB' ,
181+ `--globalTeardownPerWorker=${ teardownPath } ` ,
134182 '--testPathPatterns=__tests__' ,
135183 ] ) ;
136184
137185 expect ( exitCode ) . toBe ( 1 ) ;
138- expect ( stderr ) . toContain ( 'Jest: Got error running globalTeardown ' ) ;
186+ expect ( stderr ) . toContain ( 'Jest: Got error running globalTeardownPerWorker ' ) ;
139187 expect ( stderr ) . toContain (
140- `globalTeardown file must export a function at ${ teardownPath } ` ,
188+ `globalTeardownPerWorker file must export a function at ${ teardownPath } ` ,
141189 ) ;
142190} ) ;
143191
144192test ( '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+ ) ;
148200
149201 expect ( exitCode ) . toBe ( 0 ) ;
150202} ) ;
0 commit comments