Skip to content

Commit 4cd6bb3

Browse files
committed
#8708 Copy e2e test of globalSetup for globalSetupPerWorker
1 parent 9869d99 commit 4cd6bb3

File tree

34 files changed

+2467
-0
lines changed

34 files changed

+2467
-0
lines changed
Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
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 {
12+
cleanup,
13+
createEmptyPackage,
14+
runYarnInstall,
15+
writeFiles,
16+
} from '../Utils';
17+
import runJest, {json as runWithJson} from '../runJest';
18+
19+
const DIR = path.join(tmpdir(), 'jest-global-setup-per-worker');
20+
const project1DIR = path.join(
21+
tmpdir(),
22+
'jest-global-setup-per-worker-project-1',
23+
);
24+
const project2DIR = path.join(
25+
tmpdir(),
26+
'jest-global-setup-per-worker-project-2',
27+
);
28+
const customTransformDIR = path.join(
29+
tmpdir(),
30+
'jest-global-setup-per-worker-custom-transform',
31+
);
32+
const nodeModulesDIR = path.join(
33+
tmpdir(),
34+
'jest-global-setup-per-worker-node-modules',
35+
);
36+
const rejectionDir = path.join(
37+
tmpdir(),
38+
'jest-global-setup-per-worker-rejection',
39+
);
40+
const e2eDir = path.resolve(__dirname, '../global-setup-per-worker');
41+
const esmTmpDir = path.join(tmpdir(), 'jest-global-setup-per-worker-esm');
42+
43+
beforeAll(() => {
44+
runYarnInstall(e2eDir);
45+
});
46+
47+
beforeEach(() => {
48+
cleanup(DIR);
49+
cleanup(project1DIR);
50+
cleanup(project2DIR);
51+
cleanup(customTransformDIR);
52+
cleanup(nodeModulesDIR);
53+
cleanup(rejectionDir);
54+
cleanup(esmTmpDir);
55+
});
56+
57+
afterAll(() => {
58+
cleanup(DIR);
59+
cleanup(project1DIR);
60+
cleanup(project2DIR);
61+
cleanup(customTransformDIR);
62+
cleanup(nodeModulesDIR);
63+
cleanup(rejectionDir);
64+
cleanup(esmTmpDir);
65+
});
66+
67+
test('globalSetupPerWorker is triggered once before all test suites per worker', () => {
68+
const setupPath = path.join(e2eDir, 'setup.js');
69+
const result = runWithJson(e2eDir, [
70+
`--globalSetupPerWorker=${setupPath}`,
71+
'--testPathPatterns=__tests__',
72+
]);
73+
74+
expect(result.exitCode).toBe(0);
75+
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');
79+
});
80+
81+
test('jest throws an error when globalSetupPerWorker does not export a function', () => {
82+
const setupPath = path.resolve(
83+
__dirname,
84+
'../global-setup-per-worker/invalidSetup.js',
85+
);
86+
const {exitCode, stderr} = runJest(e2eDir, [
87+
`--globalSetupPerWorker=${setupPath}`,
88+
'--testPathPatterns=__tests__',
89+
]);
90+
91+
expect(exitCode).toBe(1);
92+
expect(stderr).toContain('Jest: Got error running globalSetupPerWorker');
93+
expect(stderr).toContain(
94+
`globalSetupPerWorker file must export a function at ${setupPath}`,
95+
);
96+
});
97+
98+
test('globalSetupPerWorker function gets global config object and project config as parameters', () => {
99+
const setupPath = path.resolve(e2eDir, 'setupWithConfig.js');
100+
101+
const result = runJest(e2eDir, [
102+
`--globalSetupPerWorker=${setupPath}`,
103+
'--testPathPatterns=pass',
104+
'--cache=true',
105+
]);
106+
107+
expect(result.stdout).toBe("[ 'pass' ]\ntrue");
108+
});
109+
110+
test('should call globalSetupPerWorker function of multiple projects', () => {
111+
const configPath = path.resolve(e2eDir, 'projects.jest.config.js');
112+
113+
const result = runWithJson(e2eDir, [`--config=${configPath}`]);
114+
115+
expect(result.exitCode).toBe(0);
116+
117+
expect(fs.existsSync(DIR)).toBe(true);
118+
expect(fs.existsSync(project1DIR)).toBe(true);
119+
expect(fs.existsSync(project2DIR)).toBe(true);
120+
});
121+
122+
test('should not call a globalSetupPerWorker of a project if there are no tests to run from this project', () => {
123+
const configPath = path.resolve(e2eDir, 'projects.jest.config.js');
124+
125+
const result = runWithJson(e2eDir, [
126+
`--config=${configPath}`,
127+
'--testPathPatterns=setup1',
128+
]);
129+
130+
expect(result.exitCode).toBe(0);
131+
132+
expect(fs.existsSync(DIR)).toBe(true);
133+
expect(fs.existsSync(project1DIR)).toBe(true);
134+
expect(fs.existsSync(project2DIR)).toBe(false);
135+
});
136+
137+
test('should not call any globalSetupPerWorker if there are no tests to run', () => {
138+
const configPath = path.resolve(e2eDir, 'projects.jest.config.js');
139+
140+
const result = runWithJson(e2eDir, [
141+
`--config=${configPath}`,
142+
'--onlyChanged',
143+
]);
144+
145+
expect(result.exitCode).toBe(0);
146+
147+
expect(fs.existsSync(DIR)).toBe(false);
148+
expect(fs.existsSync(project1DIR)).toBe(false);
149+
expect(fs.existsSync(project2DIR)).toBe(false);
150+
});
151+
152+
test('globalSetupPerWorker works with default export', () => {
153+
const setupPath = path.resolve(e2eDir, 'setupWithDefaultExport.js');
154+
155+
const result = runJest(e2eDir, [
156+
`--globalSetupPerWorker=${setupPath}`,
157+
'--testPathPatterns=pass',
158+
'--cache=true',
159+
]);
160+
161+
expect(result.stdout).toBe("[ 'pass' ]\ntrue");
162+
});
163+
164+
test('globalSetupPerWorker throws with named export', () => {
165+
const setupPath = path.resolve(e2eDir, 'invalidSetupWithNamedExport.js');
166+
167+
const {exitCode, stderr} = runJest(e2eDir, [
168+
`--globalSetupPerWorker=${setupPath}`,
169+
'--testPathPatterns=__tests__',
170+
]);
171+
172+
expect(exitCode).toBe(1);
173+
expect(stderr).toContain('Jest: Got error running globalSetupPerWorker');
174+
expect(stderr).toContain(
175+
`globalSetupPerWorker file must export a function at ${setupPath}`,
176+
);
177+
});
178+
179+
test('should not transpile the transformer', () => {
180+
const {exitCode} = runJest('global-setup-per-worker-custom-transform', [
181+
'--no-cache',
182+
]);
183+
184+
expect(exitCode).toBe(0);
185+
});
186+
187+
test('should transform node_modules if configured by transformIgnorePatterns', () => {
188+
const {exitCode} = runJest('global-setup-per-worker-node-modules', [
189+
'--no-cache',
190+
]);
191+
192+
expect(exitCode).toBe(0);
193+
});
194+
195+
test('properly handle rejections', () => {
196+
createEmptyPackage(rejectionDir, {
197+
jest: {globalSetupPerWorker: '<rootDir>/setup.js'},
198+
});
199+
writeFiles(rejectionDir, {
200+
'setup.js': `
201+
module.exports = () => Promise.reject();
202+
`,
203+
'test.js': `
204+
test('dummy', () => {
205+
expect(true).toBe(true);
206+
});
207+
`,
208+
});
209+
210+
const {exitCode, stderr} = runJest(rejectionDir, ['--no-cache']);
211+
212+
expect(exitCode).toBe(1);
213+
expect(stderr).toContain(
214+
'Error: Jest: Got error running globalSetupPerWorker',
215+
);
216+
expect(stderr).toContain('reason: undefined');
217+
});
218+
219+
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+
});
223+
224+
expect(exitCode).toBe(0);
225+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
const greeting = require('../');
13+
14+
const DIR = path.join(
15+
os.tmpdir(),
16+
'jest-global-setup-per-worker-custom-transform',
17+
);
18+
19+
test('should exist setup file', () => {
20+
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');
24+
});
25+
26+
test('should transform imported file', () => {
27+
expect(greeting).toBe('hello, world!');
28+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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 = 'hello!';
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"jest": {
3+
"testEnvironment": "node",
4+
"globalSetupPerWorker": "<rootDir>/setup.js",
5+
"transform": {
6+
"\\.js$": "<rootDir>/transformer.js"
7+
}
8+
}
9+
}
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-per-worker-custom-transform',
16+
);
17+
18+
module.exports = function () {
19+
return new Promise(resolve => {
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: 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+
8+
'use strict';
9+
10+
const fileToTransform = require.resolve('./index.js');
11+
12+
module.exports = {
13+
process(src, filename) {
14+
if (filename === fileToTransform) {
15+
return {code: src.replace('hello', 'hello, world')};
16+
}
17+
18+
return {code: src};
19+
},
20+
};
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+
8+
import * as os from 'os';
9+
import * as path from 'path';
10+
import fs from 'graceful-fs';
11+
import greeting from '../';
12+
13+
const DIR = path.join(os.tmpdir(), 'jest-global-setup-per-worker-esm');
14+
15+
test('should exist setup file', () => {
16+
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');
20+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
export default 'hello!';
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"type": "module",
3+
"jest": {
4+
"testEnvironment": "node",
5+
"globalSetupPerWorker": "<rootDir>/setup.js",
6+
"transform": {}
7+
}
8+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
import * as crypto from 'crypto';
8+
import * as os from 'os';
9+
import * as path from 'path';
10+
import fs from 'graceful-fs';
11+
import {createDirectory} from 'jest-util';
12+
13+
const DIR = path.join(os.tmpdir(), 'jest-global-setup-per-worker-esm');
14+
15+
export default function () {
16+
return new Promise(resolve => {
17+
createDirectory(DIR);
18+
const fileId = crypto.randomBytes(20).toString('hex');
19+
fs.writeFileSync(path.join(DIR, fileId), 'setup');
20+
resolve();
21+
});
22+
}

0 commit comments

Comments
 (0)