/
semanticInfo-singleRun.test.ts
254 lines (215 loc) · 8.5 KB
/
semanticInfo-singleRun.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import glob from 'glob';
import * as path from 'path';
import {
clearProgramCache,
parseAndGenerateServices,
ɵclearParseAndGenerateServicesCalls,
} from '../../src';
import { getCanonicalFileName } from '../../src/create-program/shared';
const mockProgram = {
getSourceFile(): void {
return;
},
getTypeChecker(): void {
return;
},
};
jest.mock('../../src/ast-converter', () => {
return {
astConverter(): unknown {
return { estree: {}, astMaps: {} };
},
};
});
interface MockProgramWithConfigFile {
__FROM_CONFIG_FILE__?: string;
}
jest.mock('../../src/create-program/shared.ts', () => {
return {
...jest.requireActual('../../src/create-program/shared.ts'),
getAstFromProgram(program: MockProgramWithConfigFile): unknown {
if (
program.__FROM_CONFIG_FILE__?.endsWith('non-matching-tsconfig.json')
) {
return null;
}
// Remove temporary tracking value for the config added by mock createProgramFromConfigFile() below
delete program.__FROM_CONFIG_FILE__;
return { ast: {}, program };
},
};
});
jest.mock('../../src/create-program/useProvidedPrograms.ts', () => {
return {
...jest.requireActual('../../src/create-program/useProvidedPrograms.ts'),
createProgramFromConfigFile: jest
.fn()
.mockImplementation((configFile): MockProgramWithConfigFile => {
return {
// So we can differentiate our mock return values based on which tsconfig this is
__FROM_CONFIG_FILE__: configFile,
...mockProgram,
};
}),
};
});
jest.mock('../../src/create-program/createWatchProgram', () => {
return {
...jest.requireActual('../../src/create-program/createWatchProgram'),
getProgramsForProjects: jest.fn(() => [mockProgram]),
};
});
const {
createProgramFromConfigFile,
} = require('../../src/create-program/useProvidedPrograms');
const FIXTURES_DIR = './tests/fixtures/semanticInfo';
const testFiles = glob.sync(`**/*.src.ts`, {
cwd: FIXTURES_DIR,
});
const code = 'const foo = 5;';
// File will not be found in the first Program, but will be in the second
const tsconfigs = ['./non-matching-tsconfig.json', './tsconfig.json'];
const options = {
filePath: testFiles[0],
tsconfigRootDir: path.join(process.cwd(), FIXTURES_DIR),
loggerFn: false,
project: tsconfigs,
allowAutomaticSingleRunInference: true,
} as const;
const resolvedProject = (p: string): string =>
getCanonicalFileName(path.resolve(path.join(process.cwd(), FIXTURES_DIR), p));
describe('semanticInfo - singleRun', () => {
beforeEach(() => {
// ensure caches are clean for each test
clearProgramCache();
// ensure invocations of mock are clean for each test
(createProgramFromConfigFile as jest.Mock).mockClear();
// Do not track invocations per file across tests
ɵclearParseAndGenerateServicesCalls();
});
it('should not create any programs ahead of time by default when there is no way to infer singleRun=true', () => {
// For when these tests themselves are running in CI, we need to ignore that for this particular spec
const originalEnvCI = process.env.CI;
process.env.CI = 'false';
/**
* At this point there is nothing to indicate it is a single run, so createProgramFromConfigFile should
* never be called
*/
parseAndGenerateServices(code, options);
expect(createProgramFromConfigFile).not.toHaveBeenCalled();
// Restore process data
process.env.CI = originalEnvCI;
});
it('should not create any programs ahead of time when when TSESTREE_SINGLE_RUN=false, even if other inferrence criteria apply', () => {
const originalTSESTreeSingleRun = process.env.TSESTREE_SINGLE_RUN;
process.env.TSESTREE_SINGLE_RUN = 'false';
// Normally CI=true would be used to infer singleRun=true, but TSESTREE_SINGLE_RUN is explicitly set to false
const originalEnvCI = process.env.CI;
process.env.CI = 'true';
parseAndGenerateServices(code, options);
expect(createProgramFromConfigFile).not.toHaveBeenCalled();
// Restore process data
process.env.TSESTREE_SINGLE_RUN = originalTSESTreeSingleRun;
process.env.CI = originalEnvCI;
});
it('should lazily create the required program out of the provided "parserOptions.project" one time when TSESTREE_SINGLE_RUN=true', () => {
/**
* Single run because of explicit environment variable TSESTREE_SINGLE_RUN
*/
const originalTSESTreeSingleRun = process.env.TSESTREE_SINGLE_RUN;
process.env.TSESTREE_SINGLE_RUN = 'true';
const resultProgram = parseAndGenerateServices(code, options).services
.program;
expect(resultProgram).toEqual(mockProgram);
// Call parseAndGenerateServices() again to ensure caching of Programs is working correctly...
parseAndGenerateServices(code, options);
// ...by asserting this was only called once per project
expect(createProgramFromConfigFile).toHaveBeenCalledTimes(tsconfigs.length);
expect(createProgramFromConfigFile).toHaveBeenNthCalledWith(
1,
resolvedProject(tsconfigs[0]),
);
expect(createProgramFromConfigFile).toHaveBeenNthCalledWith(
2,
resolvedProject(tsconfigs[1]),
);
// Restore process data
process.env.TSESTREE_SINGLE_RUN = originalTSESTreeSingleRun;
});
it('should lazily create the required program out of the provided "parserOptions.project" one time when singleRun is inferred from CI=true', () => {
/**
* Single run because of CI=true (we need to make sure we respect the original value
* so that we won't interfere with our own usage of the variable)
*/
const originalEnvCI = process.env.CI;
process.env.CI = 'true';
const resultProgram = parseAndGenerateServices(code, options).services
.program;
expect(resultProgram).toEqual(mockProgram);
// Call parseAndGenerateServices() again to ensure caching of Programs is working correctly...
parseAndGenerateServices(code, options);
// ...by asserting this was only called once per project
expect(createProgramFromConfigFile).toHaveBeenCalledTimes(tsconfigs.length);
expect(createProgramFromConfigFile).toHaveBeenNthCalledWith(
1,
resolvedProject(tsconfigs[0]),
);
expect(createProgramFromConfigFile).toHaveBeenNthCalledWith(
2,
resolvedProject(tsconfigs[1]),
);
// Restore process data
process.env.CI = originalEnvCI;
});
it('should lazily create the required program out of the provided "parserOptions.project" one time when singleRun is inferred from process.argv', () => {
/**
* Single run because of process.argv
*/
const originalProcessArgv = process.argv;
process.argv = ['', 'node_modules/.bin/eslint', ''];
const resultProgram = parseAndGenerateServices(code, options).services
.program;
expect(resultProgram).toEqual(mockProgram);
// Call parseAndGenerateServices() again to ensure caching of Programs is working correctly...
parseAndGenerateServices(code, options);
// ...by asserting this was only called once per project
expect(createProgramFromConfigFile).toHaveBeenCalledTimes(tsconfigs.length);
expect(createProgramFromConfigFile).toHaveBeenNthCalledWith(
1,
resolvedProject(tsconfigs[0]),
);
expect(createProgramFromConfigFile).toHaveBeenNthCalledWith(
2,
resolvedProject(tsconfigs[1]),
);
// Restore process data
process.argv = originalProcessArgv;
});
it('should stop iterating through and lazily creating programs for the given "parserOptions.project" once a matching one has been found', () => {
/**
* Single run because of explicit environment variable TSESTREE_SINGLE_RUN
*/
const originalTSESTreeSingleRun = process.env.TSESTREE_SINGLE_RUN;
process.env.TSESTREE_SINGLE_RUN = 'true';
const optionsWithReversedTsconfigs = {
...options,
// Now the matching tsconfig comes first
project: options.project.reverse(),
};
const resultProgram = parseAndGenerateServices(
code,
optionsWithReversedTsconfigs,
).services.program;
expect(resultProgram).toEqual(mockProgram);
// Call parseAndGenerateServices() again to ensure caching of Programs is working correctly...
parseAndGenerateServices(code, options);
// ...by asserting this was only called only once
expect(createProgramFromConfigFile).toHaveBeenCalledTimes(1);
expect(createProgramFromConfigFile).toHaveBeenNthCalledWith(
1,
resolvedProject(tsconfigs[0]),
);
// Restore process data
process.env.TSESTREE_SINGLE_RUN = originalTSESTreeSingleRun;
});
});