Skip to content

Commit 866fa98

Browse files
committed
#8708 Adjust globalTeardownPerWorker e2e test for multiple workers
1 parent 252a476 commit 866fa98

File tree

6 files changed

+78
-22
lines changed

6 files changed

+78
-22
lines changed

e2e/__tests__/globalTeardownPerWorker.test.ts

Lines changed: 67 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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

5995
test('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

73111
test('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

144192
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+
);
148200

149201
expect(exitCode).toBe(0);
150202
});

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ import {createDirectory} from 'jest-util';
1313
const DIR = path.join(os.tmpdir(), 'jest-global-teardown-per-worker-esm');
1414

1515
export default function () {
16-
return new Promise(resolve => {
16+
return new Promise((resolve, reject) => {
1717
createDirectory(DIR);
1818
const fileId = crypto.randomBytes(20).toString('hex');
19-
fs.writeFileSync(path.join(DIR, fileId), 'teardown');
19+
const data = ['teardown-per-worker', process.env.JEST_WORKER_ID].join('\n');
20+
fs.writeFileSync(path.join(DIR, fileId), data);
2021
resolve();
2122
});
2223
}

e2e/global-teardown-per-worker/project-1/teardown.js

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

e2e/global-teardown-per-worker/project-2/teardown.js

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

e2e/global-teardown-per-worker/projects.jest.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ module.exports = {
1212
projects: [
1313
{
1414
displayName: 'project-1',
15-
globalTeardown: '<rootDir>/teardown.js',
15+
globalTeardownPerWorker: '<rootDir>/teardown.js',
1616
rootDir: path.resolve(__dirname, './project-1'),
1717
testMatch: ['<rootDir>/**/*.test.js'],
1818
transformIgnorePatterns: ['/node_modules/', '/packages/'],
1919
},
2020
{
2121
displayName: 'project-2',
22-
globalTeardown: '<rootDir>/teardown.js',
22+
globalTeardownPerWorker: '<rootDir>/teardown.js',
2323
rootDir: path.resolve(__dirname, './project-2'),
2424
testMatch: ['<rootDir>/**/*.test.js'],
2525
transformIgnorePatterns: ['/node_modules/', '/packages/'],

e2e/global-teardown-per-worker/teardown.js

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

0 commit comments

Comments
 (0)