/
globalSetup.test.ts
210 lines (169 loc) · 6.01 KB
/
globalSetup.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {tmpdir} from 'os';
import * as path from 'path';
import * as fs from 'graceful-fs';
import {onNodeVersions} from '@jest/test-utils';
import {
cleanup,
createEmptyPackage,
runYarnInstall,
writeFiles,
} from '../Utils';
import runJest, {json as runWithJson} from '../runJest';
const DIR = path.join(tmpdir(), 'jest-global-setup');
const project1DIR = path.join(tmpdir(), 'jest-global-setup-project-1');
const project2DIR = path.join(tmpdir(), 'jest-global-setup-project-2');
const customTransformDIR = path.join(
tmpdir(),
'jest-global-setup-custom-transform',
);
const nodeModulesDIR = path.join(tmpdir(), 'jest-global-setup-node-modules');
const rejectionDir = path.join(tmpdir(), 'jest-global-setup-rejection');
const e2eDir = path.resolve(__dirname, '../global-setup');
const esmTmpDir = path.join(tmpdir(), 'jest-global-setup-esm');
beforeAll(() => {
runYarnInstall(e2eDir);
});
beforeEach(() => {
cleanup(DIR);
cleanup(project1DIR);
cleanup(project2DIR);
cleanup(customTransformDIR);
cleanup(nodeModulesDIR);
cleanup(rejectionDir);
cleanup(esmTmpDir);
});
afterAll(() => {
cleanup(DIR);
cleanup(project1DIR);
cleanup(project2DIR);
cleanup(customTransformDIR);
cleanup(nodeModulesDIR);
cleanup(rejectionDir);
cleanup(esmTmpDir);
});
test('globalSetup is triggered once before all test suites', () => {
const setupPath = path.join(e2eDir, 'setup.js');
const result = runWithJson(e2eDir, [
`--globalSetup=${setupPath}`,
'--testPathPattern=__tests__',
]);
expect(result.exitCode).toBe(0);
const files = fs.readdirSync(DIR);
expect(files).toHaveLength(1);
const setup = fs.readFileSync(path.join(DIR, files[0]), 'utf8');
expect(setup).toBe('setup');
});
test('jest throws an error when globalSetup does not export a function', () => {
const setupPath = path.resolve(__dirname, '../global-setup/invalidSetup.js');
const {exitCode, stderr} = runJest(e2eDir, [
`--globalSetup=${setupPath}`,
'--testPathPattern=__tests__',
]);
expect(exitCode).toBe(1);
expect(stderr).toContain('Jest: Got error running globalSetup');
expect(stderr).toContain(
`globalSetup file must export a function at ${setupPath}`,
);
});
test('globalSetup function gets global config object and project config as parameters', () => {
const setupPath = path.resolve(e2eDir, 'setupWithConfig.js');
const testPathPattern = 'pass';
const result = runJest(e2eDir, [
`--globalSetup=${setupPath}`,
`--testPathPattern=${testPathPattern}`,
'--cache=true',
]);
expect(result.stdout).toBe(`${testPathPattern}\ntrue`);
});
test('should call globalSetup function of multiple projects', () => {
const configPath = path.resolve(e2eDir, 'projects.jest.config.js');
const result = runWithJson(e2eDir, [`--config=${configPath}`]);
expect(result.exitCode).toBe(0);
expect(fs.existsSync(DIR)).toBe(true);
expect(fs.existsSync(project1DIR)).toBe(true);
expect(fs.existsSync(project2DIR)).toBe(true);
});
test('should not call a globalSetup of a project if there are no tests to run from this project', () => {
const configPath = path.resolve(e2eDir, 'projects.jest.config.js');
const result = runWithJson(e2eDir, [
`--config=${configPath}`,
'--testPathPattern=project-1',
]);
expect(result.exitCode).toBe(0);
expect(fs.existsSync(DIR)).toBe(true);
expect(fs.existsSync(project1DIR)).toBe(true);
expect(fs.existsSync(project2DIR)).toBe(false);
});
test('should not call any globalSetup if there are no tests to run', () => {
const configPath = path.resolve(e2eDir, 'projects.jest.config.js');
const result = runWithJson(e2eDir, [
`--config=${configPath}`,
// onlyChanged ensures there are no tests to run
'--onlyChanged',
]);
expect(result.exitCode).toBe(0);
expect(fs.existsSync(DIR)).toBe(false);
expect(fs.existsSync(project1DIR)).toBe(false);
expect(fs.existsSync(project2DIR)).toBe(false);
});
test('globalSetup works with default export', () => {
const setupPath = path.resolve(e2eDir, 'setupWithDefaultExport.js');
const testPathPattern = 'pass';
const result = runJest(e2eDir, [
`--globalSetup=${setupPath}`,
`--testPathPattern=${testPathPattern}`,
'--cache=true',
]);
expect(result.stdout).toBe(`${testPathPattern}\ntrue`);
});
test('globalSetup throws with named export', () => {
const setupPath = path.resolve(e2eDir, 'invalidSetupWithNamedExport.js');
const {exitCode, stderr} = runJest(e2eDir, [
`--globalSetup=${setupPath}`,
'--testPathPattern=__tests__',
]);
expect(exitCode).toBe(1);
expect(stderr).toContain('Jest: Got error running globalSetup');
expect(stderr).toContain(
`globalSetup file must export a function at ${setupPath}`,
);
});
test('should not transpile the transformer', () => {
const {exitCode} = runJest('global-setup-custom-transform', ['--no-cache']);
expect(exitCode).toBe(0);
});
test('should transform node_modules if configured by transformIgnorePatterns', () => {
const {exitCode} = runJest('global-setup-node-modules', ['--no-cache']);
expect(exitCode).toBe(0);
});
test('properly handle rejections', () => {
createEmptyPackage(rejectionDir, {jest: {globalSetup: '<rootDir>/setup.js'}});
writeFiles(rejectionDir, {
'setup.js': `
module.exports = () => Promise.reject();
`,
'test.js': `
test('dummy', () => {
expect(true).toBe(true);
});
`,
});
const {exitCode, stderr} = runJest(rejectionDir, ['--no-cache']);
expect(exitCode).toBe(1);
expect(stderr).toContain('Error: Jest: Got error running globalSetup');
expect(stderr).toContain('reason: undefined');
});
onNodeVersions('>=12.17.0', () => {
test('globalSetup works with ESM modules', () => {
const {exitCode} = runJest('global-setup-esm', ['--no-cache'], {
nodeOptions: '--experimental-vm-modules --no-warnings',
});
expect(exitCode).toBe(0);
});
});