Skip to content

Commit 8415123

Browse files
committed
#8708 Add e2e for combination of globalTeardown and globalTeardownPerWorker
1 parent 866fa98 commit 8415123

File tree

14 files changed

+2101
-0
lines changed

14 files changed

+2101
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+
14+
const DIR = path.join(tmpdir(), 'jest-global-teardown-and-per-worker');
15+
const project1DIR = path.join(
16+
tmpdir(),
17+
'jest-global-teardown-and-per-worker-project-1',
18+
);
19+
const project2DIR = path.join(
20+
tmpdir(),
21+
'jest-global-teardown-and-per-worker-project-2',
22+
);
23+
const e2eDir = path.resolve(__dirname, '../global-teardown-and-per-worker');
24+
25+
beforeAll(() => {
26+
runYarnInstall(e2eDir);
27+
});
28+
29+
beforeEach(() => {
30+
cleanup(DIR);
31+
cleanup(project1DIR);
32+
cleanup(project2DIR);
33+
});
34+
35+
afterAll(() => {
36+
cleanup(DIR);
37+
cleanup(project1DIR);
38+
cleanup(project2DIR);
39+
});
40+
41+
test('globalTeardown triggered once + globalTeardownPerWorker triggered once per worker', () => {
42+
const teardownPath = path.join(e2eDir, 'teardown.js');
43+
const teardownPerWorkerPath = path.join(e2eDir, 'teardown-per-worker.js');
44+
const result = runWithJson(e2eDir, [
45+
'--maxWorkers=2',
46+
'--workerIdleMemoryLimit=100MB',
47+
`--globalTeardown=${teardownPath}`,
48+
`--globalTeardownPerWorker=${teardownPerWorkerPath}`,
49+
'--testPathPatterns=__tests__',
50+
]);
51+
52+
expect(result.exitCode).toBe(0);
53+
const files = fs.readdirSync(DIR);
54+
expect(files).toHaveLength(3);
55+
const content = files.map(file => {
56+
const data = fs.readFileSync(path.join(DIR, file), 'utf8');
57+
return data.split('\n');
58+
});
59+
for (const [firstLine, secondLine] of content) {
60+
if (secondLine) {
61+
expect(firstLine).toBe('teardown-per-worker');
62+
} else {
63+
expect(firstLine).toBe('teardown');
64+
}
65+
}
66+
const secondLines = content.map(([, secondLine]) => secondLine);
67+
secondLines.sort();
68+
expect(secondLines).toEqual(['1', '2', undefined]);
69+
});
70+
test('globalTeardown + globalTeardownPerWorker with worker threads', () => {
71+
const teardownPath = path.join(e2eDir, 'teardown.js');
72+
const teardownPerWorkerPath = path.join(e2eDir, 'teardown-per-worker.js');
73+
const result = runWithJson(e2eDir, [
74+
'--maxWorkers=2',
75+
'--workerIdleMemoryLimit=100MB',
76+
`--globalTeardown=${teardownPath}`,
77+
`--globalTeardownPerWorker=${teardownPerWorkerPath}`,
78+
'--testPathPatterns=__tests__',
79+
'--workerThreads',
80+
]);
81+
82+
expect(result.exitCode).toBe(0);
83+
const files = fs.readdirSync(DIR);
84+
expect(files).toHaveLength(3);
85+
const content = files.map(file => {
86+
const data = fs.readFileSync(path.join(DIR, file), 'utf8');
87+
return data.split('\n');
88+
});
89+
for (const [firstLine, secondLine] of content) {
90+
if (secondLine) {
91+
expect(firstLine).toBe('teardown-per-worker');
92+
} else {
93+
expect(firstLine).toBe('teardown');
94+
}
95+
}
96+
const secondLines = content.map(([, secondLine]) => secondLine);
97+
secondLines.sort();
98+
expect(secondLines).toEqual(['1', '2', undefined]);
99+
});
100+
101+
test('multiple projects', () => {
102+
const configPath = path.resolve(e2eDir, 'projects.jest.config.js');
103+
104+
const result = runWithJson(e2eDir, [
105+
'--maxWorkers=2',
106+
'--workerIdleMemoryLimit=100MB',
107+
`--config=${configPath}`,
108+
]);
109+
110+
expect(result.exitCode).toBe(0);
111+
112+
expect(fs.existsSync(DIR)).toBe(true);
113+
expect(fs.existsSync(project1DIR)).toBe(true);
114+
expect(fs.existsSync(project2DIR)).toBe(true);
115+
});
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: 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+
};
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-2',
16+
);
17+
18+
test('teardown file should not exist', () => {
19+
expect(fs.existsSync(DIR)).toBe(false);
20+
});

0 commit comments

Comments
 (0)