Skip to content

Commit e2ff745

Browse files
committed
#8708 Add e2e for combination of globalSetup and globalSetupPerWorker
1 parent 8210505 commit e2ff745

16 files changed

+2246
-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-setup-and-per-worker');
15+
const project1DIR = path.join(
16+
tmpdir(),
17+
'jest-global-setup-and-per-worker-project-1',
18+
);
19+
const project2DIR = path.join(
20+
tmpdir(),
21+
'jest-global-setup-and-per-worker-project-2',
22+
);
23+
const e2eDir = path.resolve(__dirname, '../global-setup-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('globalSetup triggered once + globalSetupPerWorker triggered once per worker', () => {
42+
const setupPath = path.join(e2eDir, 'setup.js');
43+
const setupPerWorkerPath = path.join(e2eDir, 'setup-per-worker.js');
44+
const result = runWithJson(e2eDir, [
45+
'--maxWorkers=2',
46+
'--workerIdleMemoryLimit=100MB',
47+
`--globalSetup=${setupPath}`,
48+
`--globalSetupPerWorker=${setupPerWorkerPath}`,
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('setup-per-worker');
62+
} else {
63+
expect(firstLine).toBe('setup');
64+
}
65+
}
66+
const secondLines = content.map(([, secondLine]) => secondLine);
67+
secondLines.sort();
68+
expect(secondLines).toEqual(['1', '2', undefined]);
69+
});
70+
test('globalSetup + globalSetupPerWorker with worker threads', () => {
71+
const setupPath = path.join(e2eDir, 'setup.js');
72+
const setupPerWorkerPath = path.join(e2eDir, 'setup-per-worker.js');
73+
const result = runWithJson(e2eDir, [
74+
'--maxWorkers=2',
75+
'--workerIdleMemoryLimit=100MB',
76+
`--globalSetup=${setupPath}`,
77+
`--globalSetupPerWorker=${setupPerWorkerPath}`,
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('setup-per-worker');
92+
} else {
93+
expect(firstLine).toBe('setup');
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: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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-setup-and-per-worker');
14+
15+
test('should exist setup files', () => {
16+
const files = fs.readdirSync(DIR);
17+
expect(files).toHaveLength(3);
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, secondLine] of content) {
23+
if (secondLine) {
24+
expect(firstLine).toBe('setup-per-worker');
25+
} else {
26+
expect(firstLine).toBe('setup');
27+
}
28+
}
29+
const secondLines = content.map(([, secondLine]) => secondLine);
30+
secondLines.sort();
31+
expect(secondLines).toEqual(['1', '2', undefined]);
32+
expect(secondLines).toEqual(
33+
expect.arrayContaining([process.env.JEST_WORKER_ID]),
34+
);
35+
});
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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-setup-and-per-worker');
14+
15+
test('should exist setup files', () => {
16+
const files = fs.readdirSync(DIR);
17+
expect(files).toHaveLength(3);
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, secondLine] of content) {
23+
if (secondLine) {
24+
expect(firstLine).toBe('setup-per-worker');
25+
} else {
26+
expect(firstLine).toBe('setup');
27+
}
28+
}
29+
const secondLines = content.map(([, secondLine]) => secondLine);
30+
secondLines.sort();
31+
expect(secondLines).toEqual(['1', '2', undefined]);
32+
expect(secondLines).toEqual(
33+
expect.arrayContaining([process.env.JEST_WORKER_ID]),
34+
);
35+
});
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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-setup-and-per-worker');
14+
15+
test('should exist setup files', () => {
16+
const files = fs.readdirSync(DIR);
17+
expect(files).toHaveLength(3);
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, secondLine] of content) {
23+
if (secondLine) {
24+
expect(firstLine).toBe('setup-per-worker');
25+
} else {
26+
expect(firstLine).toBe('setup');
27+
}
28+
}
29+
const secondLines = content.map(([, secondLine]) => secondLine);
30+
secondLines.sort();
31+
expect(secondLines).toEqual(['1', '2', undefined]);
32+
expect(secondLines).toEqual(
33+
expect.arrayContaining([process.env.JEST_WORKER_ID]),
34+
);
35+
});
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-setup-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 = ['setup-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-setup-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), 'setup');
23+
resolve();
24+
});
25+
};
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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-setup-and-per-worker-project-1',
16+
);
17+
18+
test('should exist setup files', () => {
19+
const files = fs.readdirSync(DIR);
20+
expect(files).toHaveLength(3);
21+
const content = files.map(file => {
22+
const data = fs.readFileSync(path.join(DIR, file), 'utf8');
23+
return data.split('\n');
24+
});
25+
for (const [firstLine, secondLine] of content) {
26+
if (secondLine) {
27+
expect(firstLine).toBe('setup-per-worker');
28+
} else {
29+
expect(firstLine).toBe('setup');
30+
}
31+
}
32+
const secondLines = content.map(([, secondLine]) => secondLine);
33+
secondLines.sort();
34+
expect(secondLines).toEqual(['1', '2', undefined]);
35+
expect(secondLines).toEqual(
36+
expect.arrayContaining([process.env.JEST_WORKER_ID]),
37+
);
38+
});
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-setup-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 = ['setup-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)