Skip to content

Commit 8210505

Browse files
committed
#8708 Adjust globalSetupPerWorker e2e test for multiple workers
1 parent 4cd6bb3 commit 8210505

File tree

15 files changed

+208
-43
lines changed

15 files changed

+208
-43
lines changed

e2e/__tests__/globalSetupPerWorker.test.ts

Lines changed: 75 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,50 @@ afterAll(() => {
6767
test('globalSetupPerWorker is triggered once before all test suites per worker', () => {
6868
const setupPath = path.join(e2eDir, 'setup.js');
6969
const result = runWithJson(e2eDir, [
70+
'--maxWorkers=2',
71+
'--workerIdleMemoryLimit=100MB',
7072
`--globalSetupPerWorker=${setupPath}`,
7173
'--testPathPatterns=__tests__',
7274
]);
7375

7476
expect(result.exitCode).toBe(0);
7577
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']);
79114
});
80115

81116
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'
84119
'../global-setup-per-worker/invalidSetup.js',
85120
);
86121
const {exitCode, stderr} = runJest(e2eDir, [
122+
'--maxWorkers=2',
123+
'--workerIdleMemoryLimit=100MB',
87124
`--globalSetupPerWorker=${setupPath}`,
88125
'--testPathPatterns=__tests__',
89126
]);
@@ -99,18 +136,25 @@ test('globalSetupPerWorker function gets global config object and project config
99136
const setupPath = path.resolve(e2eDir, 'setupWithConfig.js');
100137

101138
const result = runJest(e2eDir, [
139+
'--maxWorkers=2',
140+
'--workerIdleMemoryLimit=100MB',
102141
`--globalSetupPerWorker=${setupPath}`,
103142
'--testPathPatterns=pass',
104143
'--cache=true',
105144
]);
106145

107-
expect(result.stdout).toBe("[ 'pass' ]\ntrue");
146+
const expected = ["[ 'pass' ]", 'true', "[ 'pass' ]", 'true'].join('\n');
147+
expect(result.stdout).toBe(expected);
108148
});
109149

110150
test('should call globalSetupPerWorker function of multiple projects', () => {
111151
const configPath = path.resolve(e2eDir, 'projects.jest.config.js');
112152

113-
const result = runWithJson(e2eDir, [`--config=${configPath}`]);
153+
const result = runWithJson(e2eDir, [
154+
'--maxWorkers=2',
155+
'--workerIdleMemoryLimit=100MB',
156+
`--config=${configPath}`,
157+
]);
114158

115159
expect(result.exitCode).toBe(0);
116160

@@ -123,6 +167,8 @@ test('should not call a globalSetupPerWorker of a project if there are no tests
123167
const configPath = path.resolve(e2eDir, 'projects.jest.config.js');
124168

125169
const result = runWithJson(e2eDir, [
170+
'--maxWorkers=2',
171+
'--workerIdleMemoryLimit=100MB',
126172
`--config=${configPath}`,
127173
'--testPathPatterns=setup1',
128174
]);
@@ -138,6 +184,8 @@ test('should not call any globalSetupPerWorker if there are no tests to run', ()
138184
const configPath = path.resolve(e2eDir, 'projects.jest.config.js');
139185

140186
const result = runWithJson(e2eDir, [
187+
'--maxWorkers=2',
188+
'--workerIdleMemoryLimit=100MB',
141189
`--config=${configPath}`,
142190
'--onlyChanged',
143191
]);
@@ -153,18 +201,23 @@ test('globalSetupPerWorker works with default export', () => {
153201
const setupPath = path.resolve(e2eDir, 'setupWithDefaultExport.js');
154202

155203
const result = runJest(e2eDir, [
204+
'--maxWorkers=2',
205+
'--workerIdleMemoryLimit=100MB',
156206
`--globalSetupPerWorker=${setupPath}`,
157207
'--testPathPatterns=pass',
158208
'--cache=true',
159209
]);
160210

161-
expect(result.stdout).toBe("[ 'pass' ]\ntrue");
211+
const expected = ["[ 'pass' ]", 'true', "[ 'pass' ]", 'true'].join('\n');
212+
expect(result.stdout).toBe(expected);
162213
});
163214

164215
test('globalSetupPerWorker throws with named export', () => {
165216
const setupPath = path.resolve(e2eDir, 'invalidSetupWithNamedExport.js');
166217

167218
const {exitCode, stderr} = runJest(e2eDir, [
219+
'--maxWorkers=2',
220+
'--workerIdleMemoryLimit=100MB',
168221
`--globalSetupPerWorker=${setupPath}`,
169222
'--testPathPatterns=__tests__',
170223
]);
@@ -178,6 +231,8 @@ test('globalSetupPerWorker throws with named export', () => {
178231

179232
test('should not transpile the transformer', () => {
180233
const {exitCode} = runJest('global-setup-per-worker-custom-transform', [
234+
'--maxWorkers=2',
235+
'--workerIdleMemoryLimit=100MB',
181236
'--no-cache',
182237
]);
183238

@@ -186,6 +241,8 @@ test('should not transpile the transformer', () => {
186241

187242
test('should transform node_modules if configured by transformIgnorePatterns', () => {
188243
const {exitCode} = runJest('global-setup-per-worker-node-modules', [
244+
'--maxWorkers=2',
245+
'--workerIdleMemoryLimit=100MB',
189246
'--no-cache',
190247
]);
191248

@@ -207,7 +264,11 @@ test('properly handle rejections', () => {
207264
`,
208265
});
209266

210-
const {exitCode, stderr} = runJest(rejectionDir, ['--no-cache']);
267+
const {exitCode, stderr} = runJest(rejectionDir, [
268+
'--maxWorkers=2',
269+
'--workerIdleMemoryLimit=100MB',
270+
'--no-cache',
271+
]);
211272

212273
expect(exitCode).toBe(1);
213274
expect(stderr).toContain(
@@ -217,9 +278,13 @@ test('properly handle rejections', () => {
217278
});
218279

219280
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+
);
223288

224289
expect(exitCode).toBe(0);
225290
});

e2e/global-setup-per-worker-custom-transform/__tests__/test.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,21 @@ const DIR = path.join(
1818

1919
test('should exist setup file', () => {
2020
const files = fs.readdirSync(DIR);
21-
expect(files).toHaveLength(1);
22-
const setup = fs.readFileSync(path.join(DIR, files[0]), 'utf8');
23-
expect(setup).toBe('setup');
21+
expect(files).toHaveLength(2);
22+
23+
const content = files.map(file => {
24+
const data = fs.readFileSync(path.join(DIR, file), 'utf8');
25+
return data.split('\n');
26+
});
27+
for (const [firstLine] of content) {
28+
expect(firstLine).toBe('setup-per-worker');
29+
}
30+
const secondLines = content.map(([, secondLine]) => secondLine);
31+
secondLines.sort();
32+
expect(secondLines).toEqual(['1', '2']);
33+
expect(secondLines).toEqual(
34+
expect.arrayContaining([process.env.JEST_WORKER_ID]),
35+
);
2436
});
2537

2638
test('should transform imported file', () => {

e2e/global-setup-per-worker-custom-transform/setup.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ module.exports = function () {
1919
return new Promise(resolve => {
2020
createDirectory(DIR);
2121
const fileId = crypto.randomBytes(20).toString('hex');
22-
fs.writeFileSync(path.join(DIR, fileId), 'setup');
22+
const data = ['setup-per-worker', process.env.JEST_WORKER_ID].join('\n');
23+
fs.writeFileSync(path.join(DIR, fileId), data);
2324
resolve();
2425
});
2526
};

e2e/global-setup-per-worker-esm/__tests__/test.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,27 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
import fs from 'graceful-fs';
89
import * as os from 'os';
910
import * as path from 'path';
10-
import fs from 'graceful-fs';
11-
import greeting from '../';
1211

1312
const DIR = path.join(os.tmpdir(), 'jest-global-setup-per-worker-esm');
1413

1514
test('should exist setup file', () => {
1615
const files = fs.readdirSync(DIR);
17-
expect(files).toHaveLength(1);
18-
const setup = fs.readFileSync(path.join(DIR, files[0]), 'utf8');
19-
expect(setup).toBe('setup');
16+
expect(files).toHaveLength(2);
17+
18+
const content = files.map(file => {
19+
const data = fs.readFileSync(path.join(DIR, file), 'utf8');
20+
return data.split('\n');
21+
});
22+
for (const [firstLine] of content) {
23+
expect(firstLine).toBe('setup-per-worker');
24+
}
25+
const secondLines = content.map(([, secondLine]) => secondLine);
26+
secondLines.sort();
27+
expect(secondLines).toEqual(['1', '2']);
28+
expect(secondLines).toEqual(
29+
expect.arrayContaining([process.env.JEST_WORKER_ID]),
30+
);
2031
});

e2e/global-setup-per-worker-esm/setup.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ export default function () {
1616
return new Promise(resolve => {
1717
createDirectory(DIR);
1818
const fileId = crypto.randomBytes(20).toString('hex');
19-
fs.writeFileSync(path.join(DIR, fileId), 'setup');
19+
const data = ['setup-per-worker', process.env.JEST_WORKER_ID].join('\n');
20+
fs.writeFileSync(path.join(DIR, fileId), data);
2021
resolve();
2122
});
2223
}

e2e/global-setup-per-worker-node-modules/__tests__/test.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,19 @@ const DIR = path.join(os.tmpdir(), 'jest-global-setup-per-worker-node-modules');
1414

1515
test('should exist setup file', () => {
1616
const files = fs.readdirSync(DIR);
17-
expect(files).toHaveLength(1);
18-
const setup = fs.readFileSync(path.join(DIR, files[0]), 'utf8');
19-
expect(setup).toBe('hello world!');
17+
expect(files).toHaveLength(2);
18+
19+
const content = files.map(file => {
20+
const data = fs.readFileSync(path.join(DIR, file), 'utf8');
21+
return data.split('\n');
22+
});
23+
for (const [firstLine] of content) {
24+
expect(firstLine).toBe('setup-per-worker');
25+
}
26+
const secondLines = content.map(([, secondLine]) => secondLine);
27+
secondLines.sort();
28+
expect(secondLines).toEqual(['1', '2']);
29+
expect(secondLines).toEqual(
30+
expect.arrayContaining([process.env.JEST_WORKER_ID]),
31+
);
2032
});

e2e/global-setup-per-worker-node-modules/setup.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*/
7-
import example from './node_modules/example';
87
const crypto = require('crypto');
98
const fs = require('fs');
109
const os = require('os');
@@ -17,7 +16,8 @@ module.exports = function () {
1716
return new Promise(resolve => {
1817
createDirectory(DIR);
1918
const fileId = crypto.randomBytes(20).toString('hex');
20-
fs.writeFileSync(path.join(DIR, fileId), `hello ${example()}`);
19+
const data = ['setup-per-worker', process.env.JEST_WORKER_ID].join('\n');
20+
fs.writeFileSync(path.join(DIR, fileId), data);
2121
resolve();
2222
});
2323
};

e2e/global-setup-per-worker/__tests__/setup1.test.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,19 @@ const DIR = path.join(os.tmpdir(), 'jest-global-setup-per-worker');
1414

1515
test('should exist setup file', () => {
1616
const files = fs.readdirSync(DIR);
17-
expect(files).toHaveLength(1);
18-
const setup = fs.readFileSync(path.join(DIR, files[0]), 'utf8');
19-
expect(setup).toBe('setup');
17+
expect(files).toHaveLength(2);
18+
19+
const content = files.map(file => {
20+
const data = fs.readFileSync(path.join(DIR, file), 'utf8');
21+
return data.split('\n');
22+
});
23+
for (const [firstLine] of content) {
24+
expect(firstLine).toBe('setup-per-worker');
25+
}
26+
const secondLines = content.map(([, secondLine]) => secondLine);
27+
secondLines.sort();
28+
expect(secondLines).toEqual(['1', '2']);
29+
expect(secondLines).toEqual(
30+
expect.arrayContaining([process.env.JEST_WORKER_ID]),
31+
);
2032
});

e2e/global-setup-per-worker/__tests__/setup2.test.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,19 @@ const DIR = path.join(os.tmpdir(), 'jest-global-setup-per-worker');
1414

1515
test('should exist setup file', () => {
1616
const files = fs.readdirSync(DIR);
17-
expect(files).toHaveLength(1);
18-
const setup = fs.readFileSync(path.join(DIR, files[0]), 'utf8');
19-
expect(setup).toBe('setup');
17+
expect(files).toHaveLength(2);
18+
19+
const content = files.map(file => {
20+
const data = fs.readFileSync(path.join(DIR, file), 'utf8');
21+
return data.split('\n');
22+
});
23+
for (const [firstLine] of content) {
24+
expect(firstLine).toBe('setup-per-worker');
25+
}
26+
const secondLines = content.map(([, secondLine]) => secondLine);
27+
secondLines.sort();
28+
expect(secondLines).toEqual(['1', '2']);
29+
expect(secondLines).toEqual(
30+
expect.arrayContaining([process.env.JEST_WORKER_ID]),
31+
);
2032
});

e2e/global-setup-per-worker/__tests__/setup3.test.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,19 @@ const DIR = path.join(os.tmpdir(), 'jest-global-setup-per-worker');
1414

1515
test('should exist setup file', () => {
1616
const files = fs.readdirSync(DIR);
17-
expect(files).toHaveLength(1);
18-
const setup = fs.readFileSync(path.join(DIR, files[0]), 'utf8');
19-
expect(setup).toBe('setup');
17+
expect(files).toHaveLength(2);
18+
19+
const content = files.map(file => {
20+
const data = fs.readFileSync(path.join(DIR, file), 'utf8');
21+
return data.split('\n');
22+
});
23+
for (const [firstLine] of content) {
24+
expect(firstLine).toBe('setup-per-worker');
25+
}
26+
const secondLines = content.map(([, secondLine]) => secondLine);
27+
secondLines.sort();
28+
expect(secondLines).toEqual(['1', '2']);
29+
expect(secondLines).toEqual(
30+
expect.arrayContaining([process.env.JEST_WORKER_ID]),
31+
);
2032
});

0 commit comments

Comments
 (0)