Skip to content

Commit 414d588

Browse files
committed
#8708 Add e2e for combination of globalTeardown and globalTeardownPerWorker
1 parent ef1d482 commit 414d588

16 files changed

+2154
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import {tmpdir} from 'os';
9+
import * as path from 'path';
10+
import * as fs from 'graceful-fs';
11+
import {cleanup, runYarnInstall} from '../Utils';
12+
import {json as runWithJson} from '../runJest';
13+
import {createDirectory} from 'jest-util';
14+
15+
const DIR = path.join(tmpdir(), 'jest-global-teardown-and-per-worker');
16+
const project1DIR = path.join(
17+
tmpdir(),
18+
'jest-global-teardown-and-per-worker-project-1',
19+
);
20+
const project2DIR = path.join(
21+
tmpdir(),
22+
'jest-global-teardown-and-per-worker-project-2',
23+
);
24+
const e2eDir = path.resolve(__dirname, '../global-teardown-and-per-worker');
25+
26+
beforeAll(() => {
27+
runYarnInstall(e2eDir);
28+
});
29+
30+
beforeEach(() => {
31+
cleanup(DIR);
32+
cleanup(project1DIR);
33+
cleanup(project2DIR);
34+
});
35+
36+
afterAll(() => {
37+
cleanup(DIR);
38+
cleanup(project1DIR);
39+
cleanup(project2DIR);
40+
});
41+
42+
test('globalTeardown triggered once + globalTeardownPerWorker triggered once per worker', () => {
43+
createDirectory(DIR);
44+
const teardownPath = path.join(e2eDir, 'teardown.js');
45+
const teardownPerWorkerPath = path.join(e2eDir, 'teardown-per-worker.js');
46+
const result = runWithJson(e2eDir, [
47+
'--maxWorkers=2',
48+
'--workerIdleMemoryLimit=100MB',
49+
`--globalTeardown=${teardownPath}`,
50+
`--globalTeardownPerWorker=${teardownPerWorkerPath}`,
51+
'--testPathPatterns=__tests__',
52+
]);
53+
54+
expect(result.exitCode).toBe(0);
55+
const files = fs.readdirSync(DIR);
56+
expect(files).toHaveLength(3);
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, secondLine] of content) {
62+
if (secondLine) {
63+
expect(firstLine).toBe('teardown-per-worker');
64+
} else {
65+
expect(firstLine).toBe('teardown');
66+
}
67+
}
68+
const secondLines = content.map(([, secondLine]) => secondLine);
69+
secondLines.sort();
70+
expect(secondLines).toEqual(['1', '2', undefined]);
71+
});
72+
test('globalTeardown + globalTeardownPerWorker with worker threads', () => {
73+
createDirectory(DIR);
74+
const teardownPath = path.join(e2eDir, 'teardown.js');
75+
const teardownPerWorkerPath = path.join(e2eDir, 'teardown-per-worker.js');
76+
const result = runWithJson(e2eDir, [
77+
'--maxWorkers=2',
78+
'--workerIdleMemoryLimit=100MB',
79+
`--globalTeardown=${teardownPath}`,
80+
`--globalTeardownPerWorker=${teardownPerWorkerPath}`,
81+
'--testPathPatterns=__tests__',
82+
'--workerThreads',
83+
]);
84+
85+
expect(result.exitCode).toBe(0);
86+
const files = fs.readdirSync(DIR);
87+
expect(files).toHaveLength(3);
88+
const content = files.map(file => {
89+
const data = fs.readFileSync(path.join(DIR, file), 'utf8');
90+
return data.split('\n');
91+
});
92+
for (const [firstLine, secondLine] of content) {
93+
if (secondLine) {
94+
expect(firstLine).toBe('teardown-per-worker');
95+
} else {
96+
expect(firstLine).toBe('teardown');
97+
}
98+
}
99+
const secondLines = content.map(([, secondLine]) => secondLine);
100+
secondLines.sort();
101+
expect(secondLines).toEqual(['1', '2', undefined]);
102+
});
103+
104+
test('multiple projects', () => {
105+
const configPath = path.resolve(e2eDir, 'projects.jest.config.js');
106+
107+
const result = runWithJson(e2eDir, [
108+
'--maxWorkers=2',
109+
'--workerIdleMemoryLimit=100MB',
110+
`--config=${configPath}`,
111+
]);
112+
113+
expect(result.exitCode).toBe(0);
114+
115+
expect(fs.existsSync(DIR)).toBe(true);
116+
expect(fs.existsSync(project1DIR)).toBe(true);
117+
expect(fs.existsSync(project2DIR)).toBe(true);
118+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
'use strict';
8+
9+
const fs = require('fs');
10+
const os = require('os');
11+
const path = require('path');
12+
13+
const DIR = path.join(os.tmpdir(), 'jest-global-teardown-and-per-worker');
14+
15+
test('should not exist teardown file', () => {
16+
const files = fs.readdirSync(DIR);
17+
expect(files).toHaveLength(0);
18+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
'use strict';
8+
9+
const fs = require('fs');
10+
const os = require('os');
11+
const path = require('path');
12+
13+
const DIR = path.join(os.tmpdir(), 'jest-global-teardown-and-per-worker');
14+
15+
test('should not exist teardown file', () => {
16+
const files = fs.readdirSync(DIR);
17+
expect(files).toHaveLength(0);
18+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
'use strict';
8+
9+
const fs = require('fs');
10+
const os = require('os');
11+
const path = require('path');
12+
13+
const DIR = path.join(os.tmpdir(), 'jest-global-teardown-and-per-worker');
14+
15+
test('should not exist teardown file', () => {
16+
const files = fs.readdirSync(DIR);
17+
expect(files).toHaveLength(0);
18+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
module.exports = {
9+
presets: ['@babel/preset-env', '@babel/preset-flow'],
10+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"jest": {
3+
"testEnvironment": "node",
4+
"transformIgnorePatterns": [
5+
"/node_modules/",
6+
"/packages/"
7+
]
8+
},
9+
"devDependencies": {
10+
"@babel/core": "^7.0.0",
11+
"@babel/preset-env": "^7.0.0",
12+
"@babel/preset-flow": "^7.0.0",
13+
"jest-util": "*"
14+
}
15+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
const crypto = require('crypto');
8+
const fs = require('fs');
9+
const os = require('os');
10+
const path = require('path');
11+
const {createDirectory} = require('jest-util');
12+
13+
const DIR = path.join(
14+
os.tmpdir(),
15+
'jest-global-teardown-and-per-worker-project-1',
16+
);
17+
18+
module.exports = function () {
19+
return new Promise((resolve, reject) => {
20+
createDirectory(DIR);
21+
const fileId = crypto.randomBytes(20).toString('hex');
22+
const data = ['teardown-per-worker', process.env.JEST_WORKER_ID].join('\n');
23+
fs.writeFileSync(path.join(DIR, fileId), data);
24+
resolve();
25+
});
26+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
const crypto = require('crypto');
8+
const fs = require('fs');
9+
const os = require('os');
10+
const path = require('path');
11+
const {createDirectory} = require('jest-util');
12+
13+
const DIR = path.join(
14+
os.tmpdir(),
15+
'jest-global-teardown-and-per-worker-project-1',
16+
);
17+
18+
module.exports = function () {
19+
return new Promise((resolve, reject) => {
20+
createDirectory(DIR);
21+
const fileId = crypto.randomBytes(20).toString('hex');
22+
fs.writeFileSync(path.join(DIR, fileId), 'teardown');
23+
resolve();
24+
});
25+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
'use strict';
8+
9+
const fs = require('fs');
10+
const os = require('os');
11+
const path = require('path');
12+
13+
const DIR = path.join(
14+
os.tmpdir(),
15+
'jest-global-teardown-and-per-worker-project-1',
16+
);
17+
18+
test('should not exist teardown file', () => {
19+
expect(fs.existsSync(DIR)).toBe(false);
20+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
const crypto = require('crypto');
8+
const fs = require('fs');
9+
const os = require('os');
10+
const path = require('path');
11+
const {createDirectory} = require('jest-util');
12+
13+
const DIR = path.join(
14+
os.tmpdir(),
15+
'jest-global-teardown-and-per-worker-project-2',
16+
);
17+
18+
module.exports = function () {
19+
return new Promise((resolve, reject) => {
20+
createDirectory(DIR);
21+
const fileId = crypto.randomBytes(20).toString('hex');
22+
const data = ['teardown-per-worker', process.env.JEST_WORKER_ID].join('\n');
23+
fs.writeFileSync(path.join(DIR, fileId), data);
24+
resolve();
25+
});
26+
};

0 commit comments

Comments
 (0)